
Part IV: Automating Administration
440
The Out-File cmdlet is used to append the bulk-newmailbox.log file with the contents of a
combination of variables that comprise the error message to be added to the log. These variables are
specified by the
InputObject parameter followed by the variables ’ names, separated by commas.
The
$blankline variable defined earlier in the script adds a line of dashes to identify the beginning of
an error message in the log. The
$errorString variable adds the error string identifying the user. The
$err[0] variable adds the specific error message generated by New-Mailbox . Note that $err is an
array, therefore to extract the error message it contains you must specify the first element in the array by
enclosing the index number (the first element is always 0) in square brackets. Finally, another
$blankline variable is used to add a line of dashes to mark the end of the error message.
After the error message is output to the log file, the
$errorTotal variable is incremented by 1 . This is
accomplished using the addition assignment operator (
+= ), which instructs Windows PowerShell to
assign a new value that is the current value plus 1.
If ($err -ne $null)
{
$errorString = “[ERROR]: Processing user $($user.name) failed with the
following error:”
Out-File $logfile -Append -Inputobject $blankLine, `
$errorString, $err[0], $blankLine