运行远程批处理文件时,Powershell进程“挂起”

时间:2013-12-03 12:23:56

标签: powershell

我正在尝试通过驱动器映射在远程服务器上运行批处理文件,如下所示,但该过程挂起...

Enable-WSManCredSSP -Role Client -DelegateComputer someserver -Force

$credential = Get-Credential -Credential domain\user (then I supply the password in the popup)

$RemoteSession = New-PSSession -ComputerName someserver -Credential $credential -Authentication Credssp

Invoke-Command -Session $RemoteSession -ScriptBlock { Enable-WSManCredSSP -Role Server -Force }

Invoke-Command -Session $RemoteSession -ScriptBlock { New-PSDrive -Name I -PSProvider FileSystem -Root \\server\share$ }

到目前为止,一切似乎都很好,我可以“开车”我开车,看看预期的内容。

执行以下操作时,进程挂起 -

Invoke-Command -Session $RemoteSession -ScriptBlock { Start-Process I:\temp.bat }

temp.bat文件执行以下命令,我手动验证它是否正常工作

echo Scott was here > C:\temp.txt

然而,该命令运行超过5分钟而没有任何响应。

有人可以帮忙吗?我做错了什么?

4 个答案:

答案 0 :(得分:0)

虽然这不是你问题的直接解决方案,但我几周来对着键盘敲击我的建议就是采取不同的方法。

$taskName = whocares
$command = I:\Temp.bat
$serverName = theServer
$userName = domain\user
$pass = pl4inT3xtPassword
$date = (Get-Date).addMinutes(1)
$formats = $date.getDateTimeFormats()
$startTime = $formats[113]
schtasks.exe /create /sc ONCE /tn $taskName /tr $command /s $serverName /u $userName /p $pass /ru $userName /rp $pass /st $startTime

这将创建一个任务,在下一分钟启动该进程,然后从调度程序中删除自己。在运行后还有一个/ z标志可以删除,但是如果你喜欢我和XP,那么这可能是你无法使用的。如果你注意到你的任务在运行后没有被删除,你可以等待一分钟然后杀死它......或者你可以对它有点看上去:

while(($timer -lt 60) -and (!$running)) {
    schtasks.exe /query /S $serverName /FO CSV /v /u $userName /p $pass | ConvertFrom-CSV | `
    % {if($_.Status -eq "Running") {
        Write-Host "IT WORKED!"
        Write-host $_
        $running = $true
      }
    }
    if(!$running) {
        $timer++
    }
    sleep 1
}
schtasks.exe /delete /s $serverName /u $userName /p $pass /tn $taskName /f

这一位只是每秒查询一次任务,检查它是否正在运行......一旦它将删除旧的计划任务,你的进程将继续运行(直到完成)。

任务调度程序是我发现使用PowerShell远程启动进程的唯一方法。它不是PSremote.exe的子进程,而是在System下。此外,与远程会话不同,如果您从PowerShell会话中退出X,则该过程将在远程计算机上保留。

我知道它不仅仅是你的一个衬垫,但我希望它很有用,如果你参数化它甚至在顶部构建一个Windows窗体应用程序,它将为你节省大量时间和头痛。

更多关于schtasks Here

答案 1 :(得分:0)

感谢您的建议。

由于其他问题(我们最终试图运行的过程),我们已经把它放在了一分钟,没有找到解决方案

感谢您的帮助和建议。希望能在几个星期内回复这个问题 斯科特

答案 2 :(得分:0)

尝试将-AsJob放在Invoke-Command行的末尾?

答案 3 :(得分:0)

作为仅供参考,如果Invoke-Command 始终 挂起,则可能是服务或防火墙:

  1. 尝试一个简单的命令到系统: Invoke-Command -ComputerName XXXXX -ScriptBlock { Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion }

  2. 启动Windows远程管理服务(在该系统上)

  3. 检查侦听端口:

     netstat -aon | findstr "5985"
          TCP    0.0.0.0:5985           0.0.0.0:0              LISTENING       4
          TCP    [::]:5985              [::]:0                 LISTENING       4