Databases Reference
In-Depth Information
Comments
Be generous when writing comments. Make sure you have a standard comment heading, such as the
following:
# =========================================================
#
# NAME: MonitorDeadlocks.ps1
#
# AUTHOR: Yan and MAK
# DATE : 5/1/2008
#
# COMMENT: This script collects the information about threads and resources involved
in deadlocks.
# =========================================================
Display
When you display a message, make sure you follow a proper format. The following is an example:
YYYY-MM-DD HH:MM:SS error/Information/Warning Message
Variable Naming Convention
Variable names should be plain English and something related to your program's functionality. Do not,
for example, name a variable $a, $marilynmonroe . Use proper casing for variable names, and declare
explicitly the data type for each variable — for example, [string] $MyHost . If necessary, suffix variables
with comments, as shown in the following example:
[string] $Squotes = "'" # -- Set variable for embedded Single Quote.
[string] $Dquotes = '"' # -- Set variable for embedded Double Quote.
Try to define and use more functions rather than call a script from another script if the code is reusable.
Exception Handling
Try to set the following variable globally on the top of the script and handle all the exceptions within the
script after major cmdlets:
$erroractionpreference = "SilentlyContinue"
There are three ways you could handle exceptions. Based on the requirements, you could use one of the
following methods:
Method 1:
Using Errvariable named parameter and ErrorAction named parameter. Example
$AFSwmi = get-wmiobject -class Win32_service -computername $hostname -Errorvariable
ERR -ErrorAction Silentlycontinue
Search WWH ::




Custom Search