March 2009 - Posts

Measure-Command

Working on a script to compare two lists of computer names (plus some other stuff) I wasn’t happy with the performance I was getting from my first try.  I started to experiment but needed to measure the time the scripts took.  Enter Measure-Command.

Measure-Comand {./script.ps1}

Output is a timespan object that gives the total execution time of the script block.  It doesn’t have to be a full script – any valid script block will work. Normally I don’t worry too much about performance but in this case it just felt the approach was too slow.

Keep Measure-Command in mind for during your testing

 

Technorati Tags: ,
Posted by Richard Siddaway's Blog
Filed under:

PowerShell uses

Normally we think of using PowerShell to perform admin tasks - creating users in AD etc - using cmdlets or scripts.  I have found myself using it for other tasks - searching through dumps of ipconfig listsing to get IP addresses; matching two lists of computer names to find discrepancies
 
PowerShell is a powerful tool with many uses - the limit is our imagination
Posted by Richard Siddaway's Blog
Filed under:

ZZZZZZZZZZ

One thing I didn't really take much notice of was the difference is spelling between English english and US english.  I have read so many technology and science books (nearly all published in the USA) that I just accepted the differences.
 
Until I come to write against a template based on US English where anything ending -ise is flagged as an error 'cos the dictionary thinks it should be -ize.  You don't realise how many times you use words like that until somethign like this comes along
 
The Victorians who introduced using -ise instaed of -ize have a lot to answer for.

Computer cmdlets

I was digging into some aspects of Exchange 2007 yesterday for Chapter 12 of my book (Chapter 11 is finished and will be on MEAP soon)  and needed to restart some of the VMs I was using.  I remembered the restart-computer that comes with V2.  It saved some time compared to logging into the offending machine and then going through the restart procedure.
 
Simple syntax
 
Restart-Computer -ComputerName target -Force
 
Force takes care of the situation where users are logged on.  It can also be run as a job.
 
Stop-Computer is a bit more permanent  :-)
 
 
Posted by Richard Siddaway's Blog
Filed under:

SQLPSX 1.5

If you use PowerShell and SQL Server you should at the very least look at SQLPSX which is a big library of PowerShell functionality.  Version 1.5 has just been released.  See http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!315.entry for details

 

Technorati Tags: ,

Test folders

I needed some test folders in a hurry.  They didn’t have to have anything in them – just exist as a set of folders for me to work with.

From the PowerShell prompt

1..20 | foreach {md "test$_"}

How easy is that.  Twenty folders in the time it would take me to open Windows explorer. 

Use the range operator to define a range of suffixes and pipe into foreach.  The md command creates the folder using string substitution to append the suffix.

If you want to be slightly more formal about things then the Sunday best version of the command is


21..40 | foreach { New-Item -Name "test$_" -ItemType directory }

The range operator is a very useful tool that probably doesn’t get as much exposure as it should

 

Technorati Tags:
Posted by Richard Siddaway's Blog
Filed under:

Active Directory Cookbook third edition

The original AD cookbook by Robbie Allen was published back in 2003. It has been one of my favourite, and most referenced, books.  When I used to use VBScript (shudder) it was indispensible and saved me vast amounts of time.  One AD implementation I did we created the whole AD structure – OUs, Sites, Subnets, Links etc form a single script! Even after moving to PowerShell I found the book very useful as I could translate the scripts in the book into PowerShell.

The third edition has just been published – ISBN 9780596521103. Like the second edition it is co-authored by Laura E Hunter and Robbie Allen. My first impression is that it is a huge book with 1015 pages of fairly small type. It has definitely grown from the first edition. 21 chapters supply nearly 500 recipes to solve AD administration problems. This edition has expanded a bit to cover ADAM\ADLS, ADFS, Exchange and ILM as well as AD itself.

Most of the recipes will present a problem and show how to solve it using the GUI tools, command line utilities, VBScript and PowerShell. Not all possibilities are available for all recipes.

Having been very impressed with the original I was delighted to see that the high quality has been maintained. The descriptions are concise but cover the required information.

The PowerShell sections feel like a last minute addition in places and could do with being a bit more consistent. In places the solutions jump from scripts to the Quest AD cmdlets and back again with no explanation.  The explanatory chapter at the front could do with more PowerShell content. I think a beginner would find getting into the PowerShell scripts harder than the VBScripts.  With Windows 2008 R2 including AD cmdlets I think the next edition would be the time to adopt a consistent approach to the PowerShell content and make it the primary scripting tool in the book.

Even with those comments this is an excellent book that I would have no hesitation in recommending. If you have the 1st edition buy it – its worth the upgrade. If you don’t have a copy buy it. Just buy it – if you do any AD admin work the book will pay for itself in the time you save.

PowerShell v1.0 is available on Microsoft Update

Microsoft have added PowerShell v1.0 as an optional update that is available through the update mechanisms.

PowerShell 1.0 through MU is only offered:

  • As an optional update
  • If you don’t have any other version of PowerShell installed (including V2 CTPs) on your machine
  • .NET 2.0 is installed on the machine
  • OS is one of: XP-SP3, W2K3-SP2, Vista-RTM, Vista-SP1

Hopefully v2 will be available through the same mechanism.

This will make deployment of PowerShell through out the enterprise a much easier proposition

Technorati Tags: ,
Posted by Richard Siddaway's Blog
Filed under:

Quest AD cmdlets

If you have followed this blog over any period of time you will know that I am a big fan of the Quest AD cmdlets.  The good news is that version 1.2 of the cmlets has gone RTM.  The bad news is that it won’t be available for a little while yet.  The major details of the changes are available from http://www.bobbobel.com/ad-cmdlets-version-12-went-gold-friday/

The ability to restore tombstoned objects is very useful as is the ability to directly pull group memberships from an object. 

There are a number of other smaller changes that I will cover in due course.

If you work with PowerShell & AD keep your eyes open for the announcement of their availability

Its all in context

I spent part of this afternoon using select-string to track down where particular settings occur in GPOs.  Use the SDM GPMC cmdlets to dump the settings to xml and then use select-string on the xml.  So much easier than wading through the GUI.

In PowerShell v1 select-string returned the line containing the text we are searching for

PS> Select-String *.txt -Pattern "target text"

file1.txt:9:some text with what we want - - - target text
file2.txt:15:some text with what we want - - - target text

In v2 we can get the context of the target ie lines before and after

PS> Select-String *.txt -Pattern "target text"   -Context 0,1

> file1.txt:9:some text with what we want - - - target text
  file1.txt:10:and lest we forget -- we want the context as well
> file2.txt:15:some text with what we want - - - target text
  file2.txt:16:and lest we forget -- we want the context as well

Note the > to show the line with the actual text.  Context is specified as number of lines before, lines after the target.

This is fine but what if you want to actually separate out the results.  At the moment we get file name, line number and the line.  what would be good would be data separated out.  First go is

Select-String *.txt -Pattern "target text"   -Context 0,1 | Select FileName, Line, Context

This doesn’t work as the object type is returned for the context.  We can use get-member to dig into the context and then do something like this

001
002
003
Select-String *.txt -Pattern "target text"   -Context 0,1 | foreach {
"{0} {1} {2} {3} " -f $_.FileName, $_.LineNumber, $_.Line, $($_.Context.PostContext)
}

 

The context collection has both a Post context and a Precontext member.   Once we have the data we can format it at leisure.

The context could be used in a number of places eg picking DNS servers out of ipconfig files.

Select-string is a very useful cmdlet and one that it pays to get to know.  BTW the pattern is actually converted to a regular expression!

Technorati Tags: ,
Posted by Richard Siddaway's Blog
Filed under:

UG Meeting on Thursday

This is the final reminder for the UG meeting on Thursday 26th March at Microsoft.

If you haven’t already told me you are coming can you do so immediately please as I have to finalise numbers

thanks

Technorati Tags: ,

Logon Scripts Live Meeting

The PowerShell User Group invites you to view a Microsoft Office Live Meeting recording of the Logon Scripts webcast.
View Recording
Recording Details
    Subject: PowerShell logon scripts
    Recording URL: https://www.livemeeting.com/cc/usergroups/view
    Recording ID: 2C4W7Q
    Attendee Key: FbH&3S\&C

This is the raw recording.

 

Technorati Tags:

January Meeting

The PowerShell User Group invites you to view a Microsoft Office Live Meeting recording of our January meeting featuring Jeffrey Snover talking about PowerShell v2.
View Recording
Recording Details
    Subject: PowerShell User Group
    Recording URL: https://www.livemeeting.com/cc/usergroups/view
    Recording ID: BHMJ3Z
    Attendee Key: xr5f$:Z/c

This is the raw recording.

 

Technorati Tags:

PowerShell IIS snapin

The PowerShell IIS snapin has been officially released.  Download it from http://www.iis.net/extensions/PowerShell

 

Technorati Tags: ,

Linkedin PowerShell Power Users Group

Already use LinkedIn?

Already use PowerShell?

Why not join the PowerShell Power Users Group on LinkedIn?

Search for PowerShell in the groups directory & join over 300 PowerShell users

 

Technorati Tags: ,
Posted by Richard Siddaway's Blog
Filed under:

PowerShell in Practice reference material

I’m getting towards the end of the book with just a few chapters left to write.  Chapter 11 will be available shortly (back to it after this post).

I’m trying to finalise the reference material I include in the Appendicies.  There are some things I have already decided to add – mainly things I always have to look up & that would be useful to have to hand.  I will not be including anything that is easily available from PowerShell help.

If there is anything along those lines that you think should be included leave a comment & I’ll see what space is available.

 

Posted by Richard Siddaway's Blog
Filed under:

AD PowerShell blog

The blog of the AD PowerShell Team can be found here - http://blogs.msdn.com/adpowershell/default.aspx

If you work with PowerShell & AD will be worth a read

 

WMI Methods and Properties

Its odd how we discover things.  I need to check the WMI classes for network adapters so used

Get-WmiObject -List -Class *network*

I intended to check out the data returned so used

Get-WmiObject -List -Class win32_networkadapterconfiguration

Notice I’ve left the –List parameter in the command by mistake.  This produces

Name                                Methods              Properties
----                                -------              ----------
Win32_NetworkAdapterConfiguration   {EnableDHCP, Rene... {ArpAlwaysSourceRoute, ArpUseEtherSNAP,…

Oh.  A quick way to see the methods and properties

(Get-WmiObject -List -Class win32_networkadapterconfiguration).Methods | Format-Table  Name, Qualifiers -AutoSize

(Get-WmiObject -List -Class win32_networkadapterconfiguration).Properties | Format-Table  Name, Type, Qualifiers  -AutoSize

 

Technorati Tags: ,

Cmdlet naming

Please read this - http://dmitrysotnikov.wordpress.com/2009/03/16/citrixs-inconsistent-cmdlets-naming/

The suggestions at the end of the article have my agreement and support.  I would add one additional suggestion -

If your vendor doesn’t use the standard verb-noun naming convention then tell them they should & keep telling them until they listen.  One of PowerShell’s great strengths is its standards. If these disappear we will have an incompatible mess that won’t help anyone.

 

Technorati Tags: ,
Posted by Richard Siddaway's Blog
Filed under:

March Live Meeting

The Live Meeting details for the March UG meeting are as follows


When: Thursday, Mar 26, 2009 6:00 PM (GMT)


Where: TVP Reading

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

Meeting will start at 6.30pm as normal

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: DW97K7
    Entry Code: m$Z949!nN
    Location: https://www.livemeeting.com/cc/usergroups

If you still cannot enter the meeting, contact support

Notice
Microsoft Office Live Meeting will 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.

 

More Posts Next page »