我认为您试图隐藏错误的窗口。我尝试添加了WindowTitle,但它具有不正确(不是预期的)的值。
我将代码更改为只响铃10次,然后停止,并添加了在此处找到的一些代码:https://stackoverflow.com/a/40354761/724039
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindows {
[DllImport("user32.dll")]
public static extern IntPtr GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count);
}
"@
$stringbuilder = New-Object System.Text.StringBuilder 256
$windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
$asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
$hwnd = (Get-Process -PID $pid).MainWindowHandle
$count = [UserWindows]::GetWindowText($hwnd, $stringbuilder, 256)
"The name of this window is: $($stringbuilder.ToString())"
if ($hwnd -ne [System.IntPtr]::Zero) {
$hidden = $asyncwindow::ShowWindowAsync($hwnd, 0)
}
$run = $true
$x=1
while ($x -le 10) {
[console]::beep(500,100)
Start-Sleep 1
$x++
}
当使用快捷方式启动以下内容时:pwsh.exe -File "d:\temp\beep1.ps1"
一个窗口会打开,上面显示文本The name of this window is:,我听到10声蜂鸣声,然后执行停止。
当在“Windows Powershell ISE”中执行时,该IDE的窗口会关闭。通过一些调试,我发现输出为“The name of this window is: Windows PowerShell ISE”。
我认为从获取WindowText的返回值应该类似于您的快捷方式标题。(但是,不幸的是,我无法解决此问题)