使用PowerShell在远程计算机上启动.exe文件

4

大家好,我想在远程系统上启动一个exe文件。我们有一个代理程序,用于连接我们的网站、数据库和打印机。它偶尔会挂起,需要我们远程登录或去工作站重新启动应用程序。我已经尝试了许多不同的解决方案,但都没有成功。

我能够结束正在运行的进程,但是它将无法重新打开exe文件以再次启动它。以下是目前的小脚本:

################################################
write-host "This will Restart Workbench helper please press the any key to  start"
pause
$mycreds = Get-Credential
$computer = Read-Host -prompt "Please Enter Computer Name"
$WBSTART1 = Read-Host -prompt "please enter command"
$pers = New-PSSession -ComputerName $computer -Credential $mycreds
# Gets Computer Name and Kills Workbench Helper #

(Get-WmiObject win32_process -ComputerName $computer -Credential $mycreds |  ?{ $_.ProcessName -match "Workbench3.helper" }).Terminate()

# Writes Host countdown to screen # 
write-host -ForegroundColor black -NoNewline "Stopping Process"
foreach ($element in 1..10)
{
Write-Host -NoNewline "${element} " -BackgroundColor 'white' - ForegroundColor 'black'
Start-Sleep -Seconds 1
}
Write-Host ''
Write-host "Process Complete"
Start-Process -filepath "C:\Program Files (x86)\Company Security\Workbench3 Helper\WorkBench3.Helper.exe"

# Writes Host countdown to termination Screen # 
write-host -ForegroundColor black -NoNewline "Starting Workbench Process"
foreach ($element in 1..10)
{
Write-Host -NoNewline "${element} " -BackgroundColor 'white' -ForegroundColor 'black'
Start-Sleep -Seconds 1
}
Write-Host ''
Write-host "Process Complete"


# Starts Process #
$WBSTART = {start-process -filepath 'C:\Program Files (x86)\FrontPoint  Security\Workbench3 Helper\WorkBench3.Helper.exe'}
Invoke-Command –ComputerName $computer -Credential $mycreds -ScriptBlock $WBSTART 

#启动进程#在结束时没有错误,但远程机器上没有任何反应,杀死进程可以正常工作。如有帮助将不胜感激。


有很多情况看起来像是在粘贴后代码被放到了新的一行。例如,在最后一行,如果$WBSTART实际上是单独一行,那么这就是你的问题所在。此外,似乎你没有任何代码来停止进程或使用你创建的会话。是否有遗漏的部分? - Patrick Meinecke
抱歉,我更新了代码。 - nathan Delsignore
$mycreds用户无法访问.exe所在的路径,这种情况可能发生吗? - Michael Timmerman
3个回答

4
您创建了一个到远程计算机的PSSession,但是没有使用它。您的Start-Process是在托管脚本的计算机上运行的。您需要使用Enter-PSSession,因为它可以:开始与远程计算机的交互式会话。一旦您进入会话,则无需远程运行WMI命令。您可以像直接在服务器上运行一样运行它们。借鉴MSDN上的示例。

The first command uses the Enter-PSSession cmdlet to start an interactive session with Server01, a remote computer. When the session starts, the command prompt changes to include the computer name.

PS C:\> Enter-PSSession -Computer Server01
[Server01]: PS C:\>

The second command gets the Windows PowerShell process and redirects the output to the Process.txt file. The command is submitted to the remote computer, and the file is saved on the remote computer.

[Server01]: PS C:\> Get-Process Powershell > C:\ps-test\Process.txt
不要忘记在完成时调用Exit-PSSession

Invoke-Command

如果您不想使用会话,则也可以在远程计算机上使用 Invoke-Command 。 您已经尝试在最后几行代码中执行此操作。

Invoke-Command -ComputerName $computer -ScriptBlock {Start-Process "bagel.exe"}

如果你选择这条路,请注意作用域问题和如何传递参数给脚本块
我不确定为什么你的代码没有起作用。也许有一些错误被压制了?我建议你尝试一些更简单的测试。你的应用程序是基于GUI的吗?这种方式可能行不通。尝试使用ipconfig,看看结果如何。
在等待远程机器上的进程终止/启动时,我建议您考虑使用带有超时条件的 while 循环。这样可以更好地处理失败情况,而不是假设 10 秒就足够了。

感谢您的帮助,最终我不得不使用but pssession和invoke命令来实现我想要完成的任务!感谢您的帮助! - nathan Delsignore

1

我认为你的本地/远程执行存在问题。

(Get-WmiObject win32_process -ComputerName $computer -Credential $mycreds | ?{ $_.ProcessName -match "Workbench3.helper" }).Terminate()

上面这行代码会将远程计算机中的所有进程返回到本地计算机。然后你在本地机器上过滤返回的进程对象,并调用 Terminate() 方法。我建议你使用以下代码:

Invoke-command -session $pers -ScriptBlock { (Get-WmiObject win32_process -ComputerName $computer -Credential $mycreds | ?{ $_.ProcessName -like"*Workbench3.helper*" }).Terminate()}

这应该会终止 Workbench3 进程。我使用了 like 运算符,使过滤更加宽松。

之后,你可以重复使用存储在 $pers 中的会话来重新启动进程。

Invoke-command -session $pers -scriptblock {Start-Process -filepath "C:\Program Files (x86)\Company Security\Workbench3 Helper\WorkBench3.Helper.exe" -Wait }

我还使用了-Wait开关。该开关强制Start-Process等待程序启动(然后返回)。
在执行远程操作时,建议始终通过Invoke-Command执行这些操作,而不是通过各种命令的-ComputerName参数执行。 Invoke-Command通过HTTP(S)使用WS-MAN,而提供-ComputerName参数的命令的远程实现因cmdlet而异。 invoke-command的这个优点使得配置例如防火墙更容易。
希望有所帮助。

感谢您的回答,您和@matt都帮了我很多! - nathan Delsignore

0

感谢您的所有帮助!这就是我最终用来完成我想做的事情的方法,您觉得怎么样?

$mycreds = Get-Credential
$computer = Read-Host -prompt "Please Enter Computer Name"
$s = New-PSSession -ComputerName $computer -Credential $mycreds

 #### Kills Workbench Helper Proccess ####
Invoke-command -ScriptBlock { (Get-WmiObject win32_process -ComputerName $computer -Credential $mycreds |  ?{ $_.ProcessName -like"*Workbench3.helper*" }).Terminate()}

################ Starting Workbench Helper #####################
Invoke-Command $s {$run = (Get-Date).AddMinutes(1) Register-ScheduledTask -TaskName "ZZ_Start WorkBench Helper"  -User "domain\user" -InputObject (
 (
New-ScheduledTask -Action (
  New-ScheduledTaskAction -Execute "C:\Program Files (x86)\Company Security\Workbench3 Helper\WorkBench3.Helper.exe" -Argument (
    " "
  )
) -Trigger (
  New-ScheduledTaskTrigger -Once -At ($run.TimeOfDay.ToString("hh\:mm")) # As a "TimeOfDay" to get 24Hr format
) -Settings (
  New-ScheduledTaskSettingsSet  -DeleteExpiredTaskAfter 00:00:01 #
) 
) | %{ $_.Triggers[0].EndBoundary = $run.AddMinutes(1).ToString('s') ; $_ }
)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接