Advertisement
david62277

StoreFront/Gateway Launcher

Apr 2nd, 2015
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function end-script {
  2. if ($internetExplorer -is [System.__ComObject]) {
  3. $logoff = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "a")) | ?{$_.innerhtml -eq "log off"}
  4. if ($logoff -ne $null) {
  5. $logoff.click()
  6. Start-Sleep -s 5
  7. }
  8.         if ($internetExplorer | Get-Member 'Quit') {        
  9.             $internetExplorer.Quit()
  10.         }
  11.         [System.Runtime.Interopservices.Marshal]::ReleaseComObject($internetExplorer) | Out-Null
  12.         rv internetexplorer -Scope global
  13.         rv document -Scope global
  14.         rv buttons -Scope global
  15.     }
  16.     break
  17. }
  18. function send-results($success,$reason) {
  19. $smtpserver = "smtp server address" # mail server address
  20. $msg = New-Object net.mail.mailmessage
  21. $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  22. if ($success -eq $false) {
  23. $msg.Priority = [System.Net.Mail.MailPriority]::High
  24. } else {
  25. $msg.Priority = [System.Net.Mail.MailPriority]::Normal
  26. }
  27. $msg.IsBodyHtml = $true
  28. $msg.From = "notification@yourcompany.com" # from address
  29. $msg.to.Add("your@emailaddresshere.com") # to address
  30. $msg.Subject = "Citrix Test Connection Results"
  31. if ($success -eq $false) {
  32. $msg.Body = "Result: Test connection to $resource failed!<br>Reason: $reason"
  33. } else {
  34. $msg.Body = "Result: Test connection to $resource successful!"
  35. }
  36. $smtp.send($msg)
  37. }
  38. function check-buttons($buttons) {
  39. $check = 0
  40. foreach ($button in $buttons) {
  41. if (($button.classname -notlike "*[a-z]*") -or ($button.classname -like "*<*")) {
  42. continue
  43. } else {
  44. $check = 1
  45. return $check
  46. }
  47. }
  48. return $check
  49. }
  50. function ConvertFrom-SecureToPlain {
  51.    
  52.     param( [Parameter(Mandatory=$true)][System.Security.SecureString] $SecurePassword)
  53.    
  54.    
  55.     $PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
  56.    
  57.    
  58.     $PlainTextPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordPointer)
  59.    
  60.    
  61.     [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PasswordPointer)
  62.    
  63.    
  64.     $PlainTextPassword
  65.    
  66. }
  67. <#
  68. I don't like putting passwords in clear text into a script... to create the passstring create your $key (can use the key below)
  69. run these commands
  70. $key = (2,44,10,222,5,3,79,43,102,33,46,88,77,13,44,67,13,79,9,95,55,24,23,66)
  71. Read-Host -AsSecureString | ConvertFrom-SecureString -Key $key
  72.  
  73. this will output something like this:
  74. 76492d1116743f0423413b16050a5345MgB8AEQAVQAxAFIARQBNAHYAWQBzAE4AZwBvAGUAUAB6AGMAKwBwAC8AVABlAFEAPQA9AHwANAAwAGYANQA0ADgAYQAxAGQAYQA3ADAANwBmAGQAZgAxADkAOQAxADgANwA3ADcAMAAxAGIAZgBlADUAYQAwAG
  75. UAZQA5AGMANQBlAGMAOQBmAGEANwA0ADUANwBhAGEAYgA5AGIANwA0ADcAZgBlADEANwA3AGQAMQBiADMAOAA=
  76.  
  77. notice it is on 2 lines - that is bad for what we want, so make it one line and edit the $passstring variable:
  78. $passstring = "76492d1116743f0423413b16050a5345MgB8AEQAVQAxAFIARQBNAHYAWQBzAE4AZwBvAGUAUAB6AGMAKwBwAC8AVABlAFEAPQA9AHwANAAwAGYANQA0ADgAYQAxAGQAYQA3ADAANwBmAGQAZgAxADkAOQAxADgANwA3ADcAMAAxAGIAZgBlADUAYQAwAGUAZQA5AGMANQBlAGMAOQBmAGEANwA0ADUANwBhAGEAYgA5AGIANwA0ADcAZgBlADEANwA3AGQAMQBiADMAOAA="
  79.  
  80. If you don't care about clear text in a script  you can comment out $passstring/$secure and change $password to "WhatEverY0urP@ssw0rdis"
  81. #>
  82. $key = (2,44,10,222,5,3,79,43,102,33,46,88,77,13,44,67,13,79,9,95,55,24,23,66)
  83. # username of the user that will be logging on
  84. $username = "username"
  85. #long string like the one above which is the password of the user encrypted in a long string
  86. $passstring = "long password string here"
  87. # converts the password string back into a secure string
  88. $secure = $passstring | ConvertTo-SecureString -Key $key
  89. # calls the function to convert the secure string to plain text (so it can enter it on the webpage)
  90. $Password = ConvertFrom-SecureToPlain -SecurePassword $secure
  91. # resource as it appears on your storefront webpage (ie: Desktop, Outlook, etc. - does not matter if it is visible to you)
  92. $resource = "application or desktop name to launch"
  93. # hostname mask of your app servers or desktops - in this case the vm would be anything "desktop0-9*"
  94. $mask = "desktop[0-9]"  
  95. # time to wait for the session to launch before logging off
  96. $wait = 30
  97. New-Variable -Name internetexplorer -Value (New-Object -ComObject "internetexplorer.application") -Scope script
  98. $internetexplorer.Visible = $true # change this to $false if you do not wish to see the IE webpage
  99. $internetexplorer.Navigate2("https://storefront.yourcompany.com") # storefront or gateway url https://storefront.company.com
  100. while ($internetexplorer.Busy -eq $true) {
  101. Start-Sleep -s 1
  102. }
  103. New-Variable -Name document -Value $internetexplorer.Document -Scope script
  104. if ($document.url -like "*/vpn/*") {
  105. $gateway = $true
  106. $wfica1 = try {get-process wfica32 -ErrorAction stop | select -expand id} catch {""}
  107. } else {
  108. $gateway = $false
  109. }
  110. $Buttons = @()
  111. while ((check-buttons -buttons $buttons) -eq "0") {
  112. Start-Sleep -s 1
  113. if ($gateway -eq $false) {
  114. $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "a"))
  115. } else {
  116. $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input"))
  117. }
  118. }
  119. if ($buttons.innerhtml -match "Continue") {
  120. ($buttons | ?{$_.innerhtml -eq "Continue"}).click()
  121. while ($internetexplorer.Busy -eq $true) {
  122. Start-Sleep -s 1
  123. }
  124. }
  125. $Buttons = @()
  126. while ((check-buttons -buttons $buttons) -eq "0") {
  127. Start-Sleep -s 1
  128. if ($gateway -eq $false) {
  129. $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "a"))
  130. } else {
  131. $buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input"))
  132. }
  133. }
  134. if (($buttons.innerhtml -match "Log On") -or (($gateway -eq $true) -and ($buttons -ne $null))) {
  135. if ($gateway -eq $false) {
  136. $usernamebox = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input")) | ?{$_.name -eq "username"}
  137. $passwordbox = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input")) | ?{$_.name -eq "password"}
  138. } else {
  139. $usernamebox = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input")) | ?{$_.name -eq "login"}
  140. $passwordbox = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input")) | ?{$_.name -eq "passwd"}
  141. }
  142. if ($usernamebox -eq $null) {
  143. $success = $false
  144. $reason = "Unable to find User Name field!"
  145. send-results -success $success -reason $reason
  146. end-script
  147. }
  148. if ($passwordbox -eq $null) {
  149. $success = $false
  150. $reason = "Unable to find Password field!"
  151. send-results -success $success -reason $reason
  152. end-script
  153. }
  154. $usernamebox.value = $username
  155. $passwordbox.value = $password
  156. ($buttons | ?{$_.outerhtml -like "*log on*"}).click()
  157. while ($internetexplorer.Busy -eq $true) {
  158. Start-Sleep -s 1
  159. }
  160. } else {
  161. $success = $false
  162. $reason = "Unable to find Log On button."
  163. send-results -success $success -reason $reason
  164. end-script
  165. }
  166. $Buttons = @()
  167. while ((check-buttons -buttons $buttons) -eq "0") {
  168. Start-Sleep -s 1
  169. $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "a"))
  170. }
  171. if (($buttons | select innerhtml) -match "Continue") {
  172. ($buttons | ?{$_.innerhtml -eq "Continue"}).click()
  173. while ($internetexplorer.Busy -eq $true) {
  174. Start-Sleep -s 1
  175. }
  176. }
  177. $Buttons = @()
  178. while ((check-buttons -buttons $buttons) -eq "0") {
  179. Start-Sleep -s 1
  180. $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "img"))
  181. }
  182. $rbutton = $buttons | ?{$_.alt -eq $resource}
  183. if ($rbutton -ne $null) {
  184. $rbutton.click()
  185. Start-Sleep -s $wait # give time to launch
  186. } else {
  187. $success = $false
  188. $reason = "Unable to find $resource!"
  189. send-results -success $success -reason $reason
  190. end-script
  191. }
  192. if ($gateway -eq $false) {
  193. [System.Reflection.Assembly]::LoadFile("c:\program files (x86)\citrix\ica client\wficalib.dll") | Out-Null
  194. $ica = New-Object WFICALib.ICAClientClass
  195. $ica.OutputMode = [WFICALib.OutputMode]::OutputModeNormal
  196. $enum = $ica.EnumerateCCMSessions()
  197. $num = $ica.GetEnumNameCount($enum );
  198. #"Number of live CCM sessions are:" + $num.tostring()
  199. for( $index = 0; $index -lt $num ;$index++)
  200. {
  201. $sessionid = $ica.GetEnumNameByIndex($enum, $index)
  202. $ica.StartMonitoringCCMSession($sessionid,$true)
  203. $APPSERVER = $ica.GetSessionString(0)
  204. if ($appserver -match $mask) {
  205. $success = $true
  206. $reason = ""
  207. $ica.Logoff() | Out-Null
  208. $ica.StopMonitoringCCMSession($sessionid) | Out-Null
  209. send-results -success $success -reason $reason
  210. } else {
  211. $ica.StopMonitoringCCMSession($sessionid) | Out-Null
  212. }
  213. }
  214. } else {
  215. $wfica2 = try {get-process wfica32 -ErrorAction stop | select -expand id} catch {$null}
  216. if ($wfica1 -eq $wfica2) {
  217. $success = $false
  218. } else {
  219. $success = $true
  220. $reason = ""
  221. stop-process -id (Compare-Object $wfica1 $wfica2 | ?{$_.sideindicator -eq "=>"} | select -expand inputobject)
  222. send-results -success $success -reason $reason
  223. }
  224. }
  225. if ($success -eq $false) {
  226. $reason = "Failed to launch the resource."
  227. send-results -success $success -reason $reason
  228. $ica.CloseEnumHandle($enum) | Out-Null
  229. end-script
  230. }
  231. if ($gateway -eq $false) {
  232. $ica.CloseEnumHandle($enum) | Out-Null
  233. }
  234. end-script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement