您好我正在使用一些小的VB.net应用程序来管理我的Exchange 2010服务器上的分发列表。此代码调用带有2个参数的ps1脚本及其工作。我唯一的问题是我想捕获PowerShell.Invoke()的输出。在执行PowerShellCommandResults之后总是空了没有集合中的记录 也许问题来自于从ps1文件中捕获但我真的不知道在哪里解决我的问题
THX
Dim PowerShell As Management.Automation.PowerShell = PowerShell.Create()
Dim PowerShellCommand As New PSCommand()
Dim PowerShellCommandResults As Collection(Of PSObject)
PowerShellCommand.AddCommand("c:\scripts\connect.ps1")
PowerShellCommand.AddCommand("c:\scripts\newld.ps1")
PowerShellCommand.AddArgument("test@cscapitale.qc.ca")
PowerShellCommand.AddArgument("Distribution liste test")
Try
PowerShellCommandResults = PowerShell.Invoke()
Dim sw As New StreamWriter("C:\" & Now.ToLongDateString & ".txt")
For Each line In PowerShellCommandResults
sw.WriteLine(line.ToString)
Next
sw.Dispose()
PowerShell.Dispose()
Catch ex As Exception
End Try
答案 0 :(得分:1)
您是否尝试过使用Powershell.Invoke(IEnumerable, IList)
签名?我通常使用C#所以我可能没有完全正确...
' Old and busted: PowerShellCommandResults = PowerShell.Invoke()
' New hotness:
PowerShell.Invoke(Nothing, PowerShellCommandResults)
' Everything below here is unchanged
Dim sw As New StreamWriter("C:\" & Now.ToLongDateString & ".txt")
For Each line In PowerShellCommandResults
sw.WriteLine(line.ToString)
Next
替代答案:尝试使用PowerShellCommand.AddScript("C:\Scripts\Connect.ps1")
备选答案:确保connect.ps1
和newld.ps1
脚本输出到管道,而不是主机。
# Bad: Explicitly writes to the host, and skips pipeline output
Write-Host "Some value"
# Good: A string, or variable on its own will be output on the pipeline
"Some Value"
$OtherVariable