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

如何通过PowerShell在远程机器可视化运行Bat脚本?

远程执行Bat并显示可视化界面的解决方案

问题背景

你需要在远程机器上运行Bat文件并可视化查看执行过程,但当前使用Invoke-Command的方式仅能在本地控制台输出日志,远程机器无法显示Bat的运行窗口——这是因为Invoke-Command的远程会话属于无桌面的后台会话,不会关联到用户的交互式桌面环境。

现有代码

workflow RunScripts {
parallel {
InlineScript { 
    $User = "foo\boo"
    $PWord = ConvertTo-SecureString -String "koo" -AsPlainText -Force
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" -Force
    Invoke-Command -ComputerName "11.111.111.222" -ScriptBlock {cmd /c "\\11.111.111.222\c$\foo\boo\Node.bat"} -Credential $Credential -ErrorAction Stop
   } 
}
}
RunScripts 

可行修改方案

方案1:使用PsExec(简单高效)

PsExec是Sysinternals工具集的一员,专门用于在远程机器的交互式桌面启动程序。修改后的代码如下:

$User = "foo\boo"
$PWord = ConvertTo-SecureString -String "koo" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($User, $PWord)

# 确保PsExec.exe在本地可访问(可将其加入环境变量或指定完整路径)
& psexec.exe \\11.111.111.222 -u $User -p $PWord -i 1 -d cmd /c "C:\foo\boo\Node.bat"

参数说明:

  • -i 1:指定在会话ID为1的交互式桌面启动程序(若不确定会话ID,可在远程机器执行quser命令查看当前登录用户的会话ID)
  • -d:不等待程序执行完成直接返回(若需要等待执行结果可移除该参数)
  • 优先使用远程机器的本地路径C:\foo\boo\Node.bat,避免网络共享路径的权限问题

方案2:使用计划任务(无需额外工具)

通过在远程机器创建临时计划任务,强制任务在用户的交互式桌面执行Bat:

workflow RunScripts {
    parallel {
        InlineScript { 
            $User = "foo\boo"
            $PWord = ConvertTo-SecureString -String "koo" -AsPlainText -Force
            $Credential = New-Object System.Management.Automation.PSCredential ($User, $PWord)
            Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" -Force

            Invoke-Command -ComputerName "11.111.111.222" -Credential $Credential -ScriptBlock {
                $taskName = "TempRunBatTask"
                # 创建任务动作:执行Bat文件
                $action = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c C:\foo\boo\Node.bat"
                # 创建触发器:5秒后执行(避免注册后立即启动的时序问题)
                $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds(5)
                # 设置任务仅在用户登录时运行,关联交互式桌面
                $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -RunOnlyIfLoggedOn
                
                # 注册并启动任务
                Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -User $using:User -Password $using:PWord -Force | Out-Null
                Start-ScheduledTask -TaskName $taskName
                
                # 可选:等待执行后删除临时任务
                Start-Sleep -Seconds 10
                Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
            } -ErrorAction Stop
        } 
    }
}
RunScripts

方案3:使用WMI创建进程

通过WMI的Win32_Process.Create方法,指定窗口显示参数来启动Bat:

workflow RunScripts {
    parallel {
        InlineScript { 
            $User = "foo\boo"
            $PWord = ConvertTo-SecureString -String "koo" -AsPlainText -Force
            $Credential = New-Object System.Management.Automation.PSCredential ($User, $PWord)
            Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" -Force

            Invoke-Command -ComputerName "11.111.111.222" -Credential $Credential -ScriptBlock {
                $processClass = Get-WmiObject -Class Win32_Process -Namespace root\cimv2
                # 第四个参数1代表正常显示窗口
                $createResult = $processClass.Create("cmd /c C:\foo\boo\Node.bat", $null, $null, 1)
                if ($createResult.ReturnValue -ne 0) {
                    Write-Error "进程创建失败,错误码: $($createResult.ReturnValue)"
                }
            } -ErrorAction Stop
        } 
    }
}
RunScripts

关键注意事项

  • 远程机器必须有用户处于登录状态(即存在交互式桌面),否则无法显示窗口
  • 执行Bat时优先使用远程机器的本地路径,避免网络共享路径带来的权限验证问题
  • 使用PsExec时,确保远程机器开启了Admin$共享(默认开启),且防火墙允许相关通信

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 15:05:20