简体   繁体   English

Powershell 和 PSExec

[英]Powershell and PSExec

I find it that I am using psexec all the time at my job to remotely make changes in mass.我发现我在工作中一直使用 psexec 远程进行大规模更改。 When I run PSEXEC in powershell, I get the following message on every computer that I connect to:当我在 powershell 中运行 PSEXEC 时,我在连接到的每台计算机上都收到以下消息:

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.很难看出是否存在问题,或者它是否以这种方式处理所有这些文本。

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,现在这将重定向所有输出,现在您还需要添加某种日志记录,因此我建议将其放入 Try、Catch 或仅在您的 foreach 末尾添加一行,该行会写信给您电脑完成,

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.现在,您将需要以这种方式更加信任您的脚本,但这是一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM