Author Posts

October 10, 2017 at 3:24 pm

Invoke-Command -ComputerName (gc C:\scripts\computers.txt) -scriptblock {get-hotfix | select-string 4038799}

When I run this against my list of servers I see the shell start moving like it's outputting something, but nothing comes up. No errors either. I can manually enter a PS session to any one of the servers, run the command and it outputs that the hotfix is there.

I can run this command and everything shows up fine.

Invoke-Command -ComputerName (gc C:\scripts\computers.txt) -scriptblock {net localgroup administrators}

Has anyone else seen this? Is there another way I should be handling this?

October 10, 2017 at 3:28 pm

Try connecting to the same machine using Enter-PSSession and running the same Get-Hotfix command. Do you get anything then?

October 10, 2017 at 3:52 pm

Yep that works fine

October 10, 2017 at 4:08 pm

OK, so the command you're sending works. So the only difference between your Invoke-Command and your Enter-PSSession is that the former is targeting multiple computers, whereas the latter is targeting only one computer, right? Could we test the Invoke-Command with just one computer – the same one you used for Enter-PSSession?

October 10, 2017 at 4:20 pm

Yep, tried that as well (Sorry, I should have listed all the troubleshooting I did!) and it doesn't work. Gives me the same blank output.

Should I be doing some type of for each on enter-pssession?

October 11, 2017 at 9:36 pm

That's pretty interesting. I was able to duplicate that error and it seems to be something with select-string. As an aside you could try this command if you're just trying to find out if a hotfix is installed. The output is a little different than your first command but the logic is essentially the same

Invoke-Command -ComputerName (gc C:\scripts\computers.txt) -scriptblock {get-hotfix -id kb4038799}

Now what's weird about the select-string thing is that if you add a format-list * to your command you get the output. Likewise if you pipe it to get-member. It appears to be just an output view thing, so this works too:

Invoke-Command -ComputerName (gc C:\scripts\computers.txt) -scriptblock {get-hotfix | select-string 4038799 | format-list *}

Edit:
Found a good explanation here: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/f5697b54-6256-4489-80e1-18b1a7d3cb56/selectstring-in-invokecommand-not-working-as-expected?forum=ITCG

The issue is caused by the '$matches' collection. It does not seem portable across sessions. This is because it is an array of pointers into some objects stashed in memory. When returned to a remote session it points to nothing for some reason so we get a list of null or zeros which show up as line feeds.

Adding the select forces a simle object to be created containing the string. This can be ported to a remote session.

So in addition to format-list * you could use select line or select anything really. That's an interesting "feature"

October 13, 2017 at 12:26 pm

Thanks Jeremy, never thought to do it that way!

October 13, 2017 at 10:38 pm

This is more for learning – am I missing something?

I don't see a ToString() in Get-HotFix and that's what help for Select-String says for Input it expects, hence it returns nothing.