{"id":421,"date":"2015-08-11T22:52:07","date_gmt":"2015-08-11T20:52:07","guid":{"rendered":"http:\/\/blog.appcloud.ch\/?p=421"},"modified":"2017-03-12T10:15:19","modified_gmt":"2017-03-12T09:15:19","slug":"script-who-alert-me-if-we-are-running-out-of-pooled-desktops","status":"publish","type":"post","link":"https:\/\/blog.sachathomet.ch\/de\/2015\/08\/11\/script-who-alert-me-if-we-are-running-out-of-pooled-desktops\/","title":{"rendered":"PoSh Script who alert me if I&#8217;m running out of pooled Desktops"},"content":{"rendered":"<p>Just a small one, last days I&#8217;ve created a small script who alert me if I&#8217;m running out of pooled Desktops.<\/p>\n<p>If you run this script at regular intervals you will receive an email as soon you have less desktops free than you defined as threshold:<\/p>\n<p><a href=\"http:\/\/blog.sachathomet.ch\/wp-content\/uploads\/2015\/08\/LowDesktopFree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-423\" src=\"http:\/\/blog.sachathomet.ch\/wp-content\/uploads\/2015\/08\/LowDesktopFree.png\" alt=\"LowDesktopFree\" width=\"821\" height=\"412\" srcset=\"https:\/\/blog.sachathomet.ch\/wp-content\/uploads\/2015\/08\/LowDesktopFree.png 821w, https:\/\/blog.sachathomet.ch\/wp-content\/uploads\/2015\/08\/LowDesktopFree-300x151.png 300w, https:\/\/blog.sachathomet.ch\/wp-content\/uploads\/2015\/08\/LowDesktopFree-768x385.png 768w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/a><\/p>\n<pre class=\"brush: powershell; collapse: true; light: false; title: You can click here to see the Script code ... ; toolbar: true; notranslate\" title=\"You can click here to see the Script code ... \">\r\n\r\n#==============================================================================================\r\n# Created on: 08.2015 Version: 0.2\r\n# Created by: Sacha Thomet\r\n# File name: Citrix-XenDesktop-Alert-low-free-desktops.ps1\r\n#\r\n# Description: Check for Free Desktops in DeliveryGroups\r\n#\r\n# Prerequisite: None\r\n#\r\n# Call by : Scheduled Task e.g every 10 minutes\r\n#\r\n# Changelog:\r\n# V0.1 Initial Version, create report file from array FreeDesktopReport and attach this to the email.\r\n# V0.2 Change from txt-file to formatted HTML-Mail\r\n#\r\n#==============================================================================================\r\nif ((Get-PSSnapin &amp;quot;Citrix.Common.Commands&amp;quot; -EA silentlycontinue) -eq $null) {\r\ntry { Add-PSSnapin Citrix.* -ErrorAction Stop }\r\ncatch { write-error &amp;quot;Error Citrix.* Powershell snapin&amp;quot;; Return }\r\n}\r\n# Change the below variables to suit your environment\r\n#==============================================================================================\r\n\r\n# Variables what should be changed according your environment and wishes\r\n\r\n$DeliveryGroups = @(&amp;quot;Win7-Desktops&amp;quot;,&amp;quot;Win10-Desktops&amp;quot;)\r\n$minDesktops = 10\r\n$directoraddress=&amp;quot;http:\/\/citrixdirector.mycompany.ch&amp;quot;\r\n$EnvironmentName=&amp;quot;Production XenDesktop&amp;quot;\r\n\r\n# E-mail report details\r\n$emailFrom = &amp;quot;citrix@mycompany.ch&amp;quot;\r\n$emailTo = &amp;quot;citrix@mycompany.ch&amp;quot;\r\n$smtpServer = &amp;quot;mailrelay.mycompany.ch&amp;quot;\r\n\r\n#=======DONT CHANGE BELOW HERE =======================================================================================\r\n\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;!DOCTYPE html&amp;amp;gt;&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;html&amp;amp;gt;&amp;quot;\r\n\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;head&amp;amp;gt;&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;style&amp;amp;gt;&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;BODY{background-color:#fbfbfb; font-family: Arial;}&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; width:60%; }&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black; text-align:left;}&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;\/style&amp;amp;gt;&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;\/head&amp;amp;gt;&amp;quot;\r\n\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;body&amp;amp;gt;&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;This is the Low-Desktop-Alert for $EnvironmentName, if you receive this mail the value of free desktops is below the configured threshold of $minDesktops desktops! &amp;amp;lt;br&amp;amp;gt;&amp;amp;lt;br&amp;amp;gt;&amp;quot;\r\n\r\n$FreeDesktopReport = @()\r\n\r\nforeach($dg in $DeliveryGroups)\r\n{\r\n$desktops = Get-BrokerDesktopGroup | where {$_.Name -eq $dg }\r\n$CurrentDeliveryGroup = &amp;quot;&amp;quot; | Select-Object Name, Alert, DesktopsAvailable\r\n\r\n# Write Array Values\r\n$CurrentDeliveryGroup.Name = $dg\r\n\r\n$CurrentDeliveryGroup.DesktopsAvailable = $desktops.DesktopsAvailable\r\n\r\nif ($desktops.DesktopsAvailable -lt $minDesktops )\r\n{\r\nWrite-Host &amp;quot;Number of free desktops to low for DeliveryGroup $dg, sending email&amp;quot;\r\n# Add Line to Report\r\n$CurrentDeliveryGroup.alert = &amp;quot;True&amp;quot;\r\n}\r\n\r\n$FreeDesktopReport += $CurrentDeliveryGroup\r\n}\r\n\r\n$mailbody += $FreeDesktopReport | ConvertTo-Html\r\n$mailbody += &amp;quot;&amp;amp;lt;br&amp;amp;gt;&amp;amp;lt;br&amp;amp;gt;Launch Citrix Studio or browse to &amp;amp;lt;a href=$directoraddress&amp;amp;gt;Citrix Director&amp;amp;lt;\/a&amp;amp;gt; see more information about the current Desktop usage&amp;amp;lt;br&amp;amp;gt;&amp;quot;\r\n\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;body&amp;amp;gt;&amp;quot;\r\n$mailbody = $mailbody + &amp;quot;&amp;amp;lt;\/html&amp;amp;gt;&amp;quot;\r\n\r\n# If any record raises an alert, send an email.\r\nif (($FreeDesktopReport | where {$_.alert -eq &amp;quot;True&amp;quot;}) -ne $null) {Send-MailMessage -to $emailTo -from $emailFrom -subject &amp;quot;********* Low free Desktop Alert for $EnvironmentName *********&amp;quot; -Body $mailbody -BodyAsHtml -SmtpServer $smtpServer }\r\n<\/pre>\n<p>Or download the Script <a href=\"http:\/\/blog.sachathomet.ch\/?wpdmdl=425\" target=\"_blank\">here <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a small one, last days I&#8217;ve created a small script who alert me if I&#8217;m running out of pooled Desktops. If you run this script at regular intervals you will receive an email as soon you have less desktops free than you defined as threshold: #============================================================================================== # Created on: 08.2015 Version: 0.2 # Created [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,1],"tags":[14,32,46,47],"class_list":["post-421","post","type-post","status-publish","format-standard","hentry","category-scripts","category-uncategorized","tag-citrix","tag-powershell","tag-xendesktop","tag-xendesktop-7-6"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"de","enabled_languages":["en","de"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"de":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/posts\/421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/comments?post=421"}],"version-history":[{"count":2,"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":948,"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/posts\/421\/revisions\/948"}],"wp:attachment":[{"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/media?parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/categories?post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sachathomet.ch\/de\/wp-json\/wp\/v2\/tags?post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}