【问题标题】:Starting a .exe file on a remote computer using powershell使用 powershell 在远程计算机上启动 .exe 文件
【发布时间】:2017-05-21 20:33:12
【问题描述】:

大家好,我正在尝试在远程系统上启动一个 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 

#Starts Process# 最后运行没有错误,但远程机器上没有任何反应,终止进程工作正常。任何帮助将不胜感激。

【问题讨论】:

  • 在很多情况下,看起来代码在粘贴后可能已放在新行上。例如,在最后一行中,如果 $WBSTART 实际上位于单独的行上,那么这就是您的问题。此外,您似乎没有任何代码来停止进程或使用您创建的会话。有什么遗漏吗?
  • 对不起,我更新了代码
  • 是否有可能 $mycreds 用户无权访问 .exe 所在的路径?

标签: windows powershell powershell-remoting


【解决方案1】:

您创建了一个到远程计算机的 pssession,但没有使用它。您的 Start-Process 在托管脚本的计算机上运行。你需要使用Enter-PSSession,因为它:

启动与远程计算机的交互式会话。

进入会话后,您无需远程运行 WMI 命令。您可以像在服务器上直接运行一样运行它们。借鉴MSDN上的例子

第一个命令使用 Enter-PSSession cmdlet 启动与远程计算机 Server01 的交互式会话。会话开始时,命令提示符会更改为包含计算机名称。

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

第二个命令获取 Windows PowerShell 进程并将输出重定向到 Process.txt 文件。命令提交到远程计算机,文件保存在远程计算机上。

[Server01]: PS C:\> Get-Process Powershell > C:\ps-test\Process.txt

完成后别忘了致电Exit-PSSession


调用命令

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

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

如果您确实走这条路,请注意范围问题和look up how to pass arguments to the scriptblock

我不确定为什么你的不工作。也许你有一些错误被压制?我会尝试一些更简单的测试。您的应用程序是否基于 GUI?像这样运行时它可能不起作用。试试ipconfig 之类的方法,看看结果如何。


当您等待进程在远程机器上终止/启动时,我还会考虑一个带有超时条件的 while 循环。那就是你可以更好地解释失败,而不是假设 10 秒就足够了。

【讨论】:

  • 感谢您的帮助,我最终不得不使用 pssession 并调用命令来获得我想要完成的任务!感谢您的帮助!
【解决方案2】:

我认为您的本地/远程执行有问题。

(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 }

我还使用了-Waitswitch。开关强制Start-Process 等待程序启动(然后返回)。

在进行远程操作时,我总是建议通过Invoke-Command而不是通过几个命令的-ComputerName参数来执行这些操作。 Invoke-Command 通过 HTTP(S) 使用 WS-MAN,而提供 -ComputerName 参数的命令的远程实现在不同 cmdlet 的实现上有所不同。 invoke-command 的这一优势使其更易于配置,例如防火墙。

希望对您有所帮助。

【讨论】:

  • 感谢您的意见,您和@matt 都帮助了我!
【解决方案3】:

感谢您的所有帮助!所以这就是我最终完成我想做的事情,你觉得呢?

$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') ; $_ }
)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2018-08-13
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多