简体   繁体   中英

Starting a .exe file on a remote computer using powershell

Hey everyone I'm attempting to start a exe file on remote systems, we have an agent that runs to connect our website to our database and printers. It hangs occasionally, and requires us to remote in and/or go to the station and restart the application. I've attempted many different solutions on here and none seem to work.

I'm able to kill the process that is running but then it will not reopen the exe file to start it again. Here is the small script so far

################################################
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# at the end runs with no errors, but nothing happens on the remote machine, killing the process works fine. Any help would be greatly appreciated.

You created a pssession to the remote computer but didn't use it . Your Start-Process was run on the computer hosting the script. You need to use Enter-PSSession since it:

Starts an interactive session with a remote computer.

Once you are in a session then you do not need to run the WMI commands remotely. You can run them like you were running on the server directly. Borrowing from the example on 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 

Don't forget to call Exit-PSSession when you are done.


Invoke-Command

If you didn't want to use pssessions then you could also you Invoke-Command on the remote machine as well. You already are trying to do this with your last lines of code.

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

If you do go down this path be aware of scoping issues and look up how to pass arguments to the scriptblock .

I am not sure why yours is not working. Perhaps you have some errors being suppressed? I would try something simpler for testing. Is your application GUI based? It might not work when run like this. Try something like ipconfig and see what the results are.


I would also consider a while loop with a timeout condition while you are waiting for the process to terminate/start on the remote machine. That was you can account for failures better instead of assuming that 10 seconds is enough.

I think you've a problem with your local/remote execution.

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

Above line returns all processes from the remote computer to your local computer. Than you're fitering the returned process-objects on your LOCAL machine, and call Terminate() on your LOCAL machine. I would suggest you use the following line instead:

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

This should kill the Workbench3 process(es). I used the like operator to make filtering less strict.

After that, you can reuse the session stored in $pers to start the process again.

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

I additionally used the -Wait switch. The switch forces Start-Process to wait for the programm to be started (and return afterwards).

When doing remote operations I would always suggest to perform these operations via Invoke-Command and not via the -ComputerName parameters of the several commands. Invoke-Command uses WS-MAN via HTTP(S), while the remote-implemenation of commands offering the -ComputerName parameter differ in implementation from cmdlet to cmdlet. This advantage of invoke-command makes it easier to configure eg firewalls.

Hope that helps.

Thanks for all your help! So this is what I ended up with to accomplish what I was trying to do, what do you think?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM