psexec仅在使用用户名和密码并从PowerShell脚本调用时挂起

我正在尝试使用psexec在已打开的交互式会话中启动进程。 我的机器和远程机器都是Windows 7.使用命令提示符内的命令可以正常工作,可执行文件已启动,并在GUI中可见。

psexec64 -accepteula -nobanner \\hostname -u domain\user -p password -h -i 2 -d C:\workingdir\subfolder\application.exe 

但是,当我尝试在PowerShell脚本中执行相同的调用时,它不起作用。 查看完整脚本的上下文。

 # invalid args: $psExecArgs = "-accepteula", "-nobanner", "-u domain`\${username}", "-p ${password}", "-h", "-i ${sessionId}", "-d", "`\`\${hostname}", "`"${executableDirectory}`\${executableName}`"" # hangs: $psExecArgs = "-accepteula", "-nobanner", "-u domain`\${username}", "-p ${password}", "-h", "-d", "`\`\${hostname}", "`"${executableDirectory}`\${executableName}`"" $psExecArgs = "-accepteula", "-nobanner", "-h", "-d", "`\`\${hostname}", "`"${executableDirectory}`\${executableName}`"" & $EchoArgsPath $psExecArgs $result = & $PsExecPath $psExecArgs #2>$null 
  • 当给出所有相同的参数时, $result的值是psexec的帮助,就像给出了一个无效的参数一样。
  • -i 2被删除时,psexec显示在我的机器上正在运行的进程列表中,并且似乎挂起; 在远程机器上没有启动psexecsvc进程。
  • -u-p被删除时,psexec被启动,但是正如预期的那样,这个过程在远程机器上的我的帐户的非交互式会话中启动。

我使用pslist和pskill时看到了同样的行为,所以我用tasklist和taskkillreplace了这些行为。 我已确保UAC被禁用,并且可以使用我提供的凭据映射到远程计算机上的Admin $共享。

input这个问题时,我发现一个问题, 提示stdin可能是一个问题,但我不知道这是否与我的情况相关。

声明一个像$psExecArgs = "-accepteula", "-nobanner"这样的variables来创build一个string数组。

你正在运行任何string说(使用& )而不是启动一个进程和提供参数,所以参数也需要是简单的string(不是一个string数组)。

然后进一步泥泞它,如果你提供的参数在一个单一的string( $psExecArgs = "-accepteula -nobanner" ),实际上只是作为一个参数PSExec出现,也不会工作。

所以,与其试图运行string,一个选项是使用PowerShell的Start-Process命令,这个命令需要将参数传送到数组中(就像你现在正在做的那样)。

尝试更改命令行,如:

& $PsExecPath $psExecArgs

像这样的东西:

Start-Process $PsExecPath $psExecArgs

您将不得不处理返回代码和错误有点不同,如:

 try { $process = (Start-Process -FilePath $psexec -ArgumentList $psExecArgs -NoNewWindow -PassThru) } catch { Write-Output "Error launching process!" Write-Output $_.Exception.Message $process = $null } finally { if ($process) { $process.WaitForExit() Write-Host "Process exit code: " $process.ExitCode } } 

debugging总是你的朋友 – 如果你在执行过程中检查$ psExecArgs的值(在完整的脚本中),你会注意到你没有提供$ password的值。 这意味着您正在向psexec发送-p(无),这又使得psexec在等待密码input时挂起。

 C:\..\foobar.ps1 cmdlet foobar.ps1 at command pipeline position 1 Supply values for the following parameters: hostname: myhostname username: myusername password: mypassword sourcedirectory: c:\mysourcedir targetdirectory: c:\mytargetdir executablename: myexec.exe Hit Line breakpoint on 'C:\...\foobar.ps1:34' [DBG]: PS C:\Users\Zerqent>> $psExecArgs -accepteula -nobanner \\myhostname -u domain\myusername -p qwinsta 

为了避免在PowerShell中,使参数强制https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_functions_advanced_pa​​rameters

更新:您还应该将用户名和密码部分分开列表中单独的条目。 基本上每次在cmd-command中都有一个空格时,就会创build一个单独的列表条目。 所以你的$ psexecArgs会变成:

 $psExecArgs = "-accepteula", "-nobanner", "-u", "domain`\${username}", "-p", "${password}", "-h", "-d", "`\`\${hostname}", "`"${executableDirectory}`\${executableName}`"" 

不太确定该redirect是否可行,(#2> $ null),还是应该以某种方式包含在列表中。