Monthly Archive

Categories

Monthly Archives: February 2011

Last call

Last reminder for the UK PowerShell Group Live Meeting tomorrow night

http://msmvps.com/blogs/richardsiddaway/archive/2011/01/18/uk-user-group-february-live-meeting.aspx

 

Meeting in March will cover Regular Expressions with guest speaker Tome Tanasovski  MVP

PowerShell and WMI code

Manning are releasing the chapters of my latest book PowerShell and WMI under their Early Access Program. The first four chapters are available now from http://www.manning.com/siddaway2/.

The source code for these chapters has also been made available for download from the same site. As more chapters are released the source code will be released as well.

The code is raw & doesn’t have headers etc. It does (should) match the contents of the chapter.

Three more chapters are in the pipeline and will be released in the not too distant future

Enjoy

Thunderbirds

Do you remember the brilliant shows produced by Gerry Anderson:

  • Thunderbirds
  • Stingray
  • Captain Scarlet
  • Fireball XL5
  • Supercar

 

And even the not so brilliant Joe 90.

The Post Office (UK) is producing a set of stamps to commemorate the shows.  They are also providing wallpapers for you computer.  Download from https://treatbox.net/genius-of-gerry-anderson-wallpapers/?utm_source=eNewsletter&utm_term=2011-1&utm_medium=email&utm_campaign=GerryAndersonWallpapers

Enjoy

Don’t forget

The UK PowerShell group has a Live Meeting on Tuesday 8 February at 7.30pm GMT.

Details from:

http://msmvps.com/blogs/richardsiddaway/archive/2011/01/18/uk-user-group-february-live-meeting.aspx

COM pt 7–Items pt 2

In part 6 we looked at the drives collection on the FileSystemObject. The Items property was mentioned.

Lets jump back to our Shell object

$shell = New-Object -ComObject "Shell.Application"

 

if we do a Get-Member

$shell | gm

You will see a method called Namespace.  This allows you to work with the Special Folders. One such special folder contains the cookies on you system.

PS> $shell.Namespace(0x21)

Title                      : Cookies
Application                : System.__ComObject
Parent                     :
ParentFolder               : System.__ComObject
Self                       : System.__ComObject
OfflineStatus              :
HaveToShowWebViewBarricade : False
ShowWebViewBarricade       : False

 

If we look at the object

$shell.Namespace(0x21) | gm

 

we will find an Items method.

$shell.Namespace(0x21).Items()

 

will dump the information faster than you can read.  The Items method can not be used to dump individual items

 

We could do something like

$shell.Namespace(0x21).Items() | select Name, ModifyDate

 

if we want to see all of them.  This can be refined as

$shell.Namespace(0x21).Items() | sort ModifyDate -Descending | select Name, ModifyDate

To discover the number of cookies

($shell.Namespace(0x21).Items()).count

 

In many ways it is similar to the GetEnumerator method on hash tables

Online help

Its been a long day today so I’m going to skip the next part of the COM series and show a useful bit of functionality from a PowerShell cmdlet.

Anyone who has been around PowerShell for any length of time is aware of the get-help cmdlet e.g.

Get-Help Set-Content

get-help about_for

 

It is so fundamental to getting to know your way around PowerShell that I referred to it as one of your four best friends in PowerShell when I was writing PowerShell in Practice http://www.manning.com/siddaway/

What you might not be aware of is the –online parameter. This opens the help as posted on TechNet in your browser.

Get-Help Set-Content –Online

If you try this notice the date the online help was last updated  - April 21, 2010 at the time of writing. The PowerShell team will update the online content as documentation bugs are filed. If you see something in the help file that you don’t think is right try the online version to see if it has been changed.

What about our other example?

get-help about_for –Online

This generates an error

Get-Help : The online version of this Help topic cannot be displayed because there is no Uri for online Help in the "Related Links" section of the cmdlet Help topic.
At line:1 char:9
+ get-help <<<<  about_for -Online
    + CategoryInfo          : InvalidOperation: (:) [Get-Help], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.GetHelpCommand

 

I’ve checked a random selection of about files and none of them had a URI. You can find them online at http://technet.microsoft.com/en-us/library/dd347616.aspx

Surprise

Life is full of surprises and one that has happened to me revolves around the post that seems to get the most attention.  Since moving to one of my mirrors from Microsoft Live to WordPress one of the extra features is a report on the post that gets the most traffic. Surprisingly it is this one from January 2008

http://richardspowershellblog.wordpress.com/2008/01/11/powershell-for-windows-server-backup/

I know that the PowerShell support for Windows backup has been changed in Windows 2008 R2 so I think I’d better do an update.  Look for it soon

COM pt 6–Collections pt 1

If we return to our filesystemobject

$fso = New-Object -ComObject "Scripting.FileSystemObject"

and look at the drives collection

$fso.Drives

 

we will see a collection of drives. If we try to index into it

$fso.Drives[0]

 

we get error that says
Unable to index into an object of type System.__ComObject.

 

can do something like this

$drives = $fso.Drives
foreach ($drive in $drives){$drive}

 

but still doesn’t allow us to reach individual drives

This is an issue with COM collections accessed in PowerShell. They do have a property called Item that we can use. Normally we would do something like

$fso.Drives.Item(1)

 

but we get an error about indexing.  If we look at the Item property

PS> $fso.Drives.Item

IsSettable          : False
IsGettable          : True
OverloadDefinitions : {IDrive Item (Variant)}
TypeNameOfValue     : System.Object
MemberType          : ParameterizedProperty
Value               : IDrive Item (Variant) {get}
Name                : Item
IsInstance          : True

 

It doesn’t want to take a number as an argument. It does take a drive letter

PS> $fso.Drives.Item("c")

Path           : C:
DriveLetter    : C
ShareName      :
DriveType      : 2
RootFolder     : System.__ComObject
AvailableSpace : 191890976768
FreeSpace      : 191890976768
TotalSize      : 249951154176
VolumeName     :
FileSystem     : NTFS
SerialNumber   : 941000572
IsReady        : True

 

If you know the drives then picking a letter is OK – otherwise we need to display everything as a first pass so we can work out what drives are mapped