How to start remotely process in PowerShell
Asked Answered
P

3

7

I have a problem, I have a script which:

  • Connect with PSSession (I use PSSession with admin account)
  • Stop 2 process
  • Do change on them files
  • Start the 2 process (Problem here)

I want to start process on server, so i'm connect with PSSession (No problem)

I do Invoke-Command :

# $pathProg path to my program
Invoke-Command -session $mySession -command {Start-Process $($args[0])} -ArgumentList $pathProg

But it does nothing (I verify with VNC)

I do Invoke-Command too :

# $pathProg path to my program
Invoke-Command -session $mySession -command {&$($args[0])} -ArgumentList $pathProg

It lauch the program (Good) but my script wait the end program (Not good)

Anyone have an idea ?

Thanks

Pichardo answered 12/8, 2013 at 8:39 Comment(0)
C
12

You can try using WMI:

$command = "notepad.exe"
$process = [WMICLASS]"\\$CompName\ROOT\CIMV2:win32_process"
$result = $process.Create($command) 

If you need passing credentials:

$cred = get-credential
$process = get-wmiobject -query "SELECT * FROM Meta_Class WHERE __Class = 'Win32_Process'" -namespace "root\cimv2" -computername $CompName -credential $cred
$results = $process.Create( "notepad.exe" )
Confutation answered 12/8, 2013 at 9:15 Comment(3)
Thanks !!! +1 :D I think that is the "version bourrin" (in French ^^) but it works !Pichardo
Le cheval ou la femme facile? ;) C'est un plaisir aider!Confutation
But there's no GUI, how to display the GUI?December
F
0

$pathProg may be not be available within the script block which gets run eventually. You might want to pass it as an argument to the script block

Invoke-Command -session $mySession -command { param($progPath) ... } -argumentlist $progPath

Not that the outer -argumentlist, passes the arguments to the scriptblock.

Faraday answered 12/8, 2013 at 8:47 Comment(0)
E
0

Have you tried building the command as a string locally, then passing it to the Invoke-Command script as a ScriptBlock?

$remoteSession = New-PSSession -ComputerName 'MyServer'
$processName = 'MyProcess'

$command = 'Start-Service ' + $processName + ';'

Invoke-Command -Session      $remoteSession `
               -ScriptBlock  ([ScriptBlock]::create($command))

Remove-PSSession $remoteSession

If you want feedback from the remote server then you can get the output via Write-Output, like this:

$command = 'Start-Service ' + $processName + ' | Write-Output ;'
Evapotranspiration answered 12/8, 2013 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.