powershell - 如何在 PowerShell 中启动远程进程

标签 powershell remoting invoke-command start-process

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

  • 与 PSSession 连接(我使用 PSSession 管理员帐户)
  • 停止 2 进程
  • 更改他们的文件
  • 启动 2 进程(问题在这里)

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

    我做调用命令:
    # $pathProg path to my program
    Invoke-Command -session $mySession -command {Start-Process $($args[0])} -ArgumentList $pathProg
    

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

    我也做调用命令:
    # $pathProg path to my program
    Invoke-Command -session $mySession -command {&$($args[0])} -ArgumentList $pathProg
    

    它启动程序(好)但我的脚本等待结束程序(不好)

    有人有想法吗?

    谢谢

    最佳答案

    您可以尝试使用 WMI:

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

    如果您需要传递凭据:
    $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" )
    

    关于powershell - 如何在 PowerShell 中启动远程进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18182690/

    相关文章:

    ios - 远程构建 iOS 应用程序

    通过 HTTPS 的 Powershell Invoke-RestMethod

    arrays - 使用 Invoke-Command 将数组传递给另一个脚本

    使用 PowerShell 进行 MySQL 转储

    arrays - 获取数组的所有组合

    powershell - IndexOf 找到一个字符串,但 -match 没有

    powershell - 使用 Powershell Limit-Eventlog 设置 Windows 日志最大大小

    Powershell远程处理-无法以其他用户身份执行exe

    .NET远程速度和VPN

    xml - 使用 invoke-command 时如何将 xml 元素传递给 scriptBlock