2017-03-03 28 views
1

這段代碼帶來了一個窗口焦點到前面。問題如果用戶點擊鼠標,我該如何保留它?有腳本運行時禁用鼠標點擊的方法嗎?如何讓焦點集中在一個窗口

param([string] $proc="C:\Program Files (x86)\Citrix\ICA Client\concentr.exe", [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); 
} 

回答

0

PS Community Extensions中有一個簡單的cmdlet可以做到這一點。 您可以使用它像這樣:

Set-ForegroundWindow (Get-Process PowerShell).MainWindowHandle 

在你的情況,

Set-ForegroundWindow $p.MainWindowHandle 

應該做你的工作,我相信。

希望它有幫助。

+0

謝謝,但這意味着我將不得不復制到整個企業的所有PC。 – user770022

+0

簡單而言,YES ...但如果您的桌面會話處於活動狀態,則可以將其與報告結合使用。 –

+0

根據您可以將模塊轉換爲base64的大小,將其添加到腳本中,然後在腳本開始時將其轉換回來,然後加載它,但是這是木馬的字面定義,並且某些病毒檢測系統也可能會以同樣的方式看到它,並簡單地刪除腳本所在的腳本。 – Random206

0

此塊禁用鼠標和鍵盤20秒,仍然允許我的腳本的其餘部分繼續。

Start-Job -ScriptBlock { 
$code = @" 
[DllImport("user32.dll")] 
public static extern bool BlockInput(bool fBlockIt); 
"@ 

$userInput = Add-Type -MemberDefinition $code -Name UserInput -Namespace UserInput -PassThru 

function Disable-UserInput($seconds) { 
$userInput::BlockInput($true) 
Start-Sleep $seconds 
$userInput::BlockInput($false) 
} 

Disable-UserInput -seconds 20 | Out-Null 
}  
相關問題