无法使PSExec与Powershell一起使用

上周我开发了一个脚本来检查指定机器上是否启用了psremoting。 本周,我开始研究一个能够在指定机器上启用psremoting的脚本,但是我无法让psexec在powershell中运行(另外,是的,我知道psremoting可以通过组策略来启用)。 这是我的脚本:

$input = Read-Host @" Select Option (1)Manually enter computer(s) (2)Retrieve computer(s) from file Option "@ If ($input -eq 1){ $count = Read-Host "How many computers" $Computers = 1..$count $b=0;$c=1; ForEach ($Computer in $Computers) {$Computers[$b] = Read-Host "Computer" $c; $b++; $c++} } ElseIF ($input-eq 2) { $Computers = Read-Host "File" $Computers = Get-Content $Computers } Else { write-host "Invalid Option" Exit } cls $User = Read-Host "Enter username" $Pass = Read-Host "Enter password" cls $PSExec = "C:\Windows\System32\PSExec\PSExec.exe" ForEach ($Computer in $Computers){ # & $PSExec \\$Computer -u $User -p $Pass -h -c "C:\Temp\mybat.bat" & $PSExec \\$Computer -u $User -p $Pass "ipconfig" } 

执行脚本时出现以下错误:

PSExec.exe:在C:\ MyStuff \ EnablePSRemoting.ps1:34 char:1 +&$ PSExec $计算机-u $用户-p $传递“ipconfig”+ ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo:NotSpecified:(:String)[],RemoteException + FullyQualifiedErrorId: NativeCommandError

PsExec v2.11 – 远程执行进程Copyright(C)2001-2014 Mark Russinovich Sysinternals – www.sysinternals.com系统找不到指定的文件。

然后我试图直接从powershell运行PSExec,仍然没有运气。

Start-Process -Filepath "$PSExec" -ArgumentList "\\$computer -u $user -p $pass $command"正是我所需要的。

在脚本中,您需要在计算机名之前使用双反斜杠:
&\\ $ PSExec $计算机-u $用户-p $传递“ipconfig”

在直接在PowerShell中尝试的列表中,如果这是实际的密码,那么双美元符号被解释为最后一个命令的最后一个标记。