Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell and PSExec

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.

like image 940
alexander7567 Avatar asked Nov 17 '25 18:11

alexander7567


1 Answers

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.

like image 80
Luke Avatar answered Nov 19 '25 08:11

Luke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!