Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function end-script {
- if ($internetExplorer -is [System.__ComObject]) {
- $logoff = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "a")) | ?{$_.innerhtml -eq "log off"}
- if ($logoff -ne $null) {
- $logoff.click()
- Start-Sleep -s 5
- }
- if ($internetExplorer | Get-Member 'Quit') {
- $internetExplorer.Quit()
- }
- [System.Runtime.Interopservices.Marshal]::ReleaseComObject($internetExplorer) | Out-Null
- rv internetexplorer -Scope global
- rv document -Scope global
- rv buttons -Scope global
- }
- break
- }
- function send-results($success,$reason) {
- $smtpserver = "smtp server address" # mail server address
- $msg = New-Object net.mail.mailmessage
- $smtp = new-object Net.Mail.SmtpClient($smtpServer)
- if ($success -eq $false) {
- $msg.Priority = [System.Net.Mail.MailPriority]::High
- } else {
- $msg.Priority = [System.Net.Mail.MailPriority]::Normal
- }
- $msg.IsBodyHtml = $true
- $msg.From = "notification@yourcompany.com" # from address
- $msg.to.Add("your@emailaddresshere.com") # to address
- $msg.Subject = "Citrix Test Connection Results"
- if ($success -eq $false) {
- $msg.Body = "Result: Test connection to $resource failed!<br>Reason: $reason"
- } else {
- $msg.Body = "Result: Test connection to $resource successful!"
- }
- $smtp.send($msg)
- }
- function check-buttons($buttons) {
- $check = 0
- foreach ($button in $buttons) {
- if (($button.classname -notlike "*[a-z]*") -or ($button.classname -like "*<*")) {
- continue
- } else {
- $check = 1
- return $check
- }
- }
- return $check
- }
- function ConvertFrom-SecureToPlain {
- param( [Parameter(Mandatory=$true)][System.Security.SecureString] $SecurePassword)
- $PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
- $PlainTextPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordPointer)
- [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PasswordPointer)
- $PlainTextPassword
- }
- <#
- I don't like putting passwords in clear text into a script... to create the passstring create your $key (can use the key below)
- run these commands
- $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)
- Read-Host -AsSecureString | ConvertFrom-SecureString -Key $key
- this will output something like this:
- 76492d1116743f0423413b16050a5345MgB8AEQAVQAxAFIARQBNAHYAWQBzAE4AZwBvAGUAUAB6AGMAKwBwAC8AVABlAFEAPQA9AHwANAAwAGYANQA0ADgAYQAxAGQAYQA3ADAANwBmAGQAZgAxADkAOQAxADgANwA3ADcAMAAxAGIAZgBlADUAYQAwAG
- UAZQA5AGMANQBlAGMAOQBmAGEANwA0ADUANwBhAGEAYgA5AGIANwA0ADcAZgBlADEANwA3AGQAMQBiADMAOAA=
- notice it is on 2 lines - that is bad for what we want, so make it one line and edit the $passstring variable:
- $passstring = "76492d1116743f0423413b16050a5345MgB8AEQAVQAxAFIARQBNAHYAWQBzAE4AZwBvAGUAUAB6AGMAKwBwAC8AVABlAFEAPQA9AHwANAAwAGYANQA0ADgAYQAxAGQAYQA3ADAANwBmAGQAZgAxADkAOQAxADgANwA3ADcAMAAxAGIAZgBlADUAYQAwAGUAZQA5AGMANQBlAGMAOQBmAGEANwA0ADUANwBhAGEAYgA5AGIANwA0ADcAZgBlADEANwA3AGQAMQBiADMAOAA="
- If you don't care about clear text in a script you can comment out $passstring/$secure and change $password to "WhatEverY0urP@ssw0rdis"
- #>
- $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)
- # username of the user that will be logging on
- $username = "username"
- #long string like the one above which is the password of the user encrypted in a long string
- $passstring = "long password string here"
- # converts the password string back into a secure string
- $secure = $passstring | ConvertTo-SecureString -Key $key
- # calls the function to convert the secure string to plain text (so it can enter it on the webpage)
- $Password = ConvertFrom-SecureToPlain -SecurePassword $secure
- # resource as it appears on your storefront webpage (ie: Desktop, Outlook, etc. - does not matter if it is visible to you)
- $resource = "application or desktop name to launch"
- # hostname mask of your app servers or desktops - in this case the vm would be anything "desktop0-9*"
- $mask = "desktop[0-9]"
- # time to wait for the session to launch before logging off
- $wait = 30
- New-Variable -Name internetexplorer -Value (New-Object -ComObject "internetexplorer.application") -Scope script
- $internetexplorer.Visible = $true # change this to $false if you do not wish to see the IE webpage
- $internetexplorer.Navigate2("https://storefront.yourcompany.com") # storefront or gateway url https://storefront.company.com
- while ($internetexplorer.Busy -eq $true) {
- Start-Sleep -s 1
- }
- New-Variable -Name document -Value $internetexplorer.Document -Scope script
- if ($document.url -like "*/vpn/*") {
- $gateway = $true
- $wfica1 = try {get-process wfica32 -ErrorAction stop | select -expand id} catch {""}
- } else {
- $gateway = $false
- }
- $Buttons = @()
- while ((check-buttons -buttons $buttons) -eq "0") {
- Start-Sleep -s 1
- if ($gateway -eq $false) {
- $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "a"))
- } else {
- $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input"))
- }
- }
- if ($buttons.innerhtml -match "Continue") {
- ($buttons | ?{$_.innerhtml -eq "Continue"}).click()
- while ($internetexplorer.Busy -eq $true) {
- Start-Sleep -s 1
- }
- }
- $Buttons = @()
- while ((check-buttons -buttons $buttons) -eq "0") {
- Start-Sleep -s 1
- if ($gateway -eq $false) {
- $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "a"))
- } else {
- $buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input"))
- }
- }
- if (($buttons.innerhtml -match "Log On") -or (($gateway -eq $true) -and ($buttons -ne $null))) {
- if ($gateway -eq $false) {
- $usernamebox = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input")) | ?{$_.name -eq "username"}
- $passwordbox = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input")) | ?{$_.name -eq "password"}
- } else {
- $usernamebox = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input")) | ?{$_.name -eq "login"}
- $passwordbox = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "input")) | ?{$_.name -eq "passwd"}
- }
- if ($usernamebox -eq $null) {
- $success = $false
- $reason = "Unable to find User Name field!"
- send-results -success $success -reason $reason
- end-script
- }
- if ($passwordbox -eq $null) {
- $success = $false
- $reason = "Unable to find Password field!"
- send-results -success $success -reason $reason
- end-script
- }
- $usernamebox.value = $username
- $passwordbox.value = $password
- ($buttons | ?{$_.outerhtml -like "*log on*"}).click()
- while ($internetexplorer.Busy -eq $true) {
- Start-Sleep -s 1
- }
- } else {
- $success = $false
- $reason = "Unable to find Log On button."
- send-results -success $success -reason $reason
- end-script
- }
- $Buttons = @()
- while ((check-buttons -buttons $buttons) -eq "0") {
- Start-Sleep -s 1
- $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "a"))
- }
- if (($buttons | select innerhtml) -match "Continue") {
- ($buttons | ?{$_.innerhtml -eq "Continue"}).click()
- while ($internetexplorer.Busy -eq $true) {
- Start-Sleep -s 1
- }
- }
- $Buttons = @()
- while ((check-buttons -buttons $buttons) -eq "0") {
- Start-Sleep -s 1
- $Buttons = @([System.__ComObject].InvokeMember(“getElementsByTagName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $document, "img"))
- }
- $rbutton = $buttons | ?{$_.alt -eq $resource}
- if ($rbutton -ne $null) {
- $rbutton.click()
- Start-Sleep -s $wait # give time to launch
- } else {
- $success = $false
- $reason = "Unable to find $resource!"
- send-results -success $success -reason $reason
- end-script
- }
- if ($gateway -eq $false) {
- [System.Reflection.Assembly]::LoadFile("c:\program files (x86)\citrix\ica client\wficalib.dll") | Out-Null
- $ica = New-Object WFICALib.ICAClientClass
- $ica.OutputMode = [WFICALib.OutputMode]::OutputModeNormal
- $enum = $ica.EnumerateCCMSessions()
- $num = $ica.GetEnumNameCount($enum );
- #"Number of live CCM sessions are:" + $num.tostring()
- for( $index = 0; $index -lt $num ;$index++)
- {
- $sessionid = $ica.GetEnumNameByIndex($enum, $index)
- $ica.StartMonitoringCCMSession($sessionid,$true)
- $APPSERVER = $ica.GetSessionString(0)
- if ($appserver -match $mask) {
- $success = $true
- $reason = ""
- $ica.Logoff() | Out-Null
- $ica.StopMonitoringCCMSession($sessionid) | Out-Null
- send-results -success $success -reason $reason
- } else {
- $ica.StopMonitoringCCMSession($sessionid) | Out-Null
- }
- }
- } else {
- $wfica2 = try {get-process wfica32 -ErrorAction stop | select -expand id} catch {$null}
- if ($wfica1 -eq $wfica2) {
- $success = $false
- } else {
- $success = $true
- $reason = ""
- stop-process -id (Compare-Object $wfica1 $wfica2 | ?{$_.sideindicator -eq "=>"} | select -expand inputobject)
- send-results -success $success -reason $reason
- }
- }
- if ($success -eq $false) {
- $reason = "Failed to launch the resource."
- send-results -success $success -reason $reason
- $ica.CloseEnumHandle($enum) | Out-Null
- end-script
- }
- if ($gateway -eq $false) {
- $ica.CloseEnumHandle($enum) | Out-Null
- }
- end-script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement