July 2008 - Posts

Powershell resources

Jonathan has picked upon on some very useful resources on the TechNet Scripting Center  -http://jonoble.spaces.live.com/blog/cns!CC73D8744F0894A5!613.entry    including a additions to the PowerShell Owners Manual and VBScript to PowerShell conversions

 

Share this post :

 

Technorati Tags:

Posted by Richard Siddaway's Blog
Filed under:

PowerShell Startup

If you think your PowerShell startup is slow, or even if you think it is OK, follow the instructions here http://blogs.msdn.com/powershell/archive/2008/07/11/speeding-up-powershell-startup.aspx and the link to http://blogs.msdn.com/powershell/archive/2007/11/08/update-gac-ps1.aspx

 

Share this post :

 

Technorati Tags:

Posted by Richard Siddaway's Blog
Filed under:

WMISearcher

Following on from our look at Get-WmiObject and [WMIClass] I thought a quick reminder of the other WMI accelerators may be worth while.

[WMISearcher] is an accelerator for System.Management.ManagementObjectSearcher - http://msdn.microsoft.com/en-us/library/system.management.managementobjectsearcher.aspx

It searches WMI classes for particular instance(s) based on a WQL query.  WQL is explained

http://msdn.microsoft.com/en-us/library/aa394606(VS.85).aspx

$query = [WMISearcher]'Select * from Win32_Process where Name = "notepad.exe"'
$query.Get() | Select Name, Processid | Format-Table -AutoSize

Alternatively the following could be used

Get-WmiObject -Class Win32_Process -Filter 'Name = "notepad.exe"' | Select Name, Processid | Format-Table -AutoSize

or

Get-WmiObject -Query 'Select * from Win32_Process where Name = "notepad.exe"' | Select Name, Processid | Format-Table -AutoSize

could be used.

If you need to search WMI for particular information you have choices.  The choice comes down to what sort of object you want to return.  If you want to work with it use Get-WMIObject but if only want to view information then could try [WMISearcher]

 

Share this post :

 

Technorati Tags: ,

WMI Objects

I had a comment left by Ryan on this post http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!983.entry regarding the difference between Get-WmiObject and [WMIClass] especially when you need to create a new object via WMI.

The code that Ryan has produced is worth looking at - see http://www.dagey.net/wordpress/2008/07/08/powershell-madness-part-1/

The important thing to remember is that PowerShell is .NET based and underneath the cmdlets and accelerators we are dealing with .NET classes.  PowerShell gives us a layer of abstraction that hides some of the more nasty programming type stuff ;-)

Ryan's post regarded using WMI on IIS 7 but for this discussion I will use Win32_Process and notepad as it is more straight forward and is definitely something you will be able to try at home.

If we start with Get-WmiObject.

Get-WmiObject is used to read information about and in some cases perform actions upon an existing WMI object.  If we create a WMI object pointing to an existing and open instance of notepad (what would we do for examples if notepad didn't exist)

PS> $g = Get-WmiObject -Class Win32_Process -Filter "Name = 'notepad.exe'"
PS> $g | Get-Member

   TypeName: System.Management.ManagementObject#root\cimv2\Win32_Process

and then use get-member on it we see that it is returning a System.Management.ManagementObject of a Win32_Process WMI Class.  If you look at the properties and methods shown by get-member there is no method to create an instance.  More information on this class at http://msdn.microsoft.com/en-us/library/system.management.managementobject.aspx

It is like picking a cup of coffee off a counter where there are many cups of coffee.  It is in existence and usable (drinkable) but you can't use it to create a cup.  My analogy breaks down slightly as there is a clone() method if we drop into the base object but that appears to clone the object rather than a new instance of notepad so we will leave that to one side.

If we now look at [WMIClass] and use get-member on it

PS> $c = [WMIClass]'Win32_Process'
PS> $c | Get-Member

   TypeName: System.Management.ManagementClass#ROOT\cimv2\Win32_Process

We see that we are using a different .NET class System.Management.ManagementClass - see http://msdn.microsoft.com/en-us/library/system.management.managementclass.aspx   Reading this shows there is a way to create a new instance of the class via CreatInstance() method.

Get-member shows us a create() method and

PS> $c.psbase | Get-Member

shows us the createinstance() method that matches the .NET documentation either will work.

$c.Create("Notepad.exe")

Is the simplest to use and will create a new process running notepad.

Using [WMIClass] is a shortcut for using New-Object

$x = New-Object -TypeName System.Management.ManagementClass -ArgumentList "Win32_Process"
$x | Get-Member
$x.Create("notepad.exe")

To summarise Get-WmiObject is a PowerShell cmdlet to work with the System.Management.ManagementObject  .NET class which allows you to read information from and interact with existing instances of WMI Classes.  [WMIClass] is a shortcut (or accelarator) for creating new instances of WMI classes.

Hope this clears the differences

 

Share this post :

 

Technorati Tags: ,

IIS cmdlets

There are three cmdlets in the IIS provider that return state information

Get-WebSiteState

Get-WebItemState

Get-AppPoolState

In all three cases you can only work with a single site\apppool at a time

PS> Get-WebSiteState demo
Started
PS> Get-WebItemState iis:\sites\demo
Started
PS> Get-AppPoolState demo
Started
PS> Get-WebItemState iis:\apppools\demo
Started

Note the different way the site name or application pool name is entered between Get-WebSiteState and Get-WebItemState

There does not seem to be away to get information on all sites using these cmdlets instead you need to use

PS> Get-ChildItem IIS:\Sites\*

Name                  ID   State      Physical Path                                  Bindings
----                     --   -----        -------------                                    --------
Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80:
demo                 2    Started    C:\inetpub\demo                            http *:8080:

PS> Get-ChildItem IIS:\AppPools\*

Name                          State        Applications
----                             -----         ------------
DefaultAppPool           Started      Default Web Site
demo                         Started      demo

which seem more useful for a general view.  I would almost look to creating a get-iishealth function that incorporated a number of these checks.

 

Share this post :

 

Technorati Tags: ,

 

Posted by Richard Siddaway's Blog
Filed under:
More Posts « Previous page