问题
I can display a simple Visual Basic inputbox from a PowerShell script like this:
$null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
$input = [microsoft.visualbasic.interaction]::inputbox($question, "bla", $text)
However, the inputbox window does not get focus which remains with the PowerShell window.
Is there a way to give focus to the inputbox window?
回答1:
You could focus the InputBox from a job.
For example:
$null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
$activateWindow = {
$null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
$isWindowFound = $false
while(-not $isWindowFound) {
try {
[microsoft.visualbasic.interaction]::AppActivate($args[0])
$isWindowFound = $true
}
catch {
sleep -Milliseconds 100
}
}
}
$job = Start-Job $activateWindow -ArgumentList "Unique Title"
$input = [microsoft.visualbasic.interaction]::inputbox("What is your answer?", "Unique Title", "none")
Remove-Job $job -Force
Write-Host $input -ForegroundColor Yellow
来源:https://stackoverflow.com/questions/9978727/focus-window-created-by-powershell-script