Microsoft Windows Vista and Server 2008 have become a lot more protective of certain areas of the registry and the file system. Take an innocent little script that create a URL shortcut in a folder under C:\Program Files. Not really a problem under XP, but under Window Vista this will fail: Fortunately Windows Vista […]
Category: Howto
Writing an LDAP Search Filter
http://confluence.atlassian.com/display/DEV/How+to+write+a+LDAP+search+filter is a great article on writing LDAP search filters.
Single quotes, not double
Use ‘single quotation marks’ around string literals unless you explicitly need variable expansion. In PowerShell, single quotes are not expanded into variables: $var = ‘Hello’ $result = ‘$var $var’ # $result contains ‘$var $var’ Double quotes are: $var = ‘Hello’ $result = “$var $var” # $result contains ‘Hello Hello’ To avoid unintended variable expansion, use […]
Emit custom objects, rather than text, from functions and scripts
Whenever possible, functions (especially) and scripts (to a lesser degree) should emit custom objects, not just text. This ensures that the output of these constructs can be piped to other cmdlets, like Where-Object, Sort-Object, and so forth, further extending PowerShell’s capabilities. To create a custom object: $obj = New-Object PSObject To add properties to the […]
Formatting in scripts
Develop a standard for formatting your scripts, and use indentation. Function MyFunction { # function code here } …or… Function MyFunction { # function code here }
Avoid the use of aliases in scripts
Aliases are harder to read and maintain over the long term – so try and avoid using them in a script. If you use PrimalScript, just select Edit > Convert > Alias to Cmdlet to turn all aliases into their full cmdlet names, providing easier long-term maintenance and readability for your script.
Don’t modify out-of-scope items
Scripts, functions, and other elements should NEVER modify the variables, aliases, PSDrives, or other items from parent scopes. Instead, return data to the parent scope using the pipeline. Changing parent scope items creates code which is much more difficult to debug and maintain, and can create inconsistent results when run on different systems.
How do I sign a Windows PowerShell script?
First you’ll need a code-signing certificate. If you purchase one, you’ll be looking for a “Class III” digital certificate of the “Microsoft Authenticode” variety. This will often come in two parts: An SPC file, which is the Software Publishing Certificate, and a PVK file, which is the corresponding private key. If you use a utility […]
Running command-line tools
These examples show how to run command-line tools from within a VBScript, capturing the tool’s output. ‘ ‘ ScriptingAnswers.com Essentials – by Don Jones ‘ ‘ Run cmdline tools ‘ This shows how to launch a command-line ‘ tool and retrieve its output into a string variable ‘ to then do something with it. For […]
Checking for group membership
Here’s a script that demonstrates how to check for group membership from within VBScript. http://www.rlmueller.net/IsMember8.htm