Tuesday 27 September 2011

Book Review: Automating Microsoft Windows Server 2008 R2 Administration with Windows PowerShell 2.0

I have just read the book by Matthew Hester and Sarah Dutkiewicz entitled Automating Microsoft Windows Server 2008 R2 Administration withy Windows PowerShell 2.0 published Sybex. A strange place to start on a book review, The size. As this is an IT book you expect it to be a huge book for which you hope an eBook appears soon after as reading it would be similar to a half-hour workout at the gym, however at 400 odd pages in paperback this book is roughly a similar weight to a netbook. This made me happy as it meant that I could sling this in my bag and read it on the train. The size wasn't the only thing that made this an easy read on the train. The way this book was written in a very easy to read way, an especially important thing when writing about things like a scripting language.


This book will take you from very little or even no PowerShell knowledge to a point where you can be proficient at self discovery very quickly. Once you have got the basics covered the appendixes will take you further.

In the chapters, contents at a glance:

Introduction.

Chapter 1: What is PowerShell and Why Do You Need It?

Chapter 2: Installing and Configuring PowerShell 2.0.

Chapter 3: PowerShell Grammar Lesson.

Chapter 4: Aliases, Functions, and the Pipe. Oh My!

Chapter 5: Creating Your Own Scripts.

Chapter 6: Remoting with PowerShell 2.0.

Chapter 7: PowerShell and the Server.

Chapter 8: Managing Active Directory with PowerShell.

Chapter 9: Managing Desktops with Server PowerShell.

Chapter 10: Manage IIS Web Server with PowerShell.

Chapter 11: PowerShell and Deployment Services.

Chapter 12: PowerShell and Virtualization.

Appendix A Solutions to Exercises.

Appendix B Developing at a Command Prompt.

Appendix C Providing for PowerShell.

Appendix D Custom Cmdlets and Advanced Functions.

Appendix E Packaging PowerShell Extensions.

Appendix F Building Your Own GUI with PowerShell.

Index.

 

For each of the topic  specific chapters Matthew and Sarah do a great job of ensuring you’re aware of the basics of what you will be working with before jumping into the PowerShell.  This makes this book great if you are new to Windows Server or just not familiar with certain aspects of the OS.

This book has two great authors working in different areas, one’s an IT Pro and the other a Developer.  This adds a lot to the book as Sarah has been able to explain best how to take your skills further in the appendixes while Matthew has used his experience as an IT Pro evangelist to cover the basics and common tasks in the chapters.

This book was a great read and I hope that Matthew and Sarah plan to write a new version for when we get the final release of PowerShell 3.0 and Server vNext although from only seeing a small amount so far on Sever vNext this would be a packed full book so this is another reason to by this book now so you’re ahead of the game for vNext…

Wednesday 25 May 2011

Using New-TimeSpan cmdlet in scripts

During the 2011 Scripting Games I learned to used many new things... one being how to use New-TimeSpan cmdlet.

It was April 12th 2011 on and Event 7 of the scripting games was posted... Our challenge?  Create a pop up box at log on to show how long is left to submit orders before the financial year ends... Read more on the following page: http://blogs.technet.com/b/heyscriptingguy/archive/tags/2011+scripting+games/beginner/event+7/

My solution to this was as follows:

# Scripting Games 2011 Beginner Event 7 #
$Timespan = New-TimeSpan -Start (get-date) -End 31/07/2011
$MsgBox = New-Object -ComObject wscript.shell
$MsgBox.Popup("There are $($timespan.days) days until the end of the fiscal year.",0,"Fiscal Year Notification")
Two lessons from this script are:
  1. Making pop up message boxes.  This is very simple way using the New-Object cmdlet to create the box with a ComObject.  When I say this is easy, I mean easy to use not explain.  Resources like the Scripting Guy or MSDN will help explain this much better than I could.
  2. New-TimeSpan cmdlet.  This is what I wanted to explain in this post.  Basically if you want to see the difference in time between one date and another then it's simple using this cmdlet.  For example if you want to know how many days there are between November 10th 2012 and December 11th 2013 you would simply type "New-TimeSpan 10/11/12 11/12/13" (For the UK US "New-TimeSpan 11/10/12 12/11/13" should work).  This will give the result of "Days : 296"
In the Scripting games I was only required to give a count down of days but this can be extended to a specific point in the day.  The following block of script is my countdown to when I finish working at my current employer.  I have the TimeSpan going from Now (Using Get-Date to retrieve current date and time) and then I use Get-Date but with parameters to specify the Day, Month, Year, Hour, and Minute of the day.  I have put the cmdlet and it's parameters into a Variable to make it easier to specify later in the script when I want only, for example, the number of days, or hours.


$Timespan = New-TimeSpan -Start (get-date) -End (Get-Date -Day 24 -Month 6 -Year 2011 -Hour 15 -Minute 30)
$MsgBox = New-Object -ComObject wscript.shell
$MsgBox.Popup("There are $($timespan.days) days, $($timespan.hours) hours, and $($timespan.minutes) minutes until I Finish!",0,"Time Until I Leave")


I have made 2 variables in this script; one for the TimeSpan, and the other for the message box.  With both of these I extend them by using some of their methods.  To find out what their methods are Create the variable (The following example will use the Message Box) "$MsgBox = New-Object -ComObject wscript.shell", then type "$MsgBox | Get-Member" you will see all methods available to that object.  From this list, as I wanted a pop up box, I selected the Pop up method.  This was used by typing "$MsgBox.Popup("Text String for in the box",0,"Window Title")".  From what I can tell the "0" means it waits for a response by clicking the OK button.  If you put a different number then it will automatically shut after is the number of seconds.

With the $TimeSpan variable I have used the object properties to only display the property I am wanting.  For example to show only the Days property I just use $TimeSpan.Days and it will display the number of days.


For this script I have saved it and created a shortcut on my desktop to the following:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "c:\Scripts\TimeUntilDeparture.ps1"

I hope this is of use to you...
Thanks for reading.

Sunday 24 April 2011

Lessons from the Scripting Games 2011

We have just come to the end of another year for the Microsoft Scripting Games.  It was another exciting event and one in which I managed to take part in full this year... well not quite, I missed one deadline due to not knowing what day it was, and the other to having been traveling for most of the last day I had to write and submit it on (my fault for leaving it 'til the last minute).  Still that is better than the 1 script I had time to submit last year.

This year as I'm still fairly new to PowerShell and generally work in the console rather than Scripting I enter in the Beginner Category.  Having finished in the top half of the board I intent to train for the next year to participate in the advanced games...

Until then I need to learn from this years scripts and see where I did well, where others did well, and where failing occurred...

Lesson 1 I shall take from this years games is the requirement for reusable code... in the most part this required use of Advanced Functions. These aren't as scary as they sound.  I won't go into how to use them now but I do plan to write alot about that in the future.  I don't want to write this until I'm clearer on this myself and I am not blogging from my phone.

Lesson 2 add help into your Scripting.  Within you functions open the help text with <# help info >

I will follow up with more lessons later on, but, for now take a look at some of the entries both high and low staring and read the scripts and comments to discover more...

Sunday 13 February 2011

Update to PowerCli

VMWare has resently released the latest version of its powershell client, PowerCli 4.1.1.  Alan Renouf's blog is a great place to go if you work with VMWare and he's just posted the lastest powercli poster to download.  Whilest your there check out the rest of his site. http://www.virtu-al.net/2011/02/13/powercli-4-1-1-poster/

Tweet from Alan Renouf (alanrenouf)