0

I am running the below code to check running for a specific running process on a remote computer, if it finds process then I can catch it in output.dataadded event and add computername to a list (processrunning), if it doesn't find it then I want to catch it in ps.Streams.Error.DataAdded event and add computername to another list (processnotrunning)

But I can't seem to access PSComputerName from OriginInfo even though I can see the value in the debugger (see screenshot below)

How in Private Sub Error_DataAdded_ProcessRunning can I get the name of the failing computer to add to my list? I haven't had this issue with other PowerShell command I have run such as "ps.AddCommand("Invoke-Command").AddParameter("ComputerName", online).AddParameter("ScriptBlock", ScriptBlock.Create("1"))" or "ps.AddCommand("Test-Connection").AddParameter("ComputerName", computerlist).AddParameter("Count", 1)" as for these I have always been able to get the name using "currentStreamRecord.TargetObject.ToString"

enter image description here

enter image description here

Thanks

  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
Darren Rose
  • 169
  • 13

1 Answers1

0

Got answer from helpful person on another forum so copied below for reference as it solved my problem:-

OriginInfo is a member of RemotingErrorRecord instead of ErrorRecord. https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.remotingerrorrecord.origininfo?view=powershellsdk-7.0.0

Darren Rose
  • 169
  • 13