在 Powershell 中运行远程 GUI 应用程序

发布于 2024-09-16 04:26:49 字数 641 浏览 8 评论 0原文

我们有一个自定义组件,它封装了 powershell 的一些功能,因此可以在 BizTalk 2006 中使用。对于大多数操作(检查文件路径、复制或移动文件),这都可以正常工作。然而,我们需要远程启动 GUI 应用程序来进行一些处理。组件本身处理与远程盒子的连接,我们所要做的就是设置一些参数,然后告诉它执行命令

Start-Process -FilePath "path to exe" -ArgumentList "arguments for exe" -WorkingDirectory "workingdir for exe"

问题是这样的:如果我们从盒子本身的 powershell 命令行运行它,那么效果很好。然而,当我们远程启动它时(从 BizTalk、从测试工具,甚至使用远程 Powershell 命令行并通过 Start-PSSession 连接),该应用程序将短暂运行然后退出,而不实际执行任何操作。我怀疑,因为有问题的 exe 需要加载 GUI 才能运行该进程,所以正是这个导致了问题。我已经尝试了我能想到的一切,包括 -NoNewWindow 和 -WindowStyle 但无济于事。任何帮助使这项工作正常进行将不胜感激。

注意:我们无权访问我们尝试运行的应用程序的源代码,因为它是较旧的 win32 应用程序,并且没有提供该应用程序的批处理或命令行版本。

We have a custom comonent that wraps some of the functionality of powershell so it can be used frim BizTalk 2006. For most operations (checking a file path, copy or move a file) this works fine. However we have the need to fire up a GUI app remotely to do some processing. The component itself handles the connection to the remote box, all we have to do is set some parameters and then tell it to execute a command

Start-Process -FilePath "path to exe" -ArgumentList "arguments for exe" -WorkingDirectory "workingdir for exe"

The issue is this: If we run this from a powershell command line on the box itself, this works fine. However when we fire it up remotely (from BizTalk, from a test harness, even using a remote Powershell command line and connection via Start-PSSession), this app will run briefly then exit without actually doing anything. My suspicion is that because the exe in question requires a GUI to load to run the process, that it is this that is causing an issue. I've tried everything I can think of, including -NoNewWindow and -WindowStyle but to no avail. Any help getting this working would be very much appreciated.

Note: We do not have access to the source for the application we are trying to run as it is an older win32 application, and no batch or command line version of this application has been supplied.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光无声 2024-09-23 04:26:49

使用标准 PowerShell 方法(WinRM、WMI),您无法使用 GUI 启动应用程序。我知道的唯一解决方案是使用 SysInternals 的 PsExec (或类似的工具)。它可以启动向用户呈现 GUI 的应用程序。您的命令行将如下所示:

& ".\psexec" -accepteula -i "\\computername" -u "domain\username" -p "password" "command line"
  • -accepteula — 静默接受 EULA。
  • -i — 允许 GUI。

其他解决方案更加hacky,包括将任务远程添加到调度程序。

Using standard PowerShell methods (WinRM, WMI) you can't launch applications with GUI. The only solution I know about is to use PsExec from SysInternals (or similar tools). It can launch applications which present GUI to the user. Your command line will look like this:

& ".\psexec" -accepteula -i "\\computername" -u "domain\username" -p "password" "command line"
  • -accepteula — silently accept EULA.
  • -i — allow GUI.

Other solutions are more hacky, including remote adding of tasks to the scheduler.

不醒的梦 2024-09-23 04:26:49

由于我最近遇到了这个问题,这是我使用 Discord 添加远程任务的建议的解决方案。与必须设置单独的工具相比,我更喜欢“黑客”。

function Start-Process-Active
{
    param
    (
        [System.Management.Automation.Runspaces.PSSession]$Session,
        [string]$Executable,
        [string]$Argument,
        [string]$WorkingDirectory,
        [string]$UserID,
        [switch]$Verbose = $false

    )

    if (($Session -eq $null) -or ($Session.Availability -ne [System.Management.Automation.Runspaces.RunspaceAvailability]::Available))
    {
        $Session.Availability
        throw [System.Exception] "Session is not availabile"
    }

    Invoke-Command -Session $Session -ArgumentList $Executable,$Argument,$WorkingDirectory,$UserID -ScriptBlock {
        param($Executable, $Argument, $WorkingDirectory, $UserID)
        $action = New-ScheduledTaskAction -Execute $Executable -Argument $Argument -WorkingDirectory $WorkingDirectory
        $principal = New-ScheduledTaskPrincipal -userid $UserID
        $task = New-ScheduledTask -Action $action -Principal $principal
        $taskname = "_StartProcessActiveTask"
        try 
        {
            $registeredTask = Get-ScheduledTask $taskname -ErrorAction SilentlyContinue
        } 
        catch 
        {
            $registeredTask = $null
        }
        if ($registeredTask)
        {
            Unregister-ScheduledTask -InputObject $registeredTask -Confirm:$false
        }
        $registeredTask = Register-ScheduledTask $taskname -InputObject $task

        Start-ScheduledTask -InputObject $registeredTask

        Unregister-ScheduledTask -InputObject $registeredTask -Confirm:$false
    }

}

Since I came across this recently, here is my solution using Discord's suggestion of adding a remote task. I preferred the "hack" over having to setup a separate tool.

function Start-Process-Active
{
    param
    (
        [System.Management.Automation.Runspaces.PSSession]$Session,
        [string]$Executable,
        [string]$Argument,
        [string]$WorkingDirectory,
        [string]$UserID,
        [switch]$Verbose = $false

    )

    if (($Session -eq $null) -or ($Session.Availability -ne [System.Management.Automation.Runspaces.RunspaceAvailability]::Available))
    {
        $Session.Availability
        throw [System.Exception] "Session is not availabile"
    }

    Invoke-Command -Session $Session -ArgumentList $Executable,$Argument,$WorkingDirectory,$UserID -ScriptBlock {
        param($Executable, $Argument, $WorkingDirectory, $UserID)
        $action = New-ScheduledTaskAction -Execute $Executable -Argument $Argument -WorkingDirectory $WorkingDirectory
        $principal = New-ScheduledTaskPrincipal -userid $UserID
        $task = New-ScheduledTask -Action $action -Principal $principal
        $taskname = "_StartProcessActiveTask"
        try 
        {
            $registeredTask = Get-ScheduledTask $taskname -ErrorAction SilentlyContinue
        } 
        catch 
        {
            $registeredTask = $null
        }
        if ($registeredTask)
        {
            Unregister-ScheduledTask -InputObject $registeredTask -Confirm:$false
        }
        $registeredTask = Register-ScheduledTask $taskname -InputObject $task

        Start-ScheduledTask -InputObject $registeredTask

        Unregister-ScheduledTask -InputObject $registeredTask -Confirm:$false
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文