Follow スクリプトは win 7 では問題なく正常に動作しましたが、アプリを Windows 8 にロードして何が壊れているかを確認すると、このスクリプトが正しく動作しなくなることがわかりました。ユーザーが大規模なアプリ内にメールボックスを持っているかどうかを検索するために、ユーザー交換メールボックスをロードする非常に単純なスクリプトです。プラグイン システムとして IronPython を使用しているため、実際には Python で記述されています。
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 が問題の原因であるかどうかはわかりませんが、win 7 の正確なコードでは null 値なしで正常に動作します。どんな提案でも大いに役立ちます。
更新(回答):
絶え間なく掘り下げた後、最終的に問題が Powershell 3 にあることがわかりました。IronPython で Powershell 2 を使用してメンバーにアクセスします。result.Members["Name"].Value を使用できますが、Powershell 3 と DLR では、IronPython は null 値を返しますresult.Members コレクションなので、result.Name として呼び出す必要があります。(結果 = PSObject)