Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .SYNOPSIS
  3.         Start a process and set the window to a given position and size.
  4.        
  5.     .DESCRIPTION
  6.         Starts a process using Start-Process cmdlet and set the windows position and size afterwards using SetWindowPos.
  7.         Any excess parameter will be passed to StartProcess. In case you want to set X, you must also set Y and vice
  8.         versa. Same for width and height.
  9.         Please be aware of programs that store their window position and size. You may not be able to get them back easily
  10.         if you place them outside of your displays range.
  11.  
  12.     .PARAMETER FilePath
  13.         This parameter specifies the executable file that will be passed to Start-Process. This is a mandatory parameter
  14.         for Start-ProcessAndSetWindow because it is also the only mandatory parameter for Start-Process. This way you will
  15.         be forced to use it.
  16.  
  17.     .PARAMETER PosX
  18.         Specifies the window's PosX position. PosX will be ignored if PosY has not been set.
  19.  
  20.     .PARAMETER PosY
  21.         Specifies the window's PosY position. PosY will be ignored if PosX has not been set.
  22.  
  23.     .PARAMETER Width
  24.         [Optional] Specifies the window's width. Width will be ignored if Height has not been set.
  25.         If set, the value must be lesser or equal to 0 to have any effect.
  26.  
  27.     .PARAMETER Height
  28.         [Optional] Specifies the window's height. Height will be ignored if Width has not been set.
  29.         If set, the value must be lesser or equal to 0 to have any effect.
  30.  
  31.     .PARAMETER StartProcessParameters
  32.         [Optional] Any excess parameters will be passed to Start-Process
  33.  
  34.     .NOTES
  35.         Name: Start-ProcessAndSetWindow
  36.         Author: Thorsten Windrath, https://www.windrath.com
  37.         Version History
  38.             1.0//Thorsten Windrath - 05/24/2016
  39.                 - Initial build
  40. #>
  41. function global:Start-ProcessAndSetWindow()
  42. {
  43.     [CmdletBinding()]
  44.     param
  45.     (
  46.         [Parameter(Mandatory = $true, Position = 0)]
  47.         [string] $FilePath,
  48.  
  49.         [Parameter(Mandatory = $true)]
  50.         [int] $PosX,
  51.  
  52.         [Parameter(Mandatory = $true)]
  53.         [int] $PosY,
  54.  
  55.         [Parameter(Mandatory = $false)]
  56.         [int] $Height = -1,
  57.  
  58.         [Parameter(Mandatory = $false)]
  59.         [int] $Width = -1,
  60.          
  61.         [Parameter(Mandatory = $false, ValueFromRemainingArguments=$true)]
  62.         $StartProcessParameters
  63.     )
  64.        
  65.     # Invoke process
  66.     $process = "Start-Process -FilePath $FilePath -PassThru $StartProcessParameters" | Invoke-Expression
  67.  
  68.     # We need to get the process' MainWindowHandle. That's not the processes handle or Id!
  69.     if($process -is [System.Array]) { $procId = $process[0].Id } else { $procId = $process.Id }
  70.  
  71.     # ... fallback in case something goes south (wait up to 5 seconds for the process to launch)
  72.     $i = 50  
  73.  
  74.     # ... Start looking for the main window handle. May take a bit of time for the window to show up
  75.     $mainWindowHandle = [System.IntPtr]::Zero
  76.     while($mainWindowHandle -eq [System.IntPtr]::Zero)
  77.     {
  78.        [System.Threading.Thread]::Sleep(100)
  79.        $tmp = Get-Process -Id $procId -ErrorAction SilentlyContinue
  80.  
  81.        if($tmp -ne $null)
  82.        {
  83.          $mainWindowHandle = $tmp.MainWindowHandle
  84.        }
  85.  
  86.        $i = $i - 1
  87.        if($i -le 0)
  88.        {
  89.          break
  90.        }
  91.     }
  92.    
  93.     # Once we grabbed the MainWindowHandle, we need to use the Win32-API function SetWindowPosition (using inline C#)
  94.     if($mainWindowHandle -ne [System.IntPtr]::Zero)
  95.     {
  96.         $CSharpSource = @"
  97.            using System;
  98.            using System.Runtime.InteropServices;
  99.  
  100.            namespace TW.Tools.InlinePS
  101.            {
  102.                public static class WindowManagement
  103.                {
  104.                    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
  105.                    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
  106.                    
  107.                    public const int SWP_NOSIZE = 0x01, SWP_NOMOVE = 0x02, SWP_SHOWWINDOW = 0x40, SWP_HIDEWINDOW = 0x80;
  108.  
  109.                    public static void SetPosition(IntPtr handle, int x, int y, int width, int height)
  110.                    {
  111.                        if (handle != null)
  112.                        {
  113.                            SetWindowPos(handle, 0, x, y, 0, 0, SWP_NOSIZE | SWP_HIDEWINDOW);
  114.                
  115.                            if (width > -1 && height > -1)
  116.                                SetWindowPos(handle, 0, 0, 0, width, height, SWP_NOMOVE);
  117.  
  118.                            SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
  119.                        }
  120.                    }
  121.                }
  122.            }
  123. "@
  124.  
  125.         Add-Type -TypeDefinition $CSharpSource -Language CSharp -ErrorAction SilentlyContinue
  126.         [TW.Tools.InlinePS.WindowManagement]::SetPosition($mainWindowHandle, $PosX, $PosY, $Width, $Height);
  127.     }
  128.     else
  129.     {
  130.       throw "Couldn't find the MainWindowHandle, aborting (your process should be still alive)"
  131.     }
  132. }
  133.  
  134. #Start-ProcessAndSetWindow powershell -Argumentlist "{ssh -p 443 -i .\proton davinci@167.99.70.96}" -PosX 2873 -PosY 0 -Height 529 -Width 975
  135. #Start-ProcessAndSetWindow powershell -Argumentlist "{ssh -p 443 -i .\proton davinci@167.99.70.96}" -PosX 2873 -PosY 0 -Height 529 -Width 975
  136. Start-ProcessAndSetWindow -FilePath "C:\PROGRA~2\Google\Chrome\Application\chrome.exe" -Argumentlist "https://nebula.service-now.com/navpage.do" -PosX 2873 -PosY 0 -Height 529 -Width 975
  137. #Start-ProcessAndSetWindow WhatsApp1 -FilePath "C:\Users\leonardnyh\AppData\Local\WhatsApp\whatsapp.exe" -PosX 2873 -PosY 0 -Height 529 -Width 975
  138. #Start-ProcessAndSetWindow Slack1 -FilePath "C:\Users\leonardnyh\AppData\Local\slack\slack.exe" -PosX 2873 -PosY 0 -Height 529 -Width 975
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement