How to start remotely process in PowerShell

前提是你 提交于 2019-11-29 10:21:21

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" )

$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.

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 ;'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!