Monthly Archive

Categories

Monthly Archives: January 2011

Live Meeting with Microsoft Scripting Guy

Ed Wilson – the Microsoft Scripting Guy – will be presenting a Live Meeting on PowerShell Best Practices. Tuesday 11 January 2011 @ 7.30 GMT

Details and link to Live meeting from http://richardspowershellblog.wordpress.com/2010/12/29/powershell-uk-user-group-january-2011/

PowerShell Books

There are a lot of PowerShell books on the market. Whether you are just starting  with PowerShell, want to dig further into the subject or need some help with using PowerShell with a particular product it is often helpful to find a book on the subject instead of learning from scratch.

NOTE: This is my own personal view of the books. 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

These are the books I would recommend. I use many of these on a regular basis.

Title Author Publisher ISBN
Learn Windows PowerShell in a Month of Lunches Don Jones Manning 978161790213
Windows PowerShell Scripting Guide Ed Wilson Microsoft Press 9780735622791
Windows PowerShell Cookbook
First Edition
Second Edition
Lee Holmes O’Reilly
9780596528492
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 Jeffery Hicks Sapien Press 0977659798
Managing VMware Infrastructure with Windows PowerShell Hal Rottenberg Sapien Press 0982131402
PowerShell in Action
First Edition
Second Edition
Bruce Payette Manning  
1932394-90-7
9781935182139

 

Comments:

Learn Windows PowerShell in a Month of Lunches by Don Jones. This is still being written at the time of posting. It is a beginners guide to PowerShell. If you haven’t used PowerShell before this is the place to start.

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.

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.

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.

PowerShell and WMI is still being written. 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

Managing AD with Windows PowerShell by Jeffery Hicks. A second edition is in preparation. 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.

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 one this list please let me know but I will only recommend books I have read.

File splitter update

I said last time that I wanted to make the file splitter function a bit more sophisticated. I would like to be able to wrap the distribution so it goes file 1,2,3,3,2,1,1,2..  etc

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
function split-file {
[CmdletBinding()]
param (
  [string]$file="data.txt",
  [string[]]$outfiles,
  [switch]$wrap
) 

 if (!(Test-Path $file)){throw "File $file not found"}
 
## delete previous files
 $num = $outfiles.count
 foreach ($ofile in $outfiles) {
  if (Test-Path $ofile){Remove-Item $ofile}
 }
 
 $list = @()
 if ($wrap) {
    0..$($num-1) | foreach { $list += $_ }
    $($num-1)..0 | foreach { $list += $_ }
    $numwrap = $list.count
   
 }
 
 $data = Get-Content $file 
 for($i=0; $i -le $data.count; $i++){
   if ($wrap) {
     $fnum = $list[$($i % $numwrap)]     
   }
   else { $fnum = $i % $num }

   $data[$i] | Out-File -FilePath $($outfiles[$fnum]) -Append
 }
}

All I did was add an array $list. If the –wrap switch is used this is populated by taking the values from the range of the number of files in a forward and reverse direction.

we then work out which file to use by doing the modulo arithmetic on the number of entries in list rather than in the outfile set.

other more sophisticated patterns could be devised if required.

Enjoy

File splitting

I’ve recently had to do a lot of file splitting e.g. taking lists of names and creating lists of alternate names to give me two files. Every time I have one of these problems I seem to re-write the script.  Time for a generic file splitting function.

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
function split-file {
[CmdletBinding()]
param
 (
 
[string]$file="data.txt",
  [string[]]$outfiles
) 

 if (!(Test-Path $file)){throw "File $file not found"}
 

## delete previous files
 $num = $outfiles.
count
 
foreach ($ofile in $outfiles
) {
 
if (Test-Path $ofile){Remove-Item $ofile
}
 }
 
 
$data = Get-Content $file
 
 
for($i=0; $i -le $data.count; $i++
){
  
$fnum = $i % $num
   $data[$i] | Out-File -FilePath $($outfiles[$fnum]) -Append
 }
}

Input parameters are the file to split and the list of files as output – this also give the number of splits.

Test that the input file can be found and count the number of output files.

For each of the output files test if it exists and delete it.

Read the input data.

Loop through the input data – calculate a number based on the modulus from the counter and the number of output files. This gives us which file in the array to output the data.

Finally write out the data – remembering to append.

This is a rather simple splitting scenario but will satisfy many requirements. Next I want to modify this so that the output wraps i.e. the output is not written 1,2,3,1,2,3,1,2,3 etc  but is written 1,2,3,3,2,1,1,2,3,3,2,1 etc

Seven Habits of Highly Effective PowerShellers

Some thoughts on highly effective PowerShell users. They:

  1. Get the job done. This isn’t as obvious as it seems. PowerShell is an automation tool. Use it.  Don’t agonise over the last bit of polishing for your script. Get it into use and gain the benefits. It can always be modified another day. Remember the 80:20 rule.  You’ll get 80% of the benefits with 20% of the effort. 
  2. Don’t forget interactive use of cmdlets. An awful lot of useful work can be performed by using cmdlets. This is especially true if working with Exchange or remote systems (don’t forget WMI)
  3. Use the pipeline. PowerShell is about the pipeline. Use the inbuilt power as much as you can but don’t forget habit 1
  4. Use scripts, functions and modules. This is all about reuse.  Don’t re-write every time.  Modules are great for organising your scripts and loading as required.
  5. Use comments You will see this one in every scripting guide.  You need to be able to understand what you meant to do with the script. This is especially important if you are sharing your scripts
  6. Write PowerShell not VBScript A lot of PowerShell examples are VBScript that has been translated directly into PowerShell. Don’t bring the VBScript habits into PowerShell – but don’t forget habit 1
  7. Are part of the PowerShell community.  By this I mean that they use the PowerShell community to find out how to do things and where possible they contribute back to the community.

Truncating numbers

This is something I fell foul of recently. I had a calculation that produced a non integer answer e.g.  2.1

I needed to get the integer part of the number.  Without really thinking about it I did this

PS> [int]2.1
2

I was then a bit surprised when I didn’t always get the right answer.  Still not thinking I was scratching my head until I realised that using [int] rounds to the nearest integer.

PS> for ($i=2.1; $i -le 3.0; $i+=0.1){"$i  $([int]$i)"}
2.1  2
2.2  2
2.3  2
2.4  2
2.5  3
2.6  3
2.7  3
2.8  3
2.9  3

This is no good because I need to round down.

The .NET Math class has a Truncate method that works however.

PS> for ($i=2.1; $i -le 3.0; $i+=0.1){"$i  $([math]::Truncate($i))"}
2.1  2
2.2  2
2.3  2
2.4  2
2.5  2
2.6  2
2.7  2
2.8  2
2.9  2

And the moral of the story is that you need to think about what you are doing and not assume that things work the way you think they do

PowerShell New Year’s Resolutions

Happy New Year to everyone

At the start of a New Year it is time to take stock and decide what you want to achieve in the next 12 months.  My PowerShell list is fairly straight forward:

  • complete PowerShell and WMI
  • produce a good programme for the user group
    • good guest speakers
    • start a series of sessions for people new to PowerShell
  • add more modules to the PowerShell Admin Modules (PSAM) on codeplex
  • blog more than last year

We’ll see how well I’ve kept them this time next year Smile