Author Posts

March 19, 2018 at 3:02 pm

Using the below code provided by Will on here when I run it from the ISE I get one set of valus for the screen.bounds but when running from the console or via scheduled task I get a different set of values. The one I get from the ISE are correct and the form fills the second monitor.. Any ideas why I get different values ?

#region screenblocker
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()
}
))
}
#endregion screenblocker

March 19, 2018 at 4:52 pm

A further bit of investigating

If I run [System.Windows.Forms.Screen]::Allscreens in the ISE it returns details on both my primary and secondary monitors, however if I run it in the console it only returns details of my primary monitor. How can I get details of the secondary monitor in the console ?