Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何测试powershell中的窗口是否远程打开_Powershell - Fatal编程技术网

如何测试powershell中的窗口是否远程打开

如何测试powershell中的窗口是否远程打开,powershell,Powershell,我试图解决的问题是一个过程,在这个例子中,acrobat被挂在后台。如果没有adobe的实际窗口,我想关闭它 此代码需要能够通过远程命令运行。我的代码在本地工作,但由于powershell检测windows的方式,它无法远程工作。有什么想法吗 function stop-adobe { param ([string]$computername) Invoke-Command $computername -scriptblock { #tests to see if

我试图解决的问题是一个过程,在这个例子中,acrobat被挂在后台。如果没有adobe的实际窗口,我想关闭它

此代码需要能够通过远程命令运行。我的代码在本地工作,但由于powershell检测windows的方式,它无法远程工作。有什么想法吗

function stop-adobe {
    param ([string]$computername)
    Invoke-Command $computername -scriptblock {
        #tests to see if the window is open and returns a value of true or false
        $test = Get-Process | where {$_.MainWindowTitle} | where {$_.ProcessName -like "acrobat"}

        If ($test) {
            msg * "Adobe is running perfectly, please inform the person you are on the phone with of this so that we can further troubleshoot"
        }
        else {
            #kills the process since it does not have an active window
            Stop-Process -name acrobat
        }
    }
}

你可以改变一下,试试看

 function stop-adobe {
param ([string]$computername)
Invoke-Command $computername -scriptblock {
    #tests to see if the window is open and returns a value of true or false
    $test = Get-Process | where {$_.MainWindowTitle} | where {$_.ProcessName -like "*acro*"}

    If ($test) {
        msg * "Adobe is running perfectly, please inform the person you are on the phone with of this so that we can further troubleshoot"
    }
    else {
        #kills the process since it does not have an active window
         Stop-Process -Id $test.Id
    }
}

}

创建计划任务,将其设置为仅在用户登录时运行(即交互);它应该能够运行这个窗口检测代码。让它把结论写在别处。然后,主脚本将创建该任务,启动它,等待它并检查输出。这听起来很复杂,但实际上远程PowerShell命令是在一个非交互式会话中运行的,与用户正在做的任何事情完全隔离,因此您需要在另一个会话中运行一些东西。您使用的是哪个版本的PowerShell
Get Process
有一个
-IncludeUserName
开关,尽管我不完全确定该开关是何时引入的。无论PS版本如何,您都可以使用WMI获取进程所有者。问题在于确定进程是否打开了一个窗口,而这只能通过在用户上下文中运行的代码来完成。我认为Jeroen Moster关于在非交互式会话中运行PowerShell的评论在这里很重要。在远程计算机上运行“Invoke命令”时,它不是以登录到该计算机的当前用户的身份运行的。它正在运行,就像powershell是由您在该计算机上的帐户启动的一样,这就是它无法运行的原因。可以按当前记录的方式运行的计划任务是一个解决方案,但可能不是一个好的解决方案,您需要在每台需要运行此脚本的计算机上设置它。好的,谢谢你的反馈,我想,比起在数千台计算机上运行psjob,询问他们是否打开然后杀死它更容易。这没有考虑导致程序无法运行的问题。它必须在用户登录计算机时运行,脚本本身不是问题所在。