我是Powershell的新手,如果我的问题听起来很愚蠢,请原谅我。我从Yuri Posidelov找到以下脚本,我对该脚本进行了调整,以激活一个进程并显示窗口并发送击键以关闭该进程,效果很好。但是,如果有两个名称相同的进程会失败,任何人都可以帮我解决这个问题。
param([string] $proc="SBDDesktop", [string]$adm)
cls
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class WinAp {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
"@
$p = Get-Process |where {$_.mainWindowTItle }|where {$_.Name -like "$proc"}
if (($p -eq $null) -and ($adm -ne ""))
{
Start-Process "$proc" -Verb runAs
}
elseif (($p -eq $null) -and ($adm -eq ""))
{
Start-Process "$proc" #-Verb runAs
}
else
{
$h = $p.MainWindowHandle
[void] [WinAp]::SetForegroundWindow($h)
[void] [WinAp]::ShowWindow($h,3);
$wshell = New-Object -ComObject wscript.shell;
#$wshell.SendKeys('~')
$wshell.SendKeys('%fx')
sleep 1
$wshell.SendKeys('N')
}
#|format-table id,name,mainwindowtitle –AutoSize
# static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
# powershell.exe -windowstyle hidden -file *.ps1 -adm "a"
问题是,如果行$p = Get-Process |where {$_.mainWindowTItle }|where {$_.Name -like "$proc"}
有多个进程,它将创建一个类型为array
的对象。否则,它将创建System.ComponentModel.Component
类型的对象。您可以使用以下方法进行测试:
$p.GetType()
为了补偿这一点,即使该数组中只有一个元素,您也可以在数组中执行代码foreach
处理:
...
[array]$array = Get-Process |where {$_.mainWindowTItle }|where {$_.Name -like "$proc"}
foreach ($p in $array){
if (($p -eq $null) -and ($adm -ne ""))
{
Start-Process "$proc" -Verb runAs
}
elseif (($p -eq $null) -and ($adm -eq ""))
{
Start-Process "$proc" #-Verb runAs
}
else
{
$h = $p.MainWindowHandle
[void] [WinAp]::SetForegroundWindow($h)
[void] [WinAp]::ShowWindow($h,3);
$wshell = New-Object -ComObject wscript.shell;
#$wshell.SendKeys('~')
$wshell.SendKeys('%fx')
sleep 1
$wshell.SendKeys('N')
}
#|format-table id,name,mainwindowtitle –AutoSize
# static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
# powershell.exe -windowstyle hidden -file *.ps1 -adm "a"
}
[如果有多个窗口句柄,请执行循环:
$processList = [object[]]@( Get-Process |where {$_.mainWindowTItle }|where {$_.Name -like "$proc"} )
foreach( $p in $processList ) {
if (($p -eq $null) -and ($adm -ne "")) {
Start-Process "$proc" -Verb runAs
}
elseif (($p -eq $null) -and ($adm -eq "")) {
Start-Process "$proc" #-Verb runAs
}
else {
$h = $p.MainWindowHandle
[WinAp]::SetForegroundWindow($h) | Out-Null
[WinAp]::ShowWindow($h,3) | Out-Null
$wshell = New-Object -ComObject wscript.shell;
#[void]$wshell.SendKeys('~')
[void]$wshell.SendKeys('%fx')
sleep 1 | Out-Null
[void]$wshell.SendKeys('N')
}
}
Get-Process
将返回符合您条件的所有进程,这意味着它将把多个对象返回到变量$p
。
所以您需要遍历它们,您可以简单地使用foreach
循环
foreach($process in $p){
if (($process -eq $null) -and ($adm -ne ""))
{
Start-Process "$proc" -Verb runAs
}
elseif (($$process -eq $null) -and ($adm -eq ""))
{
Start-Process "$proc" #-Verb runAs
}
else
{
$h = $process.MainWindowHandle
[void] [WinAp]::SetForegroundWindow($h)
[void] [WinAp]::ShowWindow($h,3);
$wshell = New-Object -ComObject wscript.shell;
#$wshell.SendKeys('~')
$wshell.SendKeys('%fx')
sleep 1
$wshell.SendKeys('N')
}
}