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:


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.



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)

Friday 7 May 2010

Guessing the Election

For those who read this blog from the UK you may know there was a general election yesterday.  Having stayed up all night watching  BBC1 (while nodding off regularly) it was seeming like they were taking wild guesses about who would win the seats before they were announced.

After playing Rock Paper Scissors with the computer for a bit (Code below) I thought I could probably use powershell to guess at the election.  Firstly I will show you Rock, Paper, Scissors.

get-random “Rock”,”Paper”,”Scissors”

After that I thought to the election and how it would be useful for whose who didn’t know which way to vote

get-random “labour”,”Lib Dem”,”Conservitives”,”other”

And then I thought to how to predict the election results.  For this we are going for more than a one liner although it could be done in one line.  For this I have created a folder in the root of C: called election and made a list of constituenies in a csv file and a file with a list of parties

$Locations=import-csv C:\Election\Constituenies.csv
foreach ($Location in $Locations) {write-output $Location,(Get-Random(get-content C:\Election\Parties.csv))}

This is only a basic script at the moment but it is something that could be built on to to make it more use.

Monday 5 April 2010

Windows Server 2008 R2 Core

Ok, so you might be wondering what that has to do with Powershell.  Hopefully by the end of this post you might know why I am posting this.  I am writing about Microsoft Windows Server 2008 R2 core as this has the ability to install Powershell (some hacks have been preformed to get it onto 2008 original but these aren’t supported)
Firstly I’ll let Microsoft introduce what Server Core is for if you are unaware:
The Server Core installation option for the Microsoft Windows Server 2008 R2 operating system provides a minimal environment for running specific server roles that reduces the maintenance and management requirements and the attack surface for those server roles.
So with being cut back the main thing to go is the GUI leaving you with only a command prompt window to work with.  You will find you need a couple of commands to get you to the point of where you can install Powershell and get back to a more sensible console.  A quick reference sheet is available here.  What I plan to do here is go through what you have to do to get from a clean install to be working with Powershell.  I’m starting from the point of the installation finishing and having chosen my password.
  • When you log in you will be presented with a cmd.exe, this is it, don’t expect any more as it’s not coming.
  • At this stage the name of the computer is fairly random. I’m just on a workgroup for now so to rename the computer I need to type:

    • netdom renamecomputer %computername% /NewName:**
    • After a reboot the name will be changed
    • you can check the name with the “echo %computername% command
  • Many services require a static IP address, however I am not setting up anything that is dependent on that and as the quick reference sheet is clear on how to do that I’ll skip over it for here.
  • Next up we’ll look at what roles and services are enabled and disabled, to do this we use the Dism command as follows:

    • dism /online /get-features /format:table
  • If you look up the list you will find a service called “MicrosoftWindowsPowerShell” and this is set to disabled by default.  When you find what you want installed type the following:

    • start /w ocsetup name_as_above
(Thanks to Jeffrey Hicks for letting me know an easier way)
  • To get Powershell you will need to first have .net framework 2.0 running (NetFx2-ServerCore)then turn on powershell as follows (please note names are caps sensitive):

    • start /w ocsetup NetFx2-ServerCore
    • start /w ocsetup MicrosoftWindowsPowerShell
  • you can now start Powershell by going to:
    • %systemroot%\system32\WindowsPowerShell\v1.0 then running Powershell.exe
  • Now you can get a list of Roles and Features by:
    • Import-Module ServerManager
    • Get-WindowsFeature
      • You can also add and remove using the Add-WindowsFeature and Remove-WindowsFeature comlets.
Jeffrey Hicks has a great blog post about setting Powershell as your default shell here: PowerShell Quick Start on Server Core R2 - The Lonely Administrator – Do this on a test server to start with so you don’t end up breaking your server
I hope this is a good start and to find out more check out the http---powerscripting.net- Podcast Episode 110 when they are talking to “The Server Core Guy”.

Monday 15 March 2010

Powershell With Microsoft Exchange

This is just a quick post for those wanting to use Powershell with Microsoft Exchange and there is some coverage of Powershell with Active Directory in server 2008 R2 – Free eBook for download here: http://www.red-gate.com/specials/Exchange/the_sysadmin_handbook_ebook.htm?utm_source=simpletalkExchange&utm_medium=email&utm_content=handbookebookmarch10

Looks good from a quick scan and I hope it’s of use to you