3

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.

alexander7567
  • 665
  • 13
  • 35
  • 1
    This is in PSEXEC and as far as turning it off I am not sure, but couldn't you do everything from powershell instead, that would get rid of the messages. – Luke Jun 18 '15 at 16:39
  • 3
    Recommend using PowerShell remoting instead of PSExec, like Luke suggested. – Benjamin Hubbard Jun 18 '15 at 16:42
  • @Luke My company turns powershell remote off. I have brought it up with them and they consider it a risk. – alexander7567 Jun 18 '15 at 16:46
  • @alexander7567 you're in an unenviable position, but the fact is they are on the wrong side of history. PowerShell remoting/WinRM is where it's at. As for your actual problem, I can't tell how you're calling `psexec`, but try adding `-i` to the command line. – briantist Jun 18 '15 at 17:26
  • @briantist I updated the original post with my code. I tried adding the -i, and got the same result. As far as WinRM I agree completely but I am not at the level to make them agree. I work for the company that has paid for SCCM for 2 years and still can't get it to work :) – alexander7567 Jun 18 '15 at 19:00
  • Screw the haters. PSExcec with powershell is a great combo if you don't want to get to every computer and enable PSRemoting... I'm not sure what your asking but if you just wanna run it silently redirect stdout and stderr to null – Cole9350 Jun 18 '15 at 21:06
  • Why are you trying to suppress all that text? are you trying to write a log file? – Luke Jun 19 '15 at 14:22
  • @Luke "It makes it very hard to see if there was a problem or if it worked with all of this text in the way." I just want to get rid of all the junk so I can only see the output of the actual command and not psexec. – alexander7567 Jun 19 '15 at 18:21

1 Answers1

1

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.

Luke
  • 647
  • 1
  • 8
  • 22
  • This looks great but does it still show the output of the actual net command – alexander7567 Jun 20 '15 at 03:37
  • It doesn't that is why you would need to add in your own instead. – Luke Jun 22 '15 at 13:15
  • @alexander7567 did this remedy your issue? – Luke Jun 29 '15 at 21:07
  • No it does not. Like I said, I would have no way to know if the command failed or not. That is the reason I want all the other text gone so I can see if the command spit out any errors between all that garbage. – alexander7567 Jun 30 '15 at 15:49
  • You can take the completion code that it returns and make your own output, if it has Error code 1 it failed, if it is 0 it succeed. – Luke Jun 30 '15 at 19:25