Database Reference
In-Depth Information
How it works...
This script deines script level parameters that control what directory the results will be
output to and what commission percentage threshold must be exceeded for an employee
to be included in the results. To be explicit, the script sets $ErrorActionPreference to
Continue (the default). This behaviour results in non-terminating errors being output and
script execution continuing; other valid values are SilentlyContinue , Inquire , and Stop .
Likewise $WarningPreference can be set the same way for handling warnings. Preferences
can be overridden when invoking cmdlets as follows: Remove-Item $outputFilename
-Force -ErrorAction SilentlyContinue .
At the bottom of the script, all of the processing code (mostly Invoke-Script ) is enclosed in
a try/catch block so we can respond to any error and stop any transcription that was started
regardless of success or failure. Transcription automatically sends all output of the script
to a speciied ile without having to explicitly direct output for each command to a ile. Since
transcription is not supported in the ISE, the function Get-CanTranscribe (in Utilities.
ps1 ) checks the host name and doesn't use transcription if running from the ISE (useful
while debugging). In the catch block the built-in $_.Exception and $_.InvocationInfo
variables are used to format the error message. The message is written to error output with
Write-Error and a non-zero Exit statement is used so the scheduled task will report a
failure when the script fails.
In the Invoke-Script function, Set-HostSettings is called to expand the host line
length from 80 to 120 characters. This is done so the output doesn't wrap as often in the
transcription log. Since this isn't critical, an empty catch block supresses any errors. Next,
the function throws an error if a valid directory was not passed in with the $outputPath
script parameter. The next error check comes with the following:
$sql = (Get-Content $sqlFilename -EV err) | Out-String
if ($err) {
Write-Warning "SQL read failed; bailing"; throw $err
}
This uses the -ErrorVariable argument ( EV for short) to send any error to the speciied
variable, the $err variable in this case. Note that -EV err is used and not -EV $err . This
technique is another error handling approach that can be used on a cmdlet by cmdlet basis.
Here we turn any non-critical error reading the SQL from a ile into both a warning and a
critical error.
Finally, we surround executing the SQL query ( Get-ODPDataTable ) with a try/catch block
to ensure the connection gets closed regardless of the success or failure of the SQL query.
The other important aspect of this script is output that's written every step of the way,
indicating what is about to happen and the result of what was just executed. This is critical
for troubleshooting since the script runs unattended in the background.
 
Search WWH ::




Custom Search