我在下面的代码在Powershell ISE中运行时成功地清空了第二个/第三个监视器但是当我在控制台中运行它或者从计划任务中运行时它只会消隐掉屏幕的一半。在做了一些调查后,我注意到屏幕边界在控制台中的报告方式不同,从而导致了问题。有关如何解决此问题的任何想法,以便在控制台中运行时,附加显示器是否正确消隐?
Add-Type -AssemblyName System.Windows.Forms
$screens = [System.Windows.Forms.Screen]::Allscreens | Where-Object Primary -eq $false
$synchash.screens = New-Object System.Collections.Generic.List[System.Object]
foreach($screen in $screens){
$synchash.screens.Add($screen.devicename.replace('\','').replace('.','')[-1])
}
foreach ($screen in $screens){
$synchash."$($screen.devicename.replace('\','').replace('.',''))" = $screen
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$PowerShell = [PowerShell]::Create().AddScript({
function LoadXaml ($filename){
$XamlLoader=(New-Object System.Xml.XmlDocument)
$XamlLoader.Load($filename)
return $XamlLoader
}
$screenNum = $synchash.screens[0]
$display = "Display" + $screenNum
$synchash.screens.RemoveAt(0)
$XamlMainWindow = LoadXaml("\blankScreen.xaml")
$reader = (New-Object System.Xml.XmlNodeReader $XamlMainWindow)
$syncHash."Window$screenNum" = [Windows.Markup.XamlReader]::Load($reader)
[xml]$XAML = $XamlMainWindow
$XamlMainWindow.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object{
#Find all of the form types and add them as members to the synchash
$syncHash.Add($("" + $_.Name + $screenNum),$syncHash."Window$screenNum".FindName($_.Name) )
}
$syncHash."Window$screenNum".Top = $synchash.$display.bounds.Top;
$syncHash."Window$screenNum".Left = $synchash.$display.bounds.Left;
$syncHash."Window$screenNum".Width = $synchash.$display.bounds.Width;
$syncHash."Window$screenNum".Height = $synchash.$display.bounds.Height;
$synchash."Window$screenNum".Show()
$synchash."error$screenNum" = $error
})
$PowerShell.Runspace = $newRunspace
[void]$Jobs.Add((
[pscustomobject]@{
PowerShell = $PowerShell
Runspace = $PowerShell.BeginInvoke()
}
))
}