Follow腳本在win 7下運行良好,沒有問題,但是當我在Windows 8上加載我的應用程序以查看所有內容已損壞時,我發現此腳本將無法正常工作。它是一個非常簡單的腳本,用於加載用戶交換郵箱,以便搜索用戶是否在更大的應用程序中有郵箱。它實際上是用python編寫的,因爲我使用IronPython作爲插件系統。Windows 8上的Powershell Exchange 2010
password = SecureString()
str_password = "PASSWORD"
str_user = "EXCHUSER"
uri = "http://" + exchangeServerIP + "/PowerShell"
for c in str_password:
password.AppendChar(c)
creds = PSCredential(str_user, password)
connectionInfo = WSManConnectionInfo(Uri(uri), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", creds)
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic
runspace = RunspaceFactory.CreateRunspace(connectionInfo)
runspace.Open()
powershell = PowerShell.Create()
powershell.AddCommand("Get-Mailbox")
if self.txtUsername.Text != "*":
powershell.AddParameter("Identity", self.txtUsername.Text)
powershell.AddParameter("ResultSize", "Unlimited")
powershell.Runspace = runspace
results = powershell.Invoke()
if results == None or results.Count <= 0:
MessageBox.Show("No mailboxes can be found!")
runspace.Close()
runspace.Dispose()
powershell.Dispose()
return
self.dataGridView1.Rows.Clear()
for result in results:
>>>>>>> if result.Properties != None: (ERROR HERE) <<<<<<<
if result.Properties["Name"] != None:
if result.Properties["Name"].Value != None:
System.Console.WriteLine(result.Properties["Name"].Value.ToString())
runspace.Dispose()
runspace.Close()
runspace = None
powershell.Dispose()
powershell = None
connectionInfo = None
我已標記在那裏我得到的錯誤說法result.Properties是調試時,我可以看到它填充和正確的結果是有NoneType。不知道如果Windows 8/.net 4.5是問題的原因,但在贏得7確切代碼沒有空值的情況下工作正常。任何建議都會有很大幫助。
更新(答):
不斷的挖掘後,我終於發現,這個問題是使用PowerShell 3.訪問使用PowerShell 2 IronPython的,我可以用result.Members [「名稱」]的成員。值,但是對於Powershell 3和DLR,IronPython將在result.Members集合上返回null值,所以我必須將它作爲result.Name來調用。 (其中結果= PSObject)