Get-CitrixHotfix: The Bitter/Sweet of Write-Verbose
There are a whole host of of Write-* Cmdlets.
Write-Debug
Write-Error
Write-Host
Write-Output
Write-Progress
Write-Verbose
Write-Warning
Each one of these are useful, but I want to specifically talk about Write-Verbose. Note, Write-Debug work basically the same. Also, for those of you that follow the Powershell Podcast over at http://powerscripting.wordpress.com some of this was covered already in Episode 11.
The way Write-Verbose works is that it uses $VerbosePreference to determine what to do. This is very useful because it gives you the ability to easily control if the string is written to the host. I think this is a good time to clarify that write-verbose only writes to the host and does NOT pollute the output stream which is SUPER useful.
Lets point out the goods and bads
Goods
1) Can use a switch Parameter to easily control the console info (I normally use $verbose)
2) It writes to host so you dont pollute the object output
3) Nice for writing Data to the console on scripts that take a long time to run
Bads
1) It has a header on every line that cannot be removed “VERBOSE:”
2) It is Yellow (eek!)
The good news is you can control the color using $host.PrivateData
More Info http://ps1.soapyfrog.com/2007/01/29/debug-and-verbose-colouring/
The following is a script that I wrote that collects Citrix Hotfixes. I have a large number of servers so I wanted to be able see where I was and I also wanted a overview of the hotfixes, but I also wanted to output and object for filtering purposes. In this case Write-Verbose was perfect. I was able to write to the screen (host) what I wanted to see without changing the object output. I was also able to control whether it was outputed by using a switch parameter to control $VerbosePreference.
# Get-CitrixHotfix.ps1
if($Verbose){$VerbosePreference = "Continue"}
if($debug){Set-PSDebug -Step}
Write-Host "`nProcessing…`n"
if($Farm)
{
# Get Farm Object
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$Server)
$CTXFarm = [system.Activator]::CreateInstance($type)
$CTXFarm.Initialize(1)
# Creating Collection for Custom Objects
$myCol = @()
foreach($Srv in $CTXFarm.Servers)
{
# Create Custom Object
$myobj = "" | Select-Object Name,Hotfix
$myobj.Name = $Srv.ServerName
$myobj.Hotfix = @()
Write-Verbose $Srv.ServerName
# Get Hotfix Information for the Server and add to Custom Object
$CTXServer = $CTXFarm.GetServer2(6,$Srv.ServerName)
$CTXServer.winServerObject2.hotfixes | %{Write-Verbose " - $($_.Name)";$myobj.HotFix += $_.name}
# Add Server Object to Collection
$myCol += $myobj
}
# Output Collection
if($log)
{
@(foreach($obj in $mycol)
{
Write-Output $obj.Name
foreach($hf in $obj.Hotfix){write-Output " - $hf"}
}) | out-File $log -enc ASCII
}
else
{
$mycol
}
}
else
{
$myobj = "" | Select-Object Name,HotFix
$myobj.Name = $Server
$myobj.HotFix = @()
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeServer",$Server)
$mfServer = [system.Activator]::CreateInstance($type)
$mfServer.Initialize(6,$Server)
Write-Output $Server
$mfServer.winServerObject2.hotfixes | foreach-Object{$myobj.HotFix += $_.Name}
$myobj
}
write-host
if($debug){Set-PSDebug -off}
tshell :: Oct.30.2007 :: Citrix, ErrorHandling, HowTo, Powershell, Scripting, functions :: No Comments »
