VB.net中powershell命令的陷阱输出

VB.net中powershell命令的陷阱输出,powershell,output,vb.net-2010,Powershell,Output,Vb.net 2010,您好,我正在开发一个小VB.net应用程序来管理exchange 2010服务器上的通讯组列表。此代码使用2个参数调用ps1脚本及其工作。我唯一的问题是想捕获PowerShell.Invoke()的输出。执行后,PowerShellCommandResults始终为空,集合中没有记录。 也许问题来自ps1文件中的陷阱,但我真的不知道从哪里解决我的问题 thx 您是否尝试过改用Powershell.Invoke(IEnumerable,IList)签名?我通常使用C#,所以我可能不完全正确 ' O

您好,我正在开发一个小VB.net应用程序来管理exchange 2010服务器上的通讯组列表。此代码使用2个参数调用ps1脚本及其工作。我唯一的问题是想捕获PowerShell.Invoke()的输出。执行后,PowerShellCommandResults始终为空,集合中没有记录。
也许问题来自ps1文件中的陷阱,但我真的不知道从哪里解决我的问题

thx


您是否尝试过改用
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

thx问题是我的脚本,它们没有返回管道!
# 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