locked
Powershell script to query Horizon View sessions longer than X days and send mail RRS feed

  • General discussion

  • Looking for feedback, suggestions or improvements regarding coding style and regex'ing the duration.

    #load the horizon view ps snapin
    add-pssnapin vm*
    
    #set variables for your organization
    
    #$Duration limited to 1-9 unless you want to figure out the -Duration input format or rewrite the regex
    $Duration = "9"
    $Domain = "ord.local"
    $FromAddr = "helpdesk@domain.tld"
    $FromName = "Helpdesk"
    $Subject = "Action Required: please log off"
    $Body = "Health of your Windows environment is negatively affected because you have been logged in for $UserDuration. We recommend logging off nightly, and strongly recommend logging off at least weekly.`r`n`r`nPlease let us know if you have questions or concerns about this."
    $ITToAddr = "helpdesk@domain.tld"
    $ITToName = "Helpdesk"
    $ITSubject = "Alert: The following users have been logged in longer than $Duration days"
    $ITBody = "<table border='0' width='auto' >`r`n<tbody>`r`n<tr>`r`n<td border='0' width='auto'>First Last</td>`r`n<td border='0' width='auto'>AD Account</td>`r`n<td border='0' width='auto'>Duration</td>`r`n<td border='0' width='auto'>Email</td>`r`n</tr>"
    $ITBodyEmailFlag = "One of the users could not be mailed because their AD Account has no Email address specified"
    $SMTPServer = "smtp.domain.tld"
    #leave this null
    $SetEmailFlag=""
    
    
    #get the horizon view sessions where the session is longer than $Duration
    $VDISessionObjectArray = (Get-RemoteSession | Where-Object { $_.duration -match "^[$Duration-9] days*|[\d]{2,3} days*" })
    
    #for each session, get the user name and email address from Active Directory
    foreach ($VDISessionObject in $VDISessionObjectArray) {
    
        $samAccountName = $VDISessionObject.Username.TrimStart($Domain) 
        $samAccountName = $samAccountName.TrimStart('\') #'
        $UserDuration = $VDISessionObject.Duration
        $ADUserObject=get-aduser -filter "samAccountName -like '$samAccountName'" -properties mail
        $ToAddr = ($ADUserObject).mail
        $ToName = ($ADUserObject).GivenName + " " + ($ADUserObject).SurName
        if($ToAddr){
            send-mailmessage -to "$ToName <$ToAddr>" -from "$FromName <$FromAddr>" -subject $Subject -body $Body -smtpServer $SMTPServer
        }else{
            $SetEmailFlag = "true"
        }
        #concatenate a list of the sessions and their details for an email to IT
        $ITBody += "`r`n<tr>`r`n<td border='0' width='auto'>$ToName</td>`r`n<td border='0' width='auto'>$samAccountName</td>`r`n<td border='0' width='auto'>$UserDuration</td>`r`n<td border='0' width='auto'>$ToAddr</td>`r`n</tr>"
    }
    
    #finish the body of the mail to IT
    $ITBody += "`r`n</tbody>`r`n</table><br><br>"
    
    #add a note if anyone didnt have an email address
    if ($SetEmailFlag) { $ITBody += "`r`n$ITBodyEmailFlag" }
    
    #send a mail to IT
    send-mailmessage -to "$ITToName <$ITToAddr>" -from "$FromName <$FromAddr>" -subject $ITSubject -body "$ITBody" -BodyAsHtml -smtpServer $SMTPServer

    • Edited by havealoha Thursday, June 4, 2015 6:50 PM
    • Changed type Bill_Stewart Wednesday, July 22, 2015 3:52 PM
    Thursday, June 4, 2015 3:53 PM

All replies

  • You should start by properly formatting your code.  Add white space to make code readable.


    \_(ツ)_/

    Thursday, June 4, 2015 4:09 PM
  • Most of this was done automatically be the editors auto-formatter.  A added a few blank lines and removed redundant comments.  Too many comments make code unreadable.  Don't comment on what is obvious..

    #load the horizon view ps snapin
    add-pssnapin vm*
    
    #set variables for your organization
    $FromAddr = "helpdesk@domain.tld"
    $FromName = "Helpdesk"
    $Subject = "Action Required: please log off"
    $Body = "Health of your Windows environment is negatively affected because you have been logged in for $UserDuration. We recommend logging off nightly, and strongly recommend logging off at least weekly.`r`n`r`nPlease let us know if you have questions or concerns about this."
    $ITToAddr = "helpdesk@domain.tld"
    $ITToName = "Helpdesk"
    $ITSubject = "Alert: The following users have been logged in longer than $Duration days"
    $ITBody = "<table border='0' width='auto' >`r`n<tbody>`r`n<tr>`r`n<td border='0' width='auto'>First Last</td>`r`n<td border='0' width='auto'>AD Account</td>`r`n<td border='0' width='auto'>Duration</td>`r`n<td border='0' width='auto'>Email</td>`r`n</tr>"
    $ITBodyEmailFlag = "One of the users could not be mailed because their AD Account has no Email address specified"
    $SMTPServer = "smtp.domain.tld"
    
    #$Duration limited to 1-9 unless you want to figure out the -Duration input format or rewrite the regex
    $Duration = "9"
    $Domain = "ord.local"
    
    #get the horizon view sessions where the session is longer than $Duration
    $VDISessionObjectArray = (Get-RemoteSession | Where-Object { $_.duration -match "^[$Duration-9] days*|[\d]{2,3} days*" })
    
    #for each session, get the user name and email address from Active Directory
    foreach ($VDISessionObject in $VDISessionObjectArray) {
    
        $samAccountName = $VDISessionObject.Username.TrimStart($Domain) 
        $samAccountName = $samAccountName.TrimStart('\') #'
        $UserDuration = $VDISessionObject.Duration
        $ADUserObject = get-aduser -filter $SamAccountName -properties mail
        $ToAddr = ($ADUserObject).mail
        $ToName = ($ADUserObject).GivenName + " " + ($ADUserObject).SurName
        if($ToAddr){
            send-mailmessage -to "$ToName <$ToAddr>" -from "$FromName <$FromAddr>" -subject $Subject -body $Body -smtpServer $SMTPServer
        }else{
            $SetEmailFlag = 1
        }
        #concatenate a list of the sessions and their details for an email to IT
        $ITBody += "`r`n<tr>`r`n<td border='0' width='auto'>$ToName</td>`r`n<td border='0' width='auto'>$samAccountName</td>`r`n<td border='0' width='auto'>$UserDuration</td>`r`n<td border='0' width='auto'>$ToAddr</td>`r`n</tr>"
    }
    
    #finish the body of the mail to IT
    $ITBody += "`r`n</tbody>`r`n</table><br><br>"
    
    #add a note if anyone didnt have an email address
    if ($SetEmailFlag) { $ITBody += "`r`n$ITBodyEmailFlag" }
    
    #send a mail to IT
    send-mailmessage -to "$ITToName <$ITToAddr>" -from "$FromName <$FromAddr>" -subject $ITSubject -body "$ITBody" -BodyAsHtml -smtpServer $SMTPServer

    Now it is much  easier to read.


    \_(ツ)_/





    • Edited by jrv Thursday, June 4, 2015 4:21 PM
    Thursday, June 4, 2015 4:18 PM
  • A handy URL regarding powershell readability and whitespace:

    https://technet.microsoft.com/en-us/magazine/hh475841.aspx

    Thursday, June 4, 2015 4:29 PM