确定脚本是否隐藏运行 [英] Determine if script is running hidden

查看:66
本文介绍了确定脚本是否隐藏运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式确定 .ps1 脚本是否正在运行.如果它运行可见,它应该重新启动自己隐藏.如果它已经隐藏,则不执行任何操作.

I am trying to programmatically determine if a .ps1 script is running visibly or not. If it is running visibly, it should restart itself hidden. If it is already hidden, take no action.

我遇到的问题是一个循环,由于无法确定隐藏状态,因此它不断重新启动.

The problem I have is a loop where it continually restarts itself because hidden status cannot be determined.

我一直在查看 get-process cmdlet 和 GWMI Win32_process,但没有看到类似 .visible 属性来检查状态的内容.

I've been looking at both get-process cmdlet and GWMI Win32_process and see nothing like a .visible property to check status.

    If ($me -eq visible ???)
{
$Invisible = New-Object System.Diagnostics.ProcessStartInfo
$Invisible.FileName = "PowerShell.exe"
$Invisible.windowStyle ="Hidden"
$Invisible.arguments = "$myInvocation.MyCommand.Definition"
$Invisible.Verb = 'runas'
[System.Diagnostics.Process]::Start($Invisible)
}

知道如果 -eq 反对我可以使用哪个字段吗???

Any idea what field I can If -eq against ???

推荐答案

尝试使用 user32 函数 'IsWindowVisible'

Try using the user32 function 'IsWindowVisible'

If (-not ([System.Management.Automation.PSTypeName]'My_User32').Type) {
Add-Type -Language CSharp -TypeDefinition @"
    using System.Runtime.InteropServices;
    public class My_User32
    { 
        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(int hwnd);
    }
"@
}

$proc = Start-Process powershell.exe -WindowStyle Hidden -ArgumentList $myInvocation.MyCommand.Definition -Verb runas -PassThru
If ([My_User32]::IsWindowVisible($proc.MainWindowHandle)) {
    #Window is visible
}
Else {
    #Window is not visible
}

请注意,isWindowVisible"的返回值并非严格意义上的布尔值.它返回窗口的 WS_VISIBLE 样式位.因为 hidden 的值是零,而visible 的值是非零,所以它将作为一个布尔值工作.但是如果你想安全,你可以重写 If 语句来检查 -ne 0 以确定是否可见.

Note that the return value for 'isWindowVisible' isn't strictly a boolean. It returns the WS_VISIBLE style bit of a window. Because the value for hidden is zero and the value for visible is nonzero, it will work as a boolean. But if you want to be safe, you can rewrite the If statement to check for -ne 0 to determine if visible.

还要注意 $proc.MainWindowHandle 的使用.您不能使用 $proc.Handle,因为它不是父窗口的句柄.

Also note the use of $proc.MainWindowHandle. You can't use $proc.Handle, as that is not the handle of the parent window.

有关IsWindowVisible"函数的更多信息,请参阅 Microsoft 文档:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633530%28v=vs.85%29.aspx

For more information on the 'IsWindowVisible' function, see Microsoft documentation at:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633530%28v=vs.85%29.aspx

有关窗口样式的更多信息,请参阅 Microsoft 文档:
http://msdn.microsoft.com/en-us/library/czada357.aspx

For more information on window styles, see Microsoft documentation at:
http://msdn.microsoft.com/en-us/library/czada357.aspx

这篇关于确定脚本是否隐藏运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆