Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- Written by David Ott
- I stole some code from www.thesurlyadmin.com (noted in the set-alternatingrows function)
- This script will read the odata web pages from a XenDesktop Director server, and create a .htm file
- with all sessions in a given period that took longer than a set maximum (in milliseconds) to logon.
- usage:
- 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)>
- powershell.exe -file c:\script.ps1 -window h -ddc deliverycontroller.domain.com -max 80000
- Search for and pay attention to any comments marked with #####
- #>
- Param(
- [parameter(mandatory=$true)]
- [string]$window,
- [parameter(mandatory=$true)]
- [string]$ddc,
- [int]$max = "60000"
- )
- Function Set-AlternatingRows {
- <#
- .SYNOPSIS
- Simple function to alternate the row colors in an HTML table
- .DESCRIPTION
- This function accepts pipeline input from ConvertTo-HTML or any
- string with HTML in it. It will then search for <tr> and replace
- it with <tr class=(something)>. With the combination of CSS it
- can set alternating colors on table rows.
- CSS requirements:
- .odd { background-color:#ffffff; }
- .even { background-color:#dddddd; }
- Classnames can be anything and are configurable when executing the
- function. Colors can, of course, be set to your preference.
- This function does not add CSS to your report, so you must provide
- the style sheet, typically part of the ConvertTo-HTML cmdlet using
- the -Head parameter.
- .PARAMETER Line
- String containing the HTML line, typically piped in through the
- pipeline.
- .PARAMETER CSSEvenClass
- Define which CSS class is your "even" row and color.
- .PARAMETER CSSOddClass
- Define which CSS class is your "odd" row and color.
- .EXAMPLE $Report | ConvertTo-HTML -Head $Header | Set-AlternateRows -CSSEvenClass even -CSSOddClass odd | Out-File HTMLReport.html
- $Header can be defined with a here-string as:
- $Header = @"
- <style>
- TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
- TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
- TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
- .odd { background-color:#ffffff; }
- .even { background-color:#dddddd; }
- </style>
- "@
- This will produce a table with alternating white and grey rows. Custom CSS
- is defined in the $Header string and included with the table thanks to the -Head
- parameter in ConvertTo-HTML.
- .NOTES
- Author: Martin Pugh
- Twitter: @thesurlyadm1n
- Spiceworks: Martin9700
- Blog: www.thesurlyadmin.com
- Changelog:
- 1.1 Modified replace to include the <td> tag, as it was changing the class
- for the TH row as well.
- 1.0 Initial function release
- .LINK
- http://community.spiceworks.com/scripts/show/1745-set-alternatingrows-function-modify-your-html-table-to-have-alternating-row-colors
- .LINK
- http://thesurlyadmin.com/2013/01/21/how-to-create-html-reports/
- #>
- [CmdletBinding()]
- Param(
- [Parameter(Mandatory,ValueFromPipeline)]
- [string]$Line,
- [Parameter(Mandatory)]
- [string]$CSSEvenClass,
- [Parameter(Mandatory)]
- [string]$CSSOddClass
- )
- Begin {
- $ClassName = $CSSEvenClass
- }
- Process {
- If ($Line.Contains("<tr><td>"))
- { $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
- If ($ClassName -eq $CSSEvenClass)
- { $ClassName = $CSSOddClass
- }
- Else
- { $ClassName = $CSSEvenClass
- }
- }
- Return $Line
- }
- }
- if ($window -eq "w") {
- $now = [datetime]::Now
- $start = [datetime]::Today.AddDays(-7)
- } elseif ($window -eq "m") {
- $now = [datetime]::Now
- $start = [datetime]::Today.AddMonths(-1)
- } elseif ($window -eq "d") {
- $now = [datetime]::Now
- $start = [datetime]::Now.AddDays(-1)
- } elseif ($window -eq "h") {
- $now = [datetime]::Now
- $start = [datetime]::Now.AddHours(-1)
- } else {
- Write-Host "Window:`nh = last hour`nd = last 24 hours`nw = last 7 days`nm = last month"
- break
- }
- $Header = @"
- <style>
- TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;width: 95%}
- TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
- TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
- .odd { background-color:#ffffff; }
- .even { background-color:#dddddd; }
- </style>
- "@
- <#####
- Below is one of my favorite ways to obscure credentials inside a powershell script.
- create the variable
- $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)
- then run the command below, type in the password to be encrypted, and hit enter - it will output the password into an obscured string
- Read-Host -AsSecureString | ConvertFrom-SecureString -Key $key
- 76492d1116743f0423413b16050a5345MgB8AGoAbwBBAEsAOAA2AE4AbABMAGIANgBNADUAUgBZAEEAWABtAHAAUgAzAEEAPQA9AHwAMgA0AGYAOABiAGEAOQA2AGIAYgAxADQAZQAxADUANgBmADcAZQA4ADAANwBhAGUAYwA4ADMAMwAyAGUAYQA5AG
- MAMwA3ADgAYwA0ADMAZgA3ADIAZQBmAGEAYgBlAGIANQAyADQAZAAxADQAOAA2ADAAZgBmAGMAMgBlADgANwA=
- put the output ^^ in one line and assign a variable
- $string = "76492d1116743f0423413b16050a5345MgB8AGoAbwBBAEsAOAA2AE4AbABMAGIANgBNADUAUgBZAEEAWABtAHAAUgAzAEEAPQA9AHwAMgA0AGYAOABiAGEAOQA2AGIAYgAxADQAZQAxADUANgBmADcAZQA4ADAANwBhAGUAYwA4ADMAMwAyAGUAYQA5AGMAMwA3ADgAYwA0ADMAZgA3ADIAZQBmAGEAYgBlAGIANQAyADQAZAAxADQAOAA2ADAAZgBmAGMAMgBlADgANwA="
- Then the rest is below.
- If you do not wish to save the credentials in the script delete the $key,$string, and $password lines below. Then
- replace $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\username",$password
- with
- $creds = get-credential # you will be prompted for credentials
- #####>
- $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)
- $string = "76492d1116743f0423413b16050a5345MgB8AGoAbwBBAEsAOAA2AE4AbABMAGIANgBNADUAUgBZAEEAWABtAHAAUgAzAEEAPQA9AHwAMgA0AGYAOABiAGEAOQA2AGIAYgAxADQAZQAxADUANgBmADcAZQA4ADAANwBhAGUAYwA4ADMAMwAyAGUAYQA5AGMAMwA3ADgAYwA0ADMAZgA3ADIAZQBmAGEAYgBlAGIANQAyADQAZAAxADQAOAA2ADAAZgBmAGMAMgBlADgANwA="
- $password = $string | ConvertTo-SecureString -Key $key
- ##### replace domain\username with the actual username that can see the odata web page on the ddc
- $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\username",$password
- $base = "http://$ddc/Citrix/Monitor/OData/v2/Data/"
- try {
- Invoke-RestMethod -Uri $base -Credential $creds -ErrorAction Stop -ErrorVariable err | Out-Null
- } catch {
- write-host $err.message -f Red
- break
- }
- Write-Host "Querying $ddc - this will take a minute" -f Gray
- $machines = (invoke-restmethod -uri ($base+"Machines") -Credential $creds).content.properties | select hostedmachinename,@{n="ID";e={$_.id."#text"}},@{n="DGID";e={$_.desktopgroupid."#text"}}
- $DesktopGroups = (invoke-restmethod -uri ($base+"DesktopGroups") -Credential $creds).content.properties | select Name,@{n="GID";e={$_.id."#text"}}
- $sessions = (invoke-restmethod -uri ($base+"Sessions") -Credential $creds).content.properties | ?{((get-date ($_.startdate."#text")) -ge $start) -and (([int]$_.logonduration."#text") -ge $max)}
- $users = (invoke-restmethod -uri ($base+"Users") -Credential $creds).content.properties | select Fullname,Username,@{n="ID";e={$_.id."#text"}}
- $connections = (Invoke-RestMethod -uri ($base+"Connections") -Credential $creds).content.properties | ?{($sessions.sessionkey."#text") -contains $_.sessionkey."#text"}
- $list = @()
- $z = 0
- $sc = ($sessions | Measure-Object).count
- $list = foreach ($session in $sessions) {
- $z++
- Write-Progress -Activity "Compiling Session Information..." -Status "Session $z of $sc" -PercentComplete ($z/$sc*100)
- $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)}},`
- @{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)}},`
- @{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)}},
- @{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)}},`
- @{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)}},`
- @{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)}},`
- @{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)}},`
- @{n="Machine";e={($machines | ?{$_.id -eq $session.machineid.'#text'}).hostedmachinename}},@{n="Delivery";e={($desktopgroups | ?{$_.GID -eq ($machines | ?{$_.id -eq $session.machineid.'#text'}).DGID}).name}}
- }
- $list = $list | sort start,user,logonduration
- $logpath = join-path ([environment]::getfolderpath("mydocuments")) Logon_logs
- if (!(test-path $logpath)) {md $logpath | out-null}
- $logname = join-path $logpath ("Long_login"+(get-date -f MM-dd-yyyy-HH-mm-ss)+".htm")
- $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
- Invoke-Item $logname
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement