# Launch Titanium script v 2.0 2/18/15 by Bruce Cowper # This script launches Titanium Projector on the local machine and provides a toggle on / off mechanism. i.e. if Titanium Projector is not running, it it launched, if it is running, it is killed. function Running ($app) { # This function tests to see if the $app is running and either returns the process ID, or $False $Running = Get-Process $app -ErrorAction SilentlyContinue if($Running -eq $null) { $Running = $False } return $Running } # set variable defaults $running = $False $appname = "TitaniumProjector" # check to see what OS version is installed - note that the path for Titanium will change between the x86 and x64 bit OS versions. if ((Get-WmiObject -Class Win32_OperatingSystem -ea 0).OSArchitecture -eq '64-bit') { # set the app path for x64 PCs $apppath = "C:\Program Files (x86)\Microsoft Research\Titanium Projection\" } else { # set the app path for x86 PCs $apppath = "C:\Program Files\Microsoft Research\Titanium Projection\" } # build full app path for execution $fullapppath = $apppath+$appname # import User32.dll to enable viewing of current app screen status (ShowWindowAsync) $sig = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);' Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32 # minimize PowerShell - this avoids having the PowerShell window on screen during execution of script. $PSId = @(Get-Process "powershell" -ErrorAction SilentlyContinue)[0].MainWindowHandle [Win32.NativeMethods]::ShowWindowAsync($PSId, 2) # check to see if the $app is running $running = Running ($appname) If ($running -eq $False) #if the app is not running, we want to start it. { # start application $app = start-process $fullapppath -passthru # minimize Titanium to wait for incoming connection start-sleep 1 If ($app.MainWindowHandle) { [Win32.NativeMethods]::ShowWindowAsync($app.MainWindowHandle, 6) } # wait timer for incoming connection. # Note that on a wired network handshaking will be almost instant, but on slower wireless devices this may need to be increased. start-sleep 3 # Restore titanium to full screen If ($app.MainWindowHandle) { [Win32.NativeMethods]::ShowWindowAsync($app.MainWindowHandle, 3) } } Else # If the process is already running, we want to kill it. This allows us to toggle the app state { Get-Process $appname | Stop-Process }