r/PowerShell•
br0phy

Blank output from native command piped to select-string in invoke-command

Question

Could someone please help me understand what's happening here.

Run a native command and pipe it to select-string - get the expected output.

PS C:\temp\telegraf> & cmd /C 'C:\temp\telegraf\telegraf.exe -once -console -config c:\temp\telegraf\telegraf_broker_con
nectivity_test.conf 2>&1' | select-string -SimpleMatch 'E! '

2021-06-09T12:59:46Z E! [inputs.x509_cert] Error in plugin: cannot get SSL cert
'tcp://[...]': dial tcp [...]: i/o timeout
2021-06-09T12:59:46Z E! [inputs.x509_cert] Error in plugin: cannot get SSL cert
'tcp://[...]': dial tcp [...]: i/o timeout
2021-06-09T12:59:46Z E! [telegraf] Error running agent: input plugins recorded 2 errors


PS C:\temp\telegraf>

From a remote machine: Run the same command via Invoke-Command - blank output

PS C:\> Invoke-Command -Credential $cred_svc -ComputerName my.remote.server.com -ScriptBlock {
>>   & cmd /C 'c:\temp\telegraf\telegraf.exe -once -console -config c:\temp\telegraf\telegraf_broker_connectivity_test.conf 2>&1' | select-string -SimpleMatch 'E! '
>> }






PS C:\>

From a remote machine: Run just the native command (no pipe to select-string) via Invoke-Command, then pipe the output of Invoke-Command to Select-String - get the expected output.

PS C:\> Invoke-Command -Credential $cred_svc -ComputerName my.remote.server.com -ScriptBlock {
>>   & cmd /C 'c:\temp\telegraf\telegraf.exe -once -console -config c:\temp\telegraf\telegraf_broker_connectivity_test.conf 2>&1'
>> } | select-string -SimpleMatch 'E!'

2021-06-09T13:06:35Z E! [inputs.x509_cert] Error in plugin: cannot get SSL cert
'tcp://[...]': dial tcp [...]: i/o timeout
2021-06-09T13:06:35Z E! [inputs.x509_cert] Error in plugin: cannot get SSL cert
'tcp://[...]': dial tcp [...]: i/o timeout
2021-06-09T13:06:35Z E! [telegraf] Error running agent: input plugins recorded 2 errors


PS C:\>

Both machines are Server 2016, running PowerShell 5.1

Why does #2 behave differently than #1 & #3?

Posted Jun 9, 2021, 1:23 PM UTC

31
purplemonkeymad•

The number of lines shows that the lines have been found. I think it's probably the modification of the type that happens when transfering of the data over the pssession connection.

I bet if you add | format-table to the end you will find out the objects /w all the properties are there, but are just not displaying properly.

2
[deleted]•

[deleted]

1