You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell远程在服务器上启动应用程序?附脚本示例

远程通过PowerShell启动带界面应用程序的处理方案

尝试通过PowerShell在远程服务器上启动应用程序,原使用脚本如下:

$Username = 'administrator'
$Password = 'Password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName "IP" -ScriptBlock {Start-Process -FilePath `"C:\Windows\notepad.exe" `} -credential $Cred

问题说明

原脚本执行后,程序会运行在远程服务器的非交互式后台会话中,本地登录的用户无法看到程序界面。

解决脚本

要让程序在用户的交互式桌面会话中启动,可使用以下修改后的脚本:

$Username = 'administrator'
$Password = 'Password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

Invoke-Command -ComputerName "IP" -ScriptBlock {
    # 获取当前活跃的交互式登录会话ID
    $activeSession = Get-CimInstance Win32_LogonSession | Where-Object { $_.LogonType -eq 2 } | Select-Object -First 1
    if ($activeSession) {
        # 指定会话ID创建进程,确保程序出现在用户桌面
        $processParams = @{
            ClassName = 'Win32_Process'
            MethodName = 'Create'
            Arguments = @{
                CommandLine = 'C:\Windows\notepad.exe'
                SessionId = $activeSession.SessionId
            }
        }
        Invoke-CimMethod @processParams
    } else {
        Write-Host "未检测到活跃的交互式登录会话"
    }
} -Credential $Cred

关键逻辑

  1. 通过Win32_LogonSession筛选出类型为2的会话(交互式登录会话,即用户当前登录的桌面会话)
  2. 使用Win32_Process.Create方法指定会话ID启动程序,确保程序运行在用户可见的桌面环境中

内容的提问来源于stack exchange,提问作者asf asf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 09:20:26