Terminal Services / Citrix / Remote Desktop

Blah

Send a Message to Terminal Services users of a specified group

######################################################################## # Created with: SAPIEN Technologies, Inc., PrimalForms 2009 v1.1.10.0 # Created on:   10/19/2010 4:19 PM # Created by:   Kevin Curran # Organization: Disorganized ######################################################################## param([switch]$debug) #this will allow the write-debug information to be writen to the console If ($debug) {$DebugPreference="Continue"} $LogPath="$($env:systemdrive)\scripts\logs\" $TranscriptLog = $LogPath + "PopUpMessage_" + $(get-date -uformat "%Y-%m-%d") + "_Transcript.log" Start-Transcript $TranscriptLog $StartTime = Get-Date  IF (Get-PSSnapin | where {$_.name -eq "quest.activeroles.admanagement"})  {write-host "Quest Active Roles snapin already loaded"} Else {add-PSSnapin  quest.activeroles.admanagement} #this module is not signed allow the import Set-ExecutionPolicy -Scope process unrestricted -Force Import-Module PSTerminalServices Write-Host "DebugPreference: $DebugPreference" #$Group = "Domain Admins" $Group = "TEST USERS" $PopupMessage = "Hey there how are you doing." #Build a list of servers $Servers = Get-QADComputer -SearchRoot 'blah.corp/Domain Controllers'  foreach ($Server in $Servers) {     #query each server for the list of Terminal Services sessions that have a username Write-Debug "Get the list of TS Sessions for $($Server.Name)" #this seems to terminate the script if it gets an error even if you use the -ErrorAction #trying to catch error try  {     $TSSessions = Get-TSSession -ComputerName $($Server.Name) | where {$_.Username -ne ""} } Catch  {     write-host -ForegroundColor red "Error: Failed to get sessions from machine (Error:"  $_.Exception.Message ")"     write-host  #continue #not sure why continue doesn't actually continue }     #if there are no sessions move on to the next server if ($TSSessions -eq $null)     {         Write-Host "$($Server.Name) has no open Terminal Services Sessions"     }     Else     {         #query each session on the server foreach ($session in $TSSessions)         {             #If you are a member of this group send the users Session a message box If ($(Get-QADMemberOf  -Identity "$($session.Username)" -Name $Group))             {                 Write-Host -ForegroundColor green "User:$($session.Username) on Server:$($session.server.servername) is a member of $Group" try  {     Send-TSMessage  -ComputerName $($session.server.servername) -id $($session.SessionId)-Text $PopupMessage -Caption "Notice" } Catch  {     write-host -ForegroundColor red "Error: Failed to send message (Error:"  $_.Exception.Message ")"     write-host  #continue #not sure why continue doesn't actually continue }             }             Else             {                 Write-Debug "User:$($session.Username) on Server:$($session.server.servername) is a not member of $Group"             }         }     } }   $StopTime = Get-Date $ElapsedTime =$StopTime - $StartTime Stop-Transcript  $smtpServer = "mail.blah.com" $To = "Kevin Curran <kcurran@blah.com>" $From = "AD Reporting <ADReporting@blah.com>" $message = "Positive Pay Popup message script has completed `r`n`r`n" $message = $message + "please look at the attached log files for errors `r`n`r`n" $message = $message + "This script was run by " + $env:username + " on " + $env:COMPUTERNAME  $message = $message + " and ran for $([Math]::Round($ElapsedTime.TotalMinutes,0)) Minutes"  $Attachments = $TranscriptLog Send-MailMessage -From $From -To $To -SmtpServer $smtpServer ` -Subject "Popup message script completed" -Body $message -Attachments $Attachments   #notes for manual testing #$session = Get-TSSession -ComputerName server | where {$_.Username -ne ""} #If ($(Get-QADMemberOf  -Identity "$($session.Username)" -Name "Domain Admins")) {Write-Host "Member"} Else {Write-Host "not a member"} #$session | Send-TSMessage -Text "blah" -Caption "Notice"