如何在PowerShell中启动远程进程?

时间:2021-07-11 12:15:47

I have a problem, I have a script which:

我有一个问题,我有一个脚本

  • Connect with PSSession (I use PSSession with admin account)
  • 连接PSSession(我使用PSSession和admin账户)
  • Stop 2 process
  • 停止2过程
  • 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)

我想在服务器上启动进程,所以我连接了PSSession(没有问题)

I do Invoke-Command :

我做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)

但它什么都不做(我用VNC验证)

I do Invoke-Command too :

我也做invoke命令:

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

谢谢

3 个解决方案

#1


11  

You can try using WMI:

你可以试试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" )

#2


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

$pathProg可能在最终运行的脚本块中不可用。您可能希望将其作为参数传递给脚本块。

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

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

不是外部参数列表,将参数传递给scriptblock。

#3


0  

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

您是否尝试将该命令作为本地字符串构建,然后将它作为ScriptBlock传递给invoke命令脚本?

$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 ;'

#1


11  

You can try using WMI:

你可以试试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" )

#2


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

$pathProg可能在最终运行的脚本块中不可用。您可能希望将其作为参数传递给脚本块。

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

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

不是外部参数列表,将参数传递给scriptblock。

#3


0  

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

您是否尝试将该命令作为本地字符串构建,然后将它作为ScriptBlock传递给invoke命令脚本?

$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 ;'