Powershell and PSExec

1k views Asked by At

I find it that I am using psexec all the time at my job to remotely make changes in mass. When I run PSEXEC in powershell, I get the following message on every computer that I connect to:

Code:

foreach($computer in $computers) {
        psexec -i \\$computer net localgroup "Administrators" "ITLOCALADMIN" /add
}

And here is the results:

PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
System error 1378 has occurred.
The specified account name is already a member of the group.
Connecting to {COMPNAME}...Starting PSEXESVC service on {COMPNAME}...Connecting with PsExec service on {COMPNAME}...Starting net on {COMPNAME}...
net exited on {COMPNAME}with error code 2.
psexec : 
At G:\Users\ariggs\Documents\WindowsPowerShell\Scripts\AddItlocaladmin.ps1:3 char:9
+         psexec \\$computer net localgroup "Administrators" "ITLOCALAD ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Is it possible to turn those messages off where all I see is the output of the command or an error? It makes it very hard to see if there was a problem or if it worked with all of this text in the way.

1

There are 1 answers

5
Luke On

Alright I have came up with an answer, so what you will want to do is add a null output to the end so your line of code will look like this,

psexec -i \\$Computer net localgroup "Administrators" "ITLOCALADMIN" /add > $null 2>&1

now this will redirect all the output away, now you will also want to add some sort of logging so I would suggest dropping it into either a Try, Catch or just add a line at the end of your foreach that will write to you that the computer is done,

Write-host "Computer $Computer is now done" 

now then you will need to put a lot more faith in your script this way but it is an option.