我正在尝试编写一个 PowerShell 脚本来调用在后台运行的波形声音文件,同时将焦点更改回原始窗口。但我的脚本没有将焦点更改回主窗口。有人能帮我吗?我的脚本如下所示:
Add-Type -AssemblyName presentationCore
$wsh = New-Object -ComObject Wscript.Shell
$mediaPlayer = New-Object System.Media.SoundPlayer
$mediaPlayer.SoundLocation='C:\temp\ring\2Ring.wav'
$mediaPlayer.PlaySync()
Start-Sleep -Seconds 5
$wsh.Sendkeys("%{TAB}")
答案1
谢谢你给我指明了正确的方向。我的最终代码如下:
Add-Type -AssemblyName presentationCore
[void][System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[Microsoft.VisualBasic.Interaction]::AppActivate((Get-Process -Name notep*').MainWindowTitle)
$mediaPlayer = New-Object System.Media.SoundPlayer
$mediaPlayer.SoundLocation='C:\temp\ring\2Ring.wav'
$mediaPlayer.PlaySync()