Introduction

I just spent the past week at InfoPoint taking Microsoft course 10135A Automating Administration with Windows PowerShell 2.0.  Although I have worked with PowerShell before, I decided to take this course in hopes to discover things about the scripting language that I may not already know.  The topics discussed in this post are those that I found to be either interesting or helpful.

Active Directory

On Domain Controllers running Windows Server 2008 R2, the Active Directory PowerShell module is already installed.  The cmdlets within the Active Directory module communicate with a web service running on the server.  This particular service is not installed on servers running Windows Server 2003.  However, it can be downloaded and installed from here: http://bit.ly/htxf8s.

To add the Active Directory module do:

Import-Module ActiveDirectory

To retrieve the list of commands available for use with the module do:

Get-Command -Module ActiveDirectory

Server Manager

It’s also interesting to note that Server Manager can be administered via a PowerShell module as well.  You can add, get and remove Windows Features using the included cmdlets.

To add the Server Manager module do:

Import-Module ServerManager

To retrieve a list of commands available for use with the module do:

Get-Command -Module ServerManager

WinRM

The following are a list of interesting characteristics of WinRM.

  • WinRM is a Microsoft implementation of Web Services for Management, or WS-MAN.
  • WinRM communicates via the Hypertext Transport Protocol, or HTTP.
  • For security purposes, WinRM is disabled by default and must be enabled.  It can be enabled locally on a single computer or it can be enabled and configured by means of a Group Policy object (GPO).

The easiest way to configure WinRM for remote shell is by running the Enable-PSRemoting cmdlet.

Implicit Remoting

The Active Directory cmdlets can not be used directly on Windows XP, which is the version of Windows that I am currently running (it’s a long story).  However, implicit remoting can be used to access cmdlets running on the Windows Server 2008 R2 server.  The cmdlets will behave as if they are installed on the local computer but will execute on the remote computer.

1-to-1 Shell

This shell enables a user to get a remote command-line prompt (ex. SSH).  As the end user, you are using the remote computers session.  This is done by executing:

Enter-PSSession -ComputerName [remote_computer_name]

1-to-Many Remoting

This is handy when you need to run one command on multiple computers.  Please note that, the shell will not try to contact more than 32 computers at once.  This type of remoting is done by executing:

Invoke-Command -ScriptBlock { script_block } -ComputerName [remote_1], [remote_2], [remote_3]...

Profile Locations

Windows PowerShell profiles can be stored in 1-of-6 locations.

  • Current user in $Home\[My ]Documents\profile.ps1
  • All users in $PsHome\Profile.ps1
  • Current user console in $Home\[My ]Documents\WindowsPowerShell\profile.ps1
  • All users console in $PsHome\Microsoft.PowerShell_Profile.ps1
  • Current user for ISE in $Home\[My ]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
  • All users for ISE in $PsHome\Microsoft.PowerShellISE_profile.ps1

Script Modules

Script modules are the preferred method for reusing scripts and functions.  They should be stored in one of the predefined locations.  These predefined locations are where the shell searches recursively for modules.  To see these locations, run this command:

dir env:\PsModulePath

A module is simply a normal shell script with a .psm1 file extension.

Comment-Based Help

With Comment-based help a set of specially formatted comments are used to define various sections of the help, such as parameters, usage examples, and so forth.  The most commonly used help sections are: SYNOPSIS, DESCRIPTION, PARAMETER and EXAMPLE.

For example:

<#
.SYNOPSIS
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.

.DESCRIPTION
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.
#>

In Closing

PowerShell is a cool scripting language that will assist me greatly in automating tasks within my current environment.  As a Systems Administrator I don’t have a lot of time so the more I can automate mundane tasks the better.