Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Windows PowerShell Best Practices
Dedication
Foreword
Introduction
Who is this book for?
How is this book organized?
System requirements
The companion website
Acknowledgements
Support & feedback
Errata
We want to hear from you
Stay in touch
I. Understanding the basics of Windows PowerShell
1. Survey of Windows PowerShell capabilities
Understanding Windows PowerShell
Installing Windows PowerShell
Deploying Windows PowerShell
Using cmdlets
Using command-line utilities
Security issues with Windows PowerShell
Controlling execution of Windows PowerShell cmdlets
Confirming commands
Suspending confirmation of cmdlets
Working with Windows PowerShell
Accessing Windows PowerShell
Configuring Windows PowerShell
Supplying options for cmdlets
Working with the help options
Updating help information
Discovering information in help
Additional resources
2. Using the CIM cmdlets
Using the CIM cmdlets to explore WMI classes
Using the classname parameter
Finding WMI class methods
Filtering classes by qualifier
Retrieving WMI instances
Reduce returned properties and instances
Clean up output from the command
Working with Association classes
Additional resources
II. Planning for scripting
3. Using the Active Directory module
Understanding the Active Directory module
Installing the Active Directory module
Getting started with the Active Directory module
Using the Active Directory module
Finding the FSMO role holders
Documenting Active Directory
Renaming Active Directory sites
Managing users
Creating a user
Finding and unlocking AD user accounts
Finding disabled users
Finding unused user accounts
Additional resources
4. Identifying scripting opportunities
Automating routine tasks
Automation interface
Using RegRead to read the registry
Using WMI to read the registry
Using .NET to read the registry
Using intrinsic Windows PowerShell techniques
Structured requirements
Security requirements
Detecting the current user
Detecting the user role
.NET Framework version requirements
Operating system requirements
Application requirements
Module requirements
Additional resources
5. Configuring the script environment
Configuring a profile
Creating aliases
Creating functions
Overriding existing commands
Aliasing the function
Looping the array
Passing multiple parameters
Multiple parameters with $args
Multiple named parameters
Creating variables
Creating PSDrives
Enabling scripting
Creating a profile
Choosing the correct profile
Creating other profiles
Accessing functions in other scripts
Creating a function library
Using an include file
Additional resources
6. Avoiding scripting pitfalls
Lack of cmdlet support
Complicated constructors
Version compatibility issues
Trapping the operating system version
Lack of WMI support
Working with objects and namespaces
Listing WMI providers
Working with WMI classes
Changing settings
Modifying values through the registry
Lack of .NET Framework support
Use of static methods and properties
Version dependencies
Lack of COM support
Lack of external application support
Additional resources
7. Tracking scripting opportunities
Evaluating the need for the script
Reading a text file
Export command history
Fan-out commands
Query Active Directory
Using [ADSISearcher]
Using Active Directory cmdlets
Just use the command line
Calculating the benefit from the script
Repeatability
Documentability
Adaptability
Script collaboration
Additional resources
III. Designing the script
8. Designing the script
Understanding functions
Using functions to provide ease of code reuse
Using two input parameters
Using a type constraint
Using more than two input parameters
Using functions to encapsulate business logic
Using functions to provide ease of modification
Understanding filters
Additional resources
9. Designing help for scripts
Adding help documentation to a script with single-line comments
Working with temporary folders
Using multiple-line comment tags in Windows PowerShell 4.0
Creating multiple-line comments with comment tags
Creating single-line comments with comment tags
Using comment-based help
The 13 rules for writing effective comments
Update documentation when a script is updated
Add comments during the development process
Write for an international audience
Consistent header information
Document prerequisites
Document deficiencies
Avoid useless information
Document the reason for the code
Use of one-line comments
Avoid end-of-line comments
Document nested structures
Use a standard set of keywords
Document the strange and bizarre
Additional resources
10. Designing modules
Understanding modules
Locate and load modules
Listing available modules
Loading modules
Install modules
Creating a modules folder
Working with the $modulePath variable
Creating a module drive
Checking for module dependencies
Using a module from a share
Creating a module
Additional resources
11. Handling input and output
Choosing the best input method
Reading from the command line
Using the $args automatic variable
Supplying multiple values to $args
Using the Param statement
Creating a mandatory parameter
Using parameter attributes
Creating a parameter alias
Validating parameter input
Using multiple parameter arguments
Working with passwords as input
Store the password in the script
Store the password in a text file
Store the password in the registry
Store the password in Active Directory Domain Services
Prompt for the password
Working with connection strings as input
Prompting for input
Choosing the best output method
Output to the screen
Output to file
Splitting the output to both the screen and the file
Output to email
Output from functions
Avoid populating the global variable
Using a namespace in the global variable
Additional resources
12. Handling errors
Handling missing parameters
Creating a default value for the parameter
Detecting the missing value and assigning it in the script
Assigning the value in the Param statement
Making the parameter mandatory
Limiting choices
Using PromptForChoice to Limit Selections
Using ping to identify accessible computers
Using the −contains Operator to examine the contents of an array
Using the −contains operator to test for properties
Handling missing rights
Attempting and failing
Checking for rights and exiting gracefully
Using #Requires
Require Administrator rights
Requiring specific modules
Handling missing WMI providers
Incorrect data types
Out of bounds errors
Using a boundary checking function
Placing limits on the parameter
Additional resources
13. Testing scripts
Using basic syntax checking techniques
Looking for errors
Running the script
Documenting what you did
Conducting performance testing of scripts
Using the store and forward approach
Using the Windows PowerShell pipeline
Comparing the speed of two scripts
Reducing code complexity
Evaluating the performance of different versions of a script
Command-line parameters
Functions
Displaying the results and creating the log file
Using standard parameters
Using the debug parameter
Using the Verbose parameter
Using the whatif parameter
Using Start-Transcript to produce a log
Advanced script testing
Additional resources
14. Documenting scripts
Getting documentation from help
Getting documentation from comments
Using the AST parser
Additional resources
IV. Deploying the script
15. Managing the execution policy
Selecting the appropriate script execution policy
The purpose of script execution policies
Understanding the different script execution policies
Understanding the Internet zone
Deploying the script execution policy
Modifying the registry
Using the Set-ExecutionPolicy cmdlet
Using the Set-ExecutionPolicy cmdlet on a local computer
Using the Set-ExecutionPolicy cmdlet via a logon script
Using Group Policy to deploy the script execution policy
Understanding code signing
Additional resources
16. Running scripts
Logon scripts
What to include in logon scripts
Methods of calling the logon scripts
Script folder
Deploy locally
Deploy an MSI package locally
Stand-alone scripts
Diagnostics
Reporting and auditing
Help desk scripts
Avoid editing
Provide a good level of help interaction
Additional resources
17. Versioning scripts
Why version control?
Avoid introducing errors
Enable accurate troubleshooting
Track changes
Maintain a master listing
Maintain compatibility with other scripts
Internal version number in the comments
Incrementing version numbers
Tracking changes
Version control software
Additional resources
18. Logging results
Logging to a text file
Designing a logging approach
Overwriting the log
Appending to the log
Using the Out-File cmdlet
Text location
Networked log files
Writing directly to the file
Writing to the local file and copying to the network
Logging to the event log
Using the Aapplication log
Creating a custom event log
Logging to the registry
Additional resources
19. Troubleshooting scripts
Understanding debugging in Windows PowerShell
Working with syntax errors
Working with runtime errors
Working with logic errors
Using the Set-PSDebug cmdlet
Tracing the script
Working with trace level 1
Working with trace level 2
Stepping through the script
Enabling strict mode
Using Set-PSDebug -strict
Using the Set-StrictMode cmdlet
Debugging scripts
Setting breakpoints
Setting a breakpoint on a line number
Setting a breakpoint on a variable
Setting a breakpoint on a command
Responding to breakpoints
Listing breakpoints
Enabling and disabling breakpoints
Deleting breakpoints
Additional resources
20. Using the Windows PowerShell ISE
Running the Windows PowerShell ISE
Navigating the Windows PowerShell ISE
Working with the script pane
Tab expansion and IntelliSense
Working with Windows PowerShell ISE snippets
Using Windows PowerShell ISE snippets to create code
Creating new Windows PowerShell ISE snippets
Removing user-defined Windows PowerShell ISE snippets
Additional resources
21. Using Windows PowerShell remoting and jobs
Understanding Windows PowerShell remoting
Classic remoting
WinRM—Windows Remote Management
Create a remote Windows PowerShell session
Run a single Windows PowerShell command
Using Windows PowerShell jobs
Additional resources
22. Using Windows PowerShell Workflow
Why use Windows PowerShell Workflow
Workflow requirements
A simple workflow
Parallel PowerShell
Workflow activities
Windows PowerShell cmdlets as activities
Disallowed core cmdlets
Non-automatic cmdlet activities
Parallel activities
Checkpointing a Windows PowerShell workflow
Understanding checkpoints
Placing checkpoints
Adding checkpoints
Workflow checkpoints are free
Checkpoint activity
Using the CheckPoint-Workflow activity
Adding a sequence activity to a workflow
Additional resources
23. Using the Windows PowerShell DSC
Understanding Desired State Configuration
The DSC process
Configuration parameters
Setting dependencies
Configuration data
Creating users with the user provider
Creating groups with the group provider
Controlling configuration drift
Additional resources
A. About the author
Index
About the Author
Copyright
← Prev
Back
Next →
← Prev
Back
Next →