How to retrieve PSComputerName from OriginInfo when returned through ps.Streams.Error.DataAdded

Darren Rose 311 Reputation points
2022-07-26T10:17:58.007+00:00

Hi

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"

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  

224757-image.png

Windows for business | Windows Server | User experience | PowerShell
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-07-27T10:08:21.603+00:00

    Hi @Darren Rose ,

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

    What did you get, null or an error?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.