Files

231 lines
9.9 KiB
PowerShell

<#
KISPoolChecker.ps1 - Horizon Pool Monitoring Script
Version: 1.5
Written by: Charlie Silverman
Purpose: Detects Horizon pools that have provisioning disabled and sends e-mail to recipients.
Command Line Options:
-credential - Configure and store Horizon server credentials. This will only store the credentials under the
current logged in user.
-test - Output results to the screen and do not send e-mail.
-help - Display command help.
#>
$ScriptName = "KISPoolChecker.ps1"
$ScriptVersion = "1.5.1"
$test = $false
$debug = $false
$now = Get-Date
$mypath = Split-Path $MyInvocation.MyCommand.Path -Parent
$configFile = $mypath + "\KISPoolChecker.cfg"
$logFile = $mypath + "\KISPoolChecker.log"
# Process command line parameters
for ( $i = 0; $i -lt $args.count; $i++ ) {
Switch ($args[$i]) {
"-credential" {
$credential = Get-Credential
$credential | Export-Clixml -path $env:USERPROFILE\User-Horizon.cred
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "Credential entry stored`r`n`r`n"
exit
}
"-smtpcredential" {
$credential = Get-Credential
$credential | Export-Clixml -path $env:USERPROFILE\User-SMTP.cred
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "Credential entry stored`r`n`r`n"
exit
}
"-test" {
$test = $true
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "Test flag set"
}
"-debug" {
$debug = $true
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "Debug flag set"
}
"-help" {
Write-Host
Write-Host
Write-Host "**********************************************************" -ForegroundColor Yellow
Write-Host "** **" -ForegroundColor Yellow
Write-Host "** KISPoolChecker.ps1 - Horizon Pool Monitoring Script **" -Foregroundcolor Yellow
Write-Host "** Version: $ScriptVersion **" -Foregroundcolor Yellow
Write-Host "** Written by: Charlie Silverman **" -Foregroundcolor Yellow
Write-Host "** **" -ForegroundColor Yellow
Write-Host "**********************************************************" -ForegroundColor Yellow
Write-Host
Write-Host "Purpose: Detects Horizon pools that have provisioning disabled and sends e-mail to recipients."
Write-Host "Command Line Options:"
Write-Host
Write-Host " -credential - Configure and store Horizon server credentials. This will only store the credentials under"
Write-Host " the current logged in user."
Write-Host " -smtpcredential - (Optional) Configure and store SMTP server credentials. This will only store the credentials"
Write-Host " under the current logged in user."
Write-Host " -test - Output results to the screen and do not send e-mail."
Write-Host " -help - Display command help."
Write-Host
Write-Host
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "Help info requested"
exit
}
}
}
# Read configuration file
try {
$inifile = Get-Content($configFile) -ErrorAction Stop | ConvertFrom-StringData -ErrorAction SilentlyContinue
} Catch {
Write-Host "Error reading from $configFile configuration file." -Backgroundcolor Red -ForegroundColor Yellow
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "ERROR: Unable to read config file $configFile`r`n`r`n"
exit
}
$orgName = $inifile.OrganizationName
$hzServer = $inifile.HorizonServer
$mailServer = $inifile.SMTPServer
$mailFrom = $inifile.SMTPFromconnec.B
$mailTo = $inifile.SMTPTo
$mailSSL = $false
$mailPort = 25
if (Test-Path Variable:\$inifile.SMTPSSL) {
if ($inifile.SMTPSSL.ToLower() -eq "true") {
$mailSSL = $true
}
if ($inifile.SMTPPort -ne $null) {
$mailPort = $inifile.SMTPPort
}
}
$logFile = $mypath + "\KISPoolChecker.log"
# Reformat email recipient list
$mtlist = $mailTo -split ","
$mailRecipients = @()
for ( $i = 0; $i -lt $mtlist.count; $i++ ) {
$mailRecipients += $mtlist[$i].Trim();
}
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "*** Script initiated $now"
# Load Credentials
$credential = import-clixml -path $env:USERPROFILE\User-Horizon.cred
try {
$mailCredential = import-clixml -path $env:USERPROFILE\User-SMTP.cred -ErrorAction Stop
} catch {
$mailCredential = $null
}
# Log into Horizon
try {
$horizon = Connect-HVServer -server $hzServer -Credential $credential
If ($debug) {
Write-Host "Connected to Horizon Server $hzServer"
}
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "Connected to Horizon Server $hzServer"
} catch {
Write-Host "ERROR: Failed to connect to Horizon Server $hzServer" -Backgroundcolor Red -ForegroundColor Yellow
If ($debug) {
Write-Host "Connected to Horizon Server $hzServer"
}
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "ERROR: Failed to connect to Horizon Server $hzServer`r`n`r`n"
}
$Services1 = $Global:DefaultHVServers.ExtensionData
# Get Horizon pool list
$pools = Get-HVPool
$results = ""
If ($debug) {
Write-Host "Inspecting pools"
}
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "Inspecting pools"
# Check each pool for enabled, automated pools with provisioning disabled
foreach ($pool in $pools) {
$poolName = $pool.Base.Name
$poolType = $pool.Type
$poolEnabled = $pool.DesktopSettings.Enabled
$provisioningEnabled = $pool.AutomatedDesktopData.VirtualCenterProvisioningSettings.EnableProvisioning
$poolError = $pool.AutomatedDesktopData.ProvisioningStatusData.LastProvisioningError
$poolErrorTime = $pool.AutomatedDesktopData.ProvisioningStatusData.LastProvisioningErrorTime
if (($poolType -eq "AUTOMATED") -and !$provisioningEnabled -and $poolEnabled) {
$results = $results + "Pool " + $poolName + " has provisioning disabled.`r`n"
$results = $results + "-- Last error: " + $poolError + "`r`n"
$results = $results + "-- Error Time: " + $poolErrortime + "`r`n"
If ($debug) {
Write-Host "Found pool $poolName with provisioning disabled"
Write-Host "-- Last error: " + $poolError
Write-Host "-- Error Time: " + $poolErrortime
}
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "Found pool $poolName with provisioning disabled"
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "-- Last error: $poolError"
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "-- Error Time: $poolErrortime"
}
}
# Output test results or send e-mail
if ($test) {
if ($results -ne "") {
Write-Host Results:
Write-Host $results
} else {
Write-Host No pools found with provisioning disabled.
}
} else {
if ($results -ne "") {
try {
$smtpCred = ""
$smtpSSL = ""
If ($mailCredential -ne $null) {
$smtpCred = " -credential $mailCredential"
}
if ($mailSSL -eq $true) {
$smtpSSL = " -UseSsl"
}
$results = $orgName + "KIS Pool Checker v" + $ScriptVersion + "`r`n`r`n" + $results
$results = $results + "`r`n`r`n" + $now
if ($debug) {
If ($mailCredential -ne $null) {
if ($mailSSL -eq $true) {
Write-Host Send-MailMessage -SmtpServer $mailServer -UseSSL -Port $mailPort -credential $mailCredential -From $mailFrom -To $mailRecipients -subject "$orgName KIS Pool Checker - Pool Provisioing Disabled" -body $results -ErrorAction Stop
} else {
Write-Host Send-MailMessage -SmtpServer $mailServer -Port $mailPort -credential $mailCredential -From $mailFrom -To $mailRecipients -subject "$orgName KIS Pool Checker - Pool Provisioing Disabled" -body $results -ErrorAction Stop
}
} else {
if ($mailSSL -eq $true) {
Write-Host Send-MailMessage -SmtpServer $mailServer -UseSSL -Port $mailPort -From $mailFrom -To $mailRecipients -subject "$orgName KIS Pool Checker - Pool Provisioing Disabled" -body $results -ErrorAction Stop
} else {
Write-Host Send-MailMessage -SmtpServer $mailServer -Port $mailPort -From $mailFrom -To $mailRecipients -subject "$orgName KIS Pool Checker - Pool Provisioing Disabled" -body $results -ErrorAction Stop
}
}
}
If ($mailCredential -ne $null) {
if ($mailSSL -eq $true) {
Send-MailMessage -SmtpServer $mailServer -UseSSL -Port $mailPort -credential $mailCredential -From $mailFrom -To $mailRecipients -subject "$orgName KIS Pool Checker - Pool Provisioing Disabled" -body $results -ErrorAction Stop
} else {
Send-MailMessage -SmtpServer $mailServer -Port $mailPort -credential $mailCredential -From $mailFrom -To $mailRecipients -subject "$orgName KIS Pool Checker - Pool Provisioing Disabled" -body $results -ErrorAction Stop
}
} else {
if ($mailSSL -eq $true) {
Send-MailMessage -SmtpServer $mailServer -UseSSL -Port $mailPort -From $mailFrom -To $mailRecipients -subject "$orgName KIS Pool Checker - Pool Provisioing Disabled" -body $results -ErrorAction Stop
} else {
Send-MailMessage -SmtpServer $mailServer -Port $mailPort -From $mailFrom -To $mailRecipients -subject "$orgName KIS Pool Checker - Pool Provisioing Disabled" -body $results -ErrorAction Stop
}
}
If ($debug) {
Write-Host "E-mail sent successfully to $mailTo"
}
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "E-mail sent successfully to $mailTo"
} catch {
Write-Host "Error: Unable to send e-mail." -Backgroundcolor Red -ForegroundColor Yellow
Write-Host $_.Exception.Message -Backgroundcolor Red -ForegroundColor Yellow
If ($debug) {
Write-Host "ERROR: Unable to send e-mail`r`n"
}
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "ERROR: Unable to send e-mail`r`n`r`n"
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject $_.Exception.Message
}
} else {
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "No pools with issues found. No e-mail sent.`r`n`r`n"
}
}
$now = Get-Date
Out-File -FilePath $logFile -Encoding ASCII -Append -InputObject "*** Script completed $now`r`n`r`n"