Estou usando a função de Janela de configuração tamanho e posição no PowerShell 5 e 6 , o que funciona de forma fantástica para apenas um aplicativo, mas eu preciso ter várias janelas do mesmo processo abertas e cada vez que eu as chamo, eu as defino em um determinado local. Em vez de todos eles indo para um local especificado.
RESOLVIDO:
Finalmente determinou como fazê-lo funcionar em um servidor. Adicionado o parâmetro $ ProcessTitle, bem como $ UserSessionID, que permitiu que ele funcionasse corretamente, mas às vezes não teria o $ ProcessID tão rápido quanto tentava mover a janela para a posição e errar. Para acomodar esse problema, adicionei um Start-Sleep enquanto $ ProcessID é nulo e reconfigure-o de volta para $ null após o comando ser executado.
While ($ProcessID -eq $Null){
$ProcessID = (Get-Process -name $ProcessName | Where-Object -property SI -eq $UserSessionID |where {$_.MainWindowTitle -eq $ProcessTitle}).Id
Start-Sleep -Milliseconds 50}
Script de função:
Function Set-Window {
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param (
[parameter(ValueFromPipelineByPropertyName=$True)]
$ProcessName,
[int]$UserSessionID,
[string]$ProcessTitle,
[int]$X,
[int]$Y,
[int]$Width,
[int]$Height,
[switch]$Passthru
)
Begin {
Try{
[void][Window]
} Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
}
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
}
}
Process {
$Rectangle = New-Object RECT
While ($ProcessID -eq $Null){
$ProcessID = (Get-Process -name $ProcessName | Where-Object -property SI -eq $UserSessionID |where {$_.MainWindowTitle -eq $ProcessTitle}).Id
Start-Sleep -Milliseconds 50}
$Handle = (Get-Process -id $ProcessID).MainWindowHandle
#$Handles = (Get-Process -Name $ProcessName).MainWindowHandle
#foreach ( $Handle in $Handles ) {
if ( $Handle -eq [System.IntPtr]::Zero ) { Continue }
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If (-NOT $PSBoundParameters.ContainsKey('Width')) {
$Width = $Rectangle.Right - $Rectangle.Left
}
If (-NOT $PSBoundParameters.ContainsKey('Height')) {
$Height = $Rectangle.Bottom - $Rectangle.Top
}
If ($Return) {
$Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
}
If ($PSBoundParameters.ContainsKey('Passthru')) {
$Rectangle = New-Object RECT
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If ($Return) {
$Height = $Rectangle.Bottom - $Rectangle.Top
$Width = $Rectangle.Right - $Rectangle.Left
$Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
$TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left, $Rectangle.Top
$BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
If ($Rectangle.Top -lt 0 -AND $Rectangle.LEft -lt 0) {
Write-Warning "Window is minimized! Coordinates will not be accurate."
}
$Object = [pscustomobject]@{
ProcessName = $ProcessName
Size = $Size
TopLeft = $TopLeft
BottomRight = $BottomRight
}
$Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
$Object
}
}
$ProcessID = $Null
}
}
Clique no botão
$btnPingFrame_Click = {
$Per = $RestID + 'per'
$Cer = $RestID + "cer"
$R = $RestID + 'r'
Get-Process |Where-Object {$_.MainWindowTitle -eq "PER - PROVIDER EDGE ROUTER"} | Stop-Process
start-process cmd -ArgumentList "/C","mode con:cols=75 lines=19 && COLOR 0b && title PER - PROVIDER EDGE ROUTER&& ping $Per -t"
Set-Window -ProcessName cmd -processtitle 'PER - PROVIDER EDGE ROUTER' -x 1 -y 1 -Width 615 -Height 345 -UserSessionID $UserSessionID -Passthru
Get-Process |Where-Object {$_.MainWindowTitle -eq "CER - CUSTOMER EDGE ROUTER"} | Stop-Process
start-process cmd -ArgumentList "/C","mode con:cols=75 lines=19 && COLOR 0a && title CER - CUSTOMER EDGE ROUTER&& ping $Cer -t"
Set-Window -ProcessName cmd -processtitle 'CER - CUSTOMER EDGE ROUTER' -x 1 -y 335 -Width 615 -Height 345 -UserSessionID $UserSessionID -Passthru
Get-Process |Where-Object {$_.MainWindowTitle -eq "R - AGGREGATED NETWORK"} | Stop-Process
start-process cmd -ArgumentList "/C","mode con:cols=75 lines=19 && COLOR 0c && title R - AGGREGATED NETWORK&& ping $R -t"
Set-Window -ProcessName cmd -processtitle 'R - AGGREGATED NETWORK' -x 1 -y 670 -Width 615 -Height 345 -UserSessionID $UserSessionID -Passthru
}