[英]How to retrieve PSComputerName from OriginInfo when returned through ps.Streams.Error.DataAdded
我正在运行下面的代码来检查远程计算机上特定运行进程的运行情况,如果它找到进程,那么我可以在 output.dataadded 事件中捕获它并将计算机名添加到列表中(processrunning),如果它没有找到它然后我想在 ps.Streams.Error.DataAdded 事件中捕获它并将计算机名添加到另一个列表(processnotrunning)
但我似乎无法从 OriginInfo 访问 PSComputerName,即使我可以在调试器中看到值(见下面的截图)
如何在 Private Sub Error_DataAdded_ProcessRunning 中获取故障计算机的名称以添加到我的列表中? 我运行的其他 PowerShell 命令没有这个问题,例如 "ps.AddCommand("Invoke-Command").AddParameter("ComputerName", online).AddParameter("ScriptBlock", ScriptBlock.Create("1" ))" 或 "ps.AddCommand("Test-Connection").AddParameter("ComputerName", computerlist).AddParameter("Count", 1)" 对于这些我总是能够使用 "currentStreamRecord.目标对象.ToString"
谢谢
Dim test = "MYPC"
Dim processtocheck = "notepad"
Using ps As PowerShell = PowerShell.Create()
Dim command As New PSCommand()
command.AddCommand("Invoke-Command")
command.AddParameter("ComputerName", test)
command.AddParameter("ScriptBlock", ScriptBlock.Create("Get-Process " & processtocheck))
ps.Commands = command
Dim output As New PSDataCollection(Of PSObject)()
AddHandler output.DataAdded, AddressOf Output_DataAdded_ProcessRunning
AddHandler ps.Streams.Error.DataAdded, AddressOf Error_DataAdded_ProcessRunning
Dim results As PSDataCollection(Of PSObject) = Await Task.Run(Function() ps.InvokeAsync(Of PSObject, PSObject)(Nothing, output))
End Using
Private Sub Output_DataAdded_ProcessRunning(ByVal sender As Object, ByVal e As DataAddedEventArgs)
Dim myp As PSDataCollection(Of PSObject) = DirectCast(sender, PSDataCollection(Of PSObject))
Dim results As Collection(Of PSObject) = myp.ReadAll()
For Each result As PSObject In results
' add to list
AddIfNotExists(processrunning, result.Properties("PSComputerName").Value)
Next result
End Sub
Private Sub Error_DataAdded_ProcessRunning(ByVal sender As Object, ByVal e As DataAddedEventArgs)
' do something when an error is written to the error stream
Dim streamObjectsReceived = TryCast(sender, PSDataCollection(Of ErrorRecord))
Dim currentStreamRecord = streamObjectsReceived(e.Index)
' add to list
' ???
End Sub
在另一个论坛上从有帮助的人那里得到答案,因此在下面复制以供参考,因为它解决了我的问题:-
OriginInfo 是 RemotingErrorRecord 的成员,而不是 ErrorRecord。 https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.remotingerrorrecord.origininfo?view=powershellsdk-7.0.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.