Monthly Archive

Categories

Monthly Archives: July 2008

Sorting a hash table

There is a really good PowerShell script for working with indexed files - scans or photos and renumbering to cope with missing numbers available from http://www.tellingmachine.com/post/2008/07/Renaming-a-series-of-indexed-files-with-Powershell.aspx

One point at the end of the post is about sorting hash tables.  This is not intuitive as piping the hash table into sort does not work.  You need to use the GetEnumerator() before trying to sort like this

PS> $a = @{}
PS> $a[[int]1] = "dir scan0001.txt"
PS> $a[[int]11] = "dir scan0011.txt"
PS> $a[[int]3] = "dir scan0003.txt"
PS> $a

Name                           Value
----                           -----
3                              dir scan0003.txt
1                              dir scan0001.txt
11                             dir scan0011.txt

PS> $a.getEnumerator() | Sort Key -Descending

Name                           Value
----                           -----
11                             dir scan0011.txt
3                              dir scan0003.txt
1                              dir scan0001.txt

PS> $a.getEnumerator() | Sort Name -Descending

Name                           Value
----                           -----
11                             dir scan0011.txt
3                              dir scan0003.txt
1                              dir scan0001.txt

Create a hash table as shown.  Use the getEnumerator() method and pipe into sort.  Sorted!

 

Share this post :

 

Technorati Tags: PowerShell

UK User group meeting

There will be a Live Meeting hosted by the UK PowerShell User group on 29 July.

Marco Shaw, PowerShell MVP, will be talking about /n software NetCmdlets

Webcast will be 1 hour starting at 7pm UK time (GMT+1)

If you are not a member of the user group leave me a comment with contact  details or email me at powershell-uk {At} hotmail [dot] co (dot) uk

 

Share this post :

 

Technorati Tags: PowerShell,User Group

PowerShell labcast

There is a PowerShell labcast available from

https://www.microsoft.com/resources/virtuallabs/step1-technet.aspx?LabId=f7c0553e-78ee-4514-a9aa-097b673be3a7

Some one who tried it reports:

"It has a few quirks – on one occasion the voice-over disappears for a few minutes. If you leave the labcast and return to it later, your VM (and your work) is gone, so best done in a single session – or do the labwork on your local machine. Exposure to other scripting tools will help – most of the concepts transfer."

 

Share this post :

 

Technorati Tags: PowerShell

Remove-WmiObject

Having shown how a new WMI object can be created using Invoke-WmiMethod we need to consider how to remove a WMI Object.  CTP 2 supplies a Remove-WMIObject cmdlet.  This takes a WMI path to identify the individual object or you can use Get-WMIObject to identify object and pipe the result to Remove-WMIObject

Get-WmiObject -Class Win32_process -Filter "Name='notepad.exe'" | Remove-WmiObject

Remove-WMIObject does not support the -whatif and -confirm parameters so best practice is to use get-wmiobject and the filter capability to identify the correct object before piping to Remove-WMIObject

 

Share this post :

 

Technorati Tags: Powershell,WMI

Invoke-WmiMethod

In this post http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1525.entry I showed how to use the [WMIClass] accelerator to create a new WMI Object - in this case a process running notepad.

CTP2 streamlines the process to a degree in that the Invoke-WmiMethod can do it all in one line

Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList "notepad.exe"

Straight forward single cmdlet that does the job.  Very easy.

 

Share this post :

 

Technorati Tags: PowerShell,WMI

 

PowerShell and SQL Server

Michiel Wories provides a look at some of the thinking behind PowerShell in SQL Server and future plans  - http://blogs.msdn.com/mwories/archive/2008/06/25/what-no-cmdlets-sql-server-powershell.aspx

 

Share this post :

 

Technorati Tags: PowerShell,SQL Server

And now Hamlet

Dmitry has risen to the challenge with a PowerShell version of Hamlet - http://dmitrysotnikov.wordpress.com/2008/07/14/powershell-hamlet/

 

Share this post :

 

Technorati Tags: PowerShell

Macbeth in PowerShell

Just read this http://blogs.technet.com/jamesone/archive/2008/07/12/you-know-you-have-been-doing-too-much-powershell-when-you-translate-macbeth.aspx

Watch this space

 

Share this post :

 

Technorati Tags: PowerShell

IIS PowerShell CTP 2 Live Meeting

The IIS Team have announce a Live Meeting to discuss IIS 7 PowerShell provider CTP2

Details from http://forums.iis.net/t/1150349.aspx

 

Share this post :

 

Technorati Tags: PowerShell,IIS 7

[WMI]

There is one last WMI accelerator to look at - [WMI] - which is an accelerator for System.Management.ManagementObject.  This is the same object type that Get-WMIObject returns. [WMI] gets an object representing an existing WMI object

If we start with Get-WmiObject

$w1 = Get-WmiObject -Class Win32_Process -Filter "Name = 'notepad.exe'"
$w1 | get-member

If we try to emulate this with [WMI] we start to run into some issues.  The only path that I could get to work was

$w2 = [WMI]'root\cimv2:Win32_Process.Handle="4112"'
$w2 | get-member

I tried name, description and other candidates but none seemed to work.  Only thing I can think of is that to create that PowerShell is constraining the object creation.

Alternatively the object could be created like this

$y = [WMI]""
$y
$y.psbase.Path = '\\PCRS2\root\cimv2:Win32_Process.Handle="4112"'

Comparing the results of these three techniques we get

Compare-Object -ReferenceObject $w1 -DifferenceObject $w2
Compare-Object -ReferenceObject $w1 -DifferenceObject $y

Both of which indicate the objects are the same.

Looking at creating this with .NET

$z = New-Object -TypeName System.Management.ManagementObject -ArgumentList '\\.\root\cimv2:Win32_Process.Handle="4112"'
$z | Get-Member

Compare-Object -ReferenceObject $w1 -DifferenceObject $z

Again the same object is created and I could only get it to build when I use the handle.

The object creation works when I do

$t = Get-WmiObject -Class Win32_process -Filter 'name="notepad.exe"'

As we need to know the path before we can use [WMI] it may be easier to use get-WmiObject instead.  I'd be interested in hearing other peoples take on [WMI] and how they are using it

 

Share this post :

 

Technorati Tags: PowerShell,WMI