I'm using a Powershell script to pull a list of servers in an Active Directory domain, poll them to determine if the SMTP-Server role is installed, and then output that list to the display in several ways.
I want to format the output of the script to use colors based on the values.
I'm creating a custom PSObject with the output, which consists of two properties simply named "Name" and "IsInstalled". Name is a string field and IsInstalled is a Boolean value. I want both values to display red if the IsInstalled value is False, and to display green if it's True.
import-module ServerManager
import-module ActiveDirectory
$Computers = Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (OperatingSystem -notlike "*2003*") -and (Enabled -eq "True")} -Properties Name,OperatingSystem | Select Name | Sort-Object -Property Name #| Select-Object -First 5
$Present = ""
$YesCount = $null
$Results = @()
$Count = 0
ForEach ($Computer in $Computers)
{
$Name = $Computer.Name
$SMTP = Get-WindowsFeature "smtp-server"
$IsInstalled = $null
if($SMTP.Installed)
{
Write-host "SMTP is installed on $Name"
$Present = "True"
$IsInstalled = $True
$YesCount++
}
else
{
Write-host "Not on $Name"
$IsInstalled = $False
}
$object = New-Object -TypeName PSObject
$object | Add-Member -MemberType NoteProperty -Name Name -Value $Name
$object | Add-Member -MemberType NoteProperty -Name IsInstalled -Value $IsInstalled
$Results += $object
$Count++
}
if (($Present = "True"))
{
Write-host ""
Write-host "Checked $Count machines, and SMTP is installed on no servers!"
}
else
{
Write-host "Checked $Count machines, and SMTP is installed on $YesCount servers!"
}
$Results |Select Name,IsInstalled | Sort-Object Name | Format-Table -AutoSize
I can use Sort-Object and Format-Table to control the output sorting and layout, but I can't figure out how to change the text color based on the value. Help?