SHARE
TWEET

Logon Duration

david62277 Feb 24th, 2016 (edited) 294 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Written by David Ott
  3. I stole some code from www.thesurlyadmin.com (noted in the set-alternatingrows function)
  4. This script will read the odata web pages from a XenDesktop Director server, and create a .htm file
  5. with all sessions in a given period that took longer than a set maximum (in milliseconds) to logon.
  6. usage:
  7. powershell.exe -file <script path> -window <window length - m,w,d,h> -ddc <delivery controller dns name> -max <max logon duration in milliseconds default is 60000 (60 seconds)>
  8. powershell.exe -file c:\script.ps1 -window h -ddc deliverycontroller.domain.com -max 80000
  9. Search for and pay attention to any comments marked with #####
  10. #>
  11. Param(
  12.     [parameter(mandatory=$true)]
  13.     [string]$window,
  14.     [parameter(mandatory=$true)]
  15.     [string]$ddc,
  16.     [int]$max = "60000"
  17. )
  18. Function Set-AlternatingRows {
  19.     <#
  20.     .SYNOPSIS
  21.         Simple function to alternate the row colors in an HTML table
  22.     .DESCRIPTION
  23.         This function accepts pipeline input from ConvertTo-HTML or any
  24.         string with HTML in it.  It will then search for <tr> and replace
  25.         it with <tr class=(something)>.  With the combination of CSS it
  26.         can set alternating colors on table rows.
  27.        
  28.         CSS requirements:
  29.         .odd  { background-color:#ffffff; }
  30.         .even { background-color:#dddddd; }
  31.        
  32.         Classnames can be anything and are configurable when executing the
  33.         function.  Colors can, of course, be set to your preference.
  34.        
  35.         This function does not add CSS to your report, so you must provide
  36.         the style sheet, typically part of the ConvertTo-HTML cmdlet using
  37.         the -Head parameter.
  38.     .PARAMETER Line
  39.         String containing the HTML line, typically piped in through the
  40.         pipeline.
  41.     .PARAMETER CSSEvenClass
  42.         Define which CSS class is your "even" row and color.
  43.     .PARAMETER CSSOddClass
  44.         Define which CSS class is your "odd" row and color.
  45.     .EXAMPLE $Report | ConvertTo-HTML -Head $Header | Set-AlternateRows -CSSEvenClass even -CSSOddClass odd | Out-File HTMLReport.html
  46.    
  47.         $Header can be defined with a here-string as:
  48.         $Header = @"
  49.         <style>
  50.         TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
  51.         TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
  52.         TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
  53.         .odd  { background-color:#ffffff; }
  54.         .even { background-color:#dddddd; }
  55.         </style>
  56.         "@
  57.        
  58.         This will produce a table with alternating white and grey rows.  Custom CSS
  59.         is defined in the $Header string and included with the table thanks to the -Head
  60.         parameter in ConvertTo-HTML.
  61.     .NOTES
  62.         Author:         Martin Pugh
  63.         Twitter:        @thesurlyadm1n
  64.         Spiceworks:     Martin9700
  65.         Blog:           www.thesurlyadmin.com
  66.        
  67.         Changelog:
  68.             1.1         Modified replace to include the <td> tag, as it was changing the class
  69.                         for the TH row as well.
  70.             1.0         Initial function release
  71.     .LINK
  72.         http://community.spiceworks.com/scripts/show/1745-set-alternatingrows-function-modify-your-html-table-to-have-alternating-row-colors
  73.     .LINK
  74.         http://thesurlyadmin.com/2013/01/21/how-to-create-html-reports/
  75.     #>
  76.     [CmdletBinding()]
  77.     Param(
  78.         [Parameter(Mandatory,ValueFromPipeline)]
  79.         [string]$Line,
  80.        
  81.         [Parameter(Mandatory)]
  82.         [string]$CSSEvenClass,
  83.        
  84.         [Parameter(Mandatory)]
  85.         [string]$CSSOddClass
  86.     )
  87.     Begin {
  88.         $ClassName = $CSSEvenClass
  89.     }
  90.     Process {
  91.         If ($Line.Contains("<tr><td>"))
  92.         {   $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
  93.             If ($ClassName -eq $CSSEvenClass)
  94.             {   $ClassName = $CSSOddClass
  95.             }
  96.             Else
  97.             {   $ClassName = $CSSEvenClass
  98.             }
  99.         }
  100.         Return $Line
  101.     }
  102. }
  103. if ($window -eq "w") {
  104. $now = [datetime]::Now
  105. $start = [datetime]::Today.AddDays(-7)
  106. } elseif ($window -eq "m") {
  107. $now = [datetime]::Now
  108. $start = [datetime]::Today.AddMonths(-1)
  109. } elseif ($window -eq "d") {
  110. $now = [datetime]::Now
  111. $start = [datetime]::Now.AddDays(-1)
  112. } elseif ($window -eq "h") {
  113. $now = [datetime]::Now
  114. $start = [datetime]::Now.AddHours(-1)
  115. } else {
  116. Write-Host "Window:`nh = last hour`nd = last 24 hours`nw = last 7 days`nm = last month"
  117. break
  118. }
  119. $Header = @"
  120. <style>
  121. TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;width: 95%}
  122. TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
  123. TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
  124. .odd { background-color:#ffffff; }
  125. .even { background-color:#dddddd; }
  126. </style>
  127. "@
  128.  
  129. <#####
  130. Below is one of my favorite ways to obscure credentials inside a powershell script.
  131. create the variable
  132. $key = (1,43,9,221,4,2,78,42,101,32,45,87,76,12,43,66,12,78,8,94,54,23,22,65)
  133. then run the command below, type in the password to be encrypted, and hit enter - it will output the password into an obscured string
  134. Read-Host -AsSecureString | ConvertFrom-SecureString -Key $key
  135. 76492d1116743f0423413b16050a5345MgB8AGoAbwBBAEsAOAA2AE4AbABMAGIANgBNADUAUgBZAEEAWABtAHAAUgAzAEEAPQA9AHwAMgA0AGYAOABiAGEAOQA2AGIAYgAxADQAZQAxADUANgBmADcAZQA4ADAANwBhAGUAYwA4ADMAMwAyAGUAYQA5AG
  136. MAMwA3ADgAYwA0ADMAZgA3ADIAZQBmAGEAYgBlAGIANQAyADQAZAAxADQAOAA2ADAAZgBmAGMAMgBlADgANwA=
  137. put the output ^^ in one line and assign a variable
  138. $string = "76492d1116743f0423413b16050a5345MgB8AGoAbwBBAEsAOAA2AE4AbABMAGIANgBNADUAUgBZAEEAWABtAHAAUgAzAEEAPQA9AHwAMgA0AGYAOABiAGEAOQA2AGIAYgAxADQAZQAxADUANgBmADcAZQA4ADAANwBhAGUAYwA4ADMAMwAyAGUAYQA5AGMAMwA3ADgAYwA0ADMAZgA3ADIAZQBmAGEAYgBlAGIANQAyADQAZAAxADQAOAA2ADAAZgBmAGMAMgBlADgANwA="
  139. Then the rest is below.
  140. If you do not wish to save the credentials in the script delete the $key,$string, and $password lines below.  Then
  141. replace $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\username",$password
  142. with
  143. $creds = get-credential # you will be prompted for credentials
  144. #####>
  145. $key = (1,43,9,221,4,2,78,42,101,32,45,87,76,12,43,66,12,78,8,94,54,23,22,65)
  146. $string = "76492d1116743f0423413b16050a5345MgB8AGoAbwBBAEsAOAA2AE4AbABMAGIANgBNADUAUgBZAEEAWABtAHAAUgAzAEEAPQA9AHwAMgA0AGYAOABiAGEAOQA2AGIAYgAxADQAZQAxADUANgBmADcAZQA4ADAANwBhAGUAYwA4ADMAMwAyAGUAYQA5AGMAMwA3ADgAYwA0ADMAZgA3ADIAZQBmAGEAYgBlAGIANQAyADQAZAAxADQAOAA2ADAAZgBmAGMAMgBlADgANwA="
  147. $password = $string | ConvertTo-SecureString -Key $key
  148. ##### replace domain\username with the actual username that can see the odata web page on the ddc
  149. $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\username",$password
  150. $base = "http://$ddc/Citrix/Monitor/OData/v2/Data/"
  151. try {
  152. Invoke-RestMethod -Uri $base -Credential $creds -ErrorAction Stop -ErrorVariable err | Out-Null
  153. } catch {
  154. write-host $err.message -f Red
  155. break
  156. }
  157. Write-Host "Querying $ddc - this will take a minute" -f Gray
  158. $machines = (invoke-restmethod -uri ($base+"Machines") -Credential $creds).content.properties | select hostedmachinename,@{n="ID";e={$_.id."#text"}},@{n="DGID";e={$_.desktopgroupid."#text"}}
  159. $DesktopGroups = (invoke-restmethod -uri ($base+"DesktopGroups") -Credential $creds).content.properties | select Name,@{n="GID";e={$_.id."#text"}}
  160. $sessions = (invoke-restmethod -uri ($base+"Sessions") -Credential $creds).content.properties | ?{((get-date ($_.startdate."#text")) -ge $start) -and (([int]$_.logonduration."#text") -ge $max)}
  161. $users = (invoke-restmethod -uri ($base+"Users") -Credential $creds).content.properties | select Fullname,Username,@{n="ID";e={$_.id."#text"}}
  162. $connections = (Invoke-RestMethod -uri ($base+"Connections") -Credential $creds).content.properties | ?{($sessions.sessionkey."#text") -contains $_.sessionkey."#text"}
  163. $list = @()
  164. $z = 0
  165. $sc = ($sessions | Measure-Object).count
  166. $list = foreach ($session in $sessions) {
  167. $z++
  168. Write-Progress -Activity "Compiling Session Information..." -Status "Session $z of $sc" -PercentComplete ($z/$sc*100)
  169. $session | select @{n="User";e={($users | ?{$_.id -eq $session.userid.'#text'}).username}},@{n="FullName";e={($users | ?{$_.id -eq $session.userid.'#text'}).fullname}},@{n="Start";e={get-date ($_.startdate."#text")}},@{n="LogonDuration";e={[math]::round(([int]$_.logonduration."#text" / 1000),2)}},`
  170. @{n="Brokering Time";e={[math]::round((($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).brokeringduration."#text" | select -index 0) / 1000,2)}},@{n="Authentication Time";e={[math]::round((($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).authenticationduration."#text" | select -index 0) /1000,2)}},`
  171. @{n="HDX Connection";e={[math]::round(((get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).HdxEndDate."#text" | select -index 0)) - (get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).HdxStartDate."#text" | select -index 0))).totalseconds,2)}},
  172. @{n="GPOs";e={[math]::round(((get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).gpoenddate."#text" | select -index 0)) - (get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).gpostartdate."#text" | select -index 0))).totalseconds,2)}},`
  173. @{n="Logon Scripts";e={[math]::round(((get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).LogOnScriptsEndDate."#text" | select -index 0)) - (get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).LogOnScriptsStartDate."#text" | select -index 0))).totalseconds,2)}},`
  174. @{n="Profile Load";e={[math]::round(((get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).ProfileLoadEndDate."#text" | select -index 0)) - (get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).ProfileLoadStartDate."#text" | select -index 0))).totalseconds,2)}},`
  175. @{n="Interactive Session";e={[math]::round(((get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).InteractiveEndDate."#text" | select -index 0)) - (get-date (($connections | ?{$_.sessionkey."#text" -eq $session.sessionkey."#text"}).InteractiveStartDate."#text" | select -index 0))).totalseconds,2)}},`
  176. @{n="Machine";e={($machines | ?{$_.id -eq $session.machineid.'#text'}).hostedmachinename}},@{n="Delivery";e={($desktopgroups | ?{$_.GID -eq ($machines | ?{$_.id -eq $session.machineid.'#text'}).DGID}).name}}
  177.  
  178. }
  179. $list = $list | sort start,user,logonduration
  180. $logpath = join-path ([environment]::getfolderpath("mydocuments")) Logon_logs
  181. if (!(test-path $logpath)) {md $logpath | out-null}
  182. $logname = join-path $logpath ("Long_login"+(get-date -f MM-dd-yyyy-HH-mm-ss)+".htm")
  183. $list | ConvertTo-Html -head $header -Title "Logon Time Report" -PreContent "<h1>Logon Time Report for $start -- $now</h1>" | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd | Out-File $logname
  184. Invoke-Item $logname
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top