PowerShell for Windows Admins


March 11, 2012  8:40 AM

PowerShell books–March 2012

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Back in January last year I posted about my preferred set of PowerShell books. http://msmvps.com/blogs/richardsiddaway/archive/2011/01/09/powershell-books.aspx

Its time to update that list

I’ll start by removing the first edition of books where the second edition is now available. As with the previous post these are books that I’ve read or been involved with in one way or another.

NOTE: This is my own highly subjective list. There are other books available. Some I have looked at and won’t recommend; others I have not yet looked at. I am also restricting my list to English language books. I will not recommend a book I haven’t read

 

Title Author Publisher ISBN
Learn Windows PowerShell in a Month of Lunches Don Jones Manning 978161790213
PowerShell v3 in Depth. An administrator’s guide. Don Jones
Richard Siddaway
Jeffery Hicks
Manning 9781617290558
Windows PowerShell Scripting Guide Ed Wilson Microsoft Press 9780735622791
Windows PowerShell Cookbook
Second Edition
Lee Holmes O’Reilly
9780596801502
Windows PowerShell 2.0 Best Practices Ed Wilson Microsoft Press 9780735626461
PowerShell in Practice Richard Siddaway Manning 9781935182009
PowerShell and WMI Richard Siddaway Manning 9781617290114
Managing Active Directory with Windows PowerShell
Second edition
Jeffery Hicks Sapien Press 9780982131442
Managing VMware Infrastructure with Windows PowerShell Hal Rottenberg Sapien Press 0982131402
VMware vSphere Power CLI Reference Luc Dekens, Alan Renouf, Glenn Sizemore, Arnim van Lieshout and Jonathan Medd Sybex 9780470890790
PowerShell in Action
Second Edition
Bruce Payette Manning
9781935182139

 

Comments:

Learn Windows PowerShell in a Month of Lunches by Don Jones. It is a beginners guide to PowerShell. If you haven’t used PowerShell before this is the place to start. It will take you through the basics of using PowerShell from scratch. At the end of this book you will know what you are doing with PowerShell and have a good idea of how to learn more.

PowerShell v3 in Depth. An administrator’s guide by Don Jones, Jeffery Hicks and myself, is currently being written, and builds on Don’s Lunches book. It is designed to parallel and underpin the domain specific books such as PowerShell and WMI or Managing AD with Windows PowerShell. The book covers  PowerShell v2 and v3 (including all new features). Its premise is showing the administrator how to get the most out of PowerShell and how to work with it so the domain specific pieces for AD, Exchange or WMI are easy to slot into the PowerShell structure they know and understand. Expect it sometime after PowerShell v3 is available.

Windows PowerShell Scripting Guide by Ed Wilson. This takes over where Don’s book stops. It supplies a good introduction to automating basic windows admin tasks with PowerShell

Windows PowerShell Cookbook by Lee Holmes. Now in its second edition it supplies a lot of scripts for using PowerShell. This book is PowerShell orientated and doesn’t cover using Exchange, AD etc. The techniques are useful for using with some of the more advanced or technology specific books. Read this if you want to know how to do something with PowerShell – in terms of using the language for example removing members from an array

Windows PowerShell 2.0 Best Practices by Ed Wilson. Builds on his Scripting Guide and contains good information on designing and testing scripts. Even if you don’t agree with all of the ideas they are worth reading to make you think about how you want to perform these tasks in your organisation. I find my self dipping into this for Ed’s ideas. It is well worth having on your book shelf.

PowerShell in Practice I wrote as a “PowerShell for Administrators” book. Contains lots of examples for working with AD, WMI, DNS, IIS, Exchange, SQL Server and Hyper-V. I wrote it but I still refer to it for syntax & ideas. Predominantly based on PowerShell v2 it is still very applicable today.

PowerShell and WMI will be available soon. WMI is a really powerful technology but the lack of documentation and the difficulty of using it in the past has meant admins have been reluctant to use it. This book is designed to shine a light on to WMI, make it accessible and provide many ready to use scripts. It also covers the new WMI based functionality in PowerShell v3 – the CIM cmdlets and how to turn WMI classes into PowerShell cmdlets. Very relevant as much of the new PowerShell functionality in Windows Server 8 is based on that model.

Managing AD with Windows PowerShell by Jeffery Hicks. Now in its second edition. Mainly based around the Microsoft and AD cmdlets, this overlaps with PowerShell in Practice to some extent but if you just want to automate AD then start here.

Managing VMware Infrastructure with Windows PowerShell by Hal Rottenberg. If you are using VMware you need this. Admin becomes a lot easier.

VMware vSphere PowerCLI Reference by Luc Dekens et al. takes a slightly different view to Hal’s book. It is more a book about managing the whole VMware infrastructure from installation onwards. Personally I use both.

PowerShell in Action by Bruce Payette. This is the book for the in depth details on the PowerShell language. If you want to know how and why PowerShell works the way it does this is the book for you. Be aware that it is an advanced text and is NOT recommended for PowerShell newcomers.

This is my view of the PowerShell book world. No doubt other people will have different views. If you think I’ve missed a book that should be on this list please let me know but I will only recommend books I have read.

March 10, 2012  6:09 AM

Migrating to CIM–doh

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Just had the blinding glimpse of the obvious and if I use a default parameter set my problem is solved

function get-CIMRegKeyName{             
[CmdletBinding(DefaultParameterSetName="UseComputer")]             
            
param (             
 [ValidateSet("HKCR", "HKCU", "HKLM", "HKUS", "HKCC")]            
 [string]$hive="HKLM",            
            
 [string]$key = "SYSTEM\CurrentControlSet\Services",            
            
 [parameter(ParameterSetName="UseComputer")]             
 [string]$computer="$env:COMPUTERNAME",            
             
 [parameter(ParameterSetName="UseCIMSession")]             
 [Microsoft.Management.Infrastructure.CimSession]$cimsession            
             
)             
BEGIN{}#begin             
PROCESS{            
            
switch ($hive){            
"HKCR" { [uint32]$hdkey = 2147483648} #HKEY_CLASSES_ROOT            
"HKCU" { [uint32]$hdkey = 2147483649} #HKEY_CURRENT_USER            
"HKLM" { [uint32]$hdkey = 2147483650} #HKEY_LOCAL_MACHINE            
"HKUS" { [uint32]$hdkey = 2147483651} #HKEY_USERS            
"HKCC" { [uint32]$hdkey = 2147483653} #HKEY_CURRENT_CONFIG            
}            
            
switch ($psCmdlet.ParameterSetName) {            
 "UseComputer"    {$result = Invoke-CimMethod -Namespace "root\cimv2" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$hdkey; sSubKeyName =$key} -ComputerName $computer}            
 "UseCIMSession"  {$result = Invoke-CimMethod -Namespace "root\cimv2" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$hdkey; sSubKeyName =$key} -CimSession $cimsession }            
 default {Write-Host "Error!!! Should not be here" }            
}            
 $result | select -ExpandProperty sNames            
            
}#process             
END{}#end            
            
<# 
.SYNOPSIS
Displays subkey names for a registry key

.DESCRIPTION
Displays subkey names for a registry key using WSMAN or DCOM 
to access remote machines 

.PARAMETER  hive
Hive Name. One of "HKCR", "HKCU", "HKLM", "HKUS" or "HKCC"
The name is validated against the set

.PARAMETER  key
The registry key - without the hive name e.g.
"SYSTEM\CurrentControlSet\Services"

.PARAMETER  computer
Indicates to use the local machine

.PARAMETER  computer
Name of a remote computer. Connectivity will be by WSMAN.

.PARAMETER  cimsession
An object representing a cimsession. Connectivity is controlled 
by the CIM session and can be WSMAN or DCOM

.EXAMPLE                                                                                       
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -computer "."

.EXAMPLE
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -computer "server8beta"  

.EXAMPLE
$cs = New-CimSession -ComputerName server8beta   
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -cimsession $cs      

.EXAMPLE
$opt = New-CimSessionOption -Protocol Dcom                                                                                                          
$csd = New-CimSession -ComputerName server02 -SessionOption $opt                                                                                    
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -cimsession $csd  

.NOTES


.LINK

#>            
            
}


March 10, 2012  6:03 AM

Migrating to CIM-part3

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The last part of the jigsaw is to wrap the CIM functionality in a function

function get-CIMRegKeyName{             
[CmdletBinding()]             
param (             
 [ValidateSet("HKCR", "HKCU", "HKLM", "HKUS", "HKCC")]            
 [string]$hive="HKLM",            
            
 [string]$key = "SYSTEM\CurrentControlSet\Services",            
            
 [parameter(ParameterSetName="UseLocal")]             
 [switch]$local,            
              
 [parameter(ParameterSetName="UseComputer")]             
 [string]$computer,            
             
 [parameter(ParameterSetName="UseCIMSession")]             
 [Microsoft.Management.Infrastructure.CimSession]$cimsession            
             
)             
BEGIN{}#begin             
PROCESS{            
            
switch ($hive){            
"HKCR" { [uint32]$hdkey = 2147483648} #HKEY_CLASSES_ROOT            
"HKCU" { [uint32]$hdkey = 2147483649} #HKEY_CURRENT_USER            
"HKLM" { [uint32]$hdkey = 2147483650} #HKEY_LOCAL_MACHINE            
"HKUS" { [uint32]$hdkey = 2147483651} #HKEY_USERS            
"HKCC" { [uint32]$hdkey = 2147483653} #HKEY_CURRENT_CONFIG            
}            
            
switch ($psCmdlet.ParameterSetName) {            
 "UseLocal"    {$result = Invoke-CimMethod -Namespace "root\cimv2" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$hdkey; sSubKeyName =$key} }            
 "UseComputer"    {$result = Invoke-CimMethod -Namespace "root\cimv2" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$hdkey; sSubKeyName =$key} -ComputerName $computer}            
 "UseCIMSession"  {$result = Invoke-CimMethod -Namespace "root\cimv2" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$hdkey; sSubKeyName =$key} -CimSession $cimsession }            
 default {Write-Host "Error!!! Should not be here" }            
}            
 $result | select -ExpandProperty sNames            
            
}#process             
END{}#end            
            
<# 
.SYNOPSIS
Displays subkey names for a registry key

.DESCRIPTION
Displays subkey names for a registry key using WSMAN or DCOM 
to access remote machines 

.PARAMETER  hive
Hive Name. One of "HKCR", "HKCU", "HKLM", "HKUS" or "HKCC"
The name is validated against the set

.PARAMETER  key
The registry key - without the hive name e.g.
"SYSTEM\CurrentControlSet\Services"

.PARAMETER  computer
Indicates to use the local machine

.PARAMETER  computer
Name of a remote computer. Connectivity will be by WSMAN.

.PARAMETER  cimsession
An object representing a cimsession. Connectivity is controlled 
by the CIM session and can be WSMAN or DCOM

.EXAMPLE
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -local

.EXAMPLE                                                                                       
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -computer "."

.EXAMPLE
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -computer "server8beta"  

.EXAMPLE
$cs = New-CimSession -ComputerName server8beta   
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -cimsession $cs      

.EXAMPLE
$opt = New-CimSessionOption -Protocol Dcom                                                                                                          
$csd = New-CimSession -ComputerName server02 -SessionOption $opt                                                                                    
get-CIMRegKeyName -hive HKLM -key "SOFTWARE\Microsoft" -cimsession $csd  

.NOTES


.LINK

#>            
            
}

I’ve added comment based help just for completeness.

The function takes a hive and a key as parameters. I’ve given safe defaults that should be available on all machines – if they aren’t you are probably in trouble.

We can then use a switch to indicate the local machine, or give a computer name or a cim session object.

Depending on the hive the correct numeric value is set and then invoke-cimmethod is called using a computername or cim session as required

I’m not totally happy with having to use the –local switch so I’ll have another think on that


March 10, 2012  5:44 AM

Migrating to CIM-part 2

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We need to find the parameters associated with a WMI method – this information has to include the parameter and data type.  WMI also gives a name to the output variable – this becomes a property on the return object.

function get-CIMmethod {            
param (            
 [string]$namespace='root\cimv2',            
 [string]$classname='StdRegProv',            
 [string]$methodname            
)            
            
$r = Get-CimClass -Namespace $namespace -ClassName $classname             
            
if ($methodname) {            
 $r.CimClassMethods | where Name -eq $methodname |            
 foreach {            
   $_            
            
    $_ | select -ExpandProperty Parameters | Format-Table -Wrap            
  }            
}            
else {            
  $r.CimClassMethods            
}            
}

For this task we use Get-CimClass. Think of it as Get-WmiObject –List but with more bells and whistles.

The parameters are a namespace, and class name and an optional method name

The object representing the class information is put into a variable $r

If we don’t specify  a method name we get a summary listing of all methods.

If we do specify a method name we get the summary of the method and an expanded view of the parameters that includes name, type and whether its input (IN) or output (OUT)

Name            CimType Qualifiers

—-            ——- ———-

hDefKey          UInt32 {ID, IN}

sSubKeyName      String {ID, IN}

sNames      StringArray {ID, out}

The other point to note in this function is the simplified where-object syntax. Normally we would use

where {$_.Name –eq $methodname}

but in PowerShell v3 we can simplify to

where Name –eq $methodname


March 10, 2012  5:18 AM

Migrating to CIM–part 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I have created many WMI posts on this blog over the years. With PowerShell v3 a new API has been introduced and new cmdlets are available for working with WMI. The new functionality offers many improvements and advantages over using the WMI cmdlets so I thought that a series of articles showing the evolution of a piece of WMI based PowerShell might be useful.

Much of our work with WMI is based on retrieving data e.g.

Get-WmiObject -Class Win32_ComputerSystem

In PowerShell v3 this could become

Get-CimInstance -ClassName Win32_ComputerSystem

 

OK that seems simple but what about using the WMI type accelerators

$HKLM = 2147483650            
$key = "SYSTEM\CurrentControlSet\Services"            
$reg = [wmiclass]'\\.\root\default:StdRegprov'            
$subkeys = $reg.EnumKey($HKLM, $key)            
$subkeys.snames

These don’t exist in the CIM cmdlets.  What we do have is Invoke-CimMethod so we can do this

[uint32]$HKLM = 2147483650            
$key = "SYSTEM\CurrentControlSet\Services"            
Invoke-CimMethod -Namespace "root\default" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$HKLM; sSubKeyName =$key} |            
select -ExpandProperty sNames

There are a few important points we need to note:

  • The value to define the hive has to be an unsigned integer
  • we have to supply the correct argument name
  • The arguments are provided in a hash table
  • ClassName is used instead of class
  • The results are a collection that we have to expand

So how do we find this information – see part 2


March 8, 2012  1:36 PM

PowerShell web access

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Would you like to be able to access a PowerShell console in a web browser to manage your remote machines?

You would – then PowerShell web access in Windows Server 8 is just for you

Introductory details here:

http://blogs.msdn.com/b/powershell/archive/2012/03/07/introducing-windows-powershell-web-access-in-windows-server-8-beta.aspx


March 6, 2012  12:50 PM

PowerShell v3 in Depth–offer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

 

I announced the availability of PowerShell v3 in Depth on Manning’s Early Access Programme

http://msmvps.com/blogs/richardsiddaway/archive/2012/03/02/powershell-v3-in-depth.aspx

Now you have an opportunity to get this book by Don Jones, Jeff Hicks and myself at a significant bargain.  Buy from www.manning.com and get a 50% discount using the code is "PSID12". The offer is valid through Friday 9 March 2012.


March 6, 2012  12:45 PM

UK PowerShell group–March 2012

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Here are the details of the March meeting


When: Tuesday, Mar 27, 2012 7:30 PM (GMT)


Where: Virtual

*~*~*~*~*~*~*~*~*~*

PowerShell v3 introduces a new way of working with WMI plus the option of creating your own cmdlets directly from WMI classes

Notes


Richard Siddaway has invited you to attend an online meeting using Live Meeting.
Join the meeting.
Audio Information
Computer Audio
To use computer audio, you need speakers and microphone, or a headset.
First Time Users:
To save time before the meeting, check your system to make sure it is ready to use Microsoft Office Live Meeting.
Troubleshooting
Unable to join the meeting? Follow these steps:

  1. Copy this address and paste it into your web browser:
    https://www.livemeeting.com/cc/usergroups/join
  2. Copy and paste the required information:
    Meeting ID: BDFCK7
    Entry Code: rfF^w>$3X
    Location: https://www.livemeeting.com/cc/usergroups

If you still cannot enter the meeting, contact support

Notice
Microsoft Office Live Meeting can be used to record meetings. By participating in this meeting, you agree that your communications may be monitored or recorded at any time during the meeting.


March 4, 2012  2:45 PM

Upload through BITS

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Downloading through BITS is easy as we have seen,  Uploading requites some work. First the BITS IIS extensions have to be loaded.  Restart IIS and look at the properties of the Virtual Directory being used for the transfer.  At the bottom you should find a BITS upload option in the Other section.  Double click and enable uploads.  Configure other properties as desired.

A single file upload can be achieved like this

Start-BitsTransfer -Source c:\source\transfer\testupload.txt -Destination http://webr201/transfer/testupload.txt -TransferType Upload

For multiple file uploads try this

Import-Module BitsTransfer -Force            
            
$computername = "WebR201"            
$source = "c:\source\transfer\"            
            
Get-ChildItem -Path $source |            
foreach {            
             
$destination =  "http://webr201/transfer/$($_.name)"            
Write-Host "Transferring $($_.Fullname) to $destination"            
            
Start-BitsTransfer -Source $($_.Fullname) -Destination $destination -TransferType Upload            
            
}

Import the module. Set the machine name & source folder. Run Get-ChildItem over the folder and for each file set up a BITS transfer source and destination variables using the name and fullname. Run the transfer.


March 4, 2012  7:27 AM

Windows Server 8–features removed or deprecated

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

When you start your implementation planning for Windows Server 8 make sure to check with this page for functionality that has been removed or deprecated (means it will be removed in a future version)

http://technet.microsoft.com/en-us/library/hh831568.aspx


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: