/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell : WMI Support in RC2 (Series part 1) "," PowerShell RC2 and Active Directory Part 2 "," PowerShell : Hosting IronPython "," PowerShell RC2 and Active Directory "," PowerShell : Using IronPython to Connect to AD and... "," PowerShell Session video of Bruce Payette "," PowerShell : Active Directory Part 11 - moving - R... "," PowerShell : Learn about the HashTable Object and ... "," PowerShell : Setting SendAs permission in Exchange... "," PowerShell : Active Directory Part 10 (AD Browser) "," ")

Monday, October 02, 2006

 


PowerShell : WMI Support in RC2 (Series part 2)



In last post PowerShell : WMI Support in RC2 (Series part 1) , I started out translating the first WMI post, about more advanced WMI queries, and the helperObjects ( RelatedObjectQuery and RelationshipQuery ) you could use as helpers for creating this kind of Queries .

Also I did show that you could use out-PropertyGrid to Edit and test those queries (Change properties and query will be automatically changed)

This is a great help in doing that advanced queries, but might be a step to much if you are just starting out with PowerShell and/or WMI

In this post I take a step back, and will first  cover the different ways to get to just one WMI object / Class, and go on from the Lanman example in last post.

After that I will cover Next post I did on my blog also in October last year the WMI viewer script WMI viewer script for MSH (GUI) .

As I already made a out-DataGrid function here : PowerShell out-DataGrid update and more Dataset utilities that can take all sorts of input and show it in a GUI Form with a DataGrid that is a more generic solution, and can also combine datasources , (See Screendumps here : MSH GUI Viewer for Dataset Example ,) , Hence we do not really need it anymore, and also I could replace the whole first Part with "gwmi $WmiClass | " 

PS C:\PowerShell> gwmi win32_service | out-DataGrid
Cancel
PS C:\PowerShell> gwmi win32_service | select [a-z] | out-DataGrid
Cancel

but I did still update the script and did put it at the end of this post as it uses the next change in RC2 getting to a static Class, and can take it as input, getting to static classes is much easier in RC2 but more about that in next post.

(we going to use that also in next post about using methods in RC2)

take a note about how I changed the input Parameter to [WmiClass] so it gets more flexible, it will not only take a string but anything it can translate to an WmiClass, so you can also pass it an (Variable with) WMI Class as input, also see I use a Class to get Instances from them not needed in this example but in next post I will show some more examples about using this  :

PS C:\PowerShell> get-WMIDatagrid win32_share
Cancel
PS C:\PowerShell> [WmiClass]'win32_share'

Win32_Share

PS C:\PowerShell> $WmiClass = [WmiClass]'win32_share'
PS C:\PowerShell> get-WMIDatagrid $WmiClass
Cancel
PS C:\PowerShell>

For More information about working with DataSets and Tables see my CSV series :

working with CSV files in MSH (part one) 

/\/\o\/\/ PowerShelled: working with CSV files in MSH (part two)

/\/\o\/\/ PowerShelled: more Monad scripts, and a bit more CSV

 

And for using datasets with other datasources see these posts :

Access :
scripting games Last Day MSH answers for event 7 and 8
Excel :
Using ADO to get data from Excel to Dataset in Monad.
SQL (SQL Provider)
Getting and Working With SQL Server data in Monad

 

But to go on with WMI here are some way's to to get to an instance of a WMI class, I will show how to use get-WmiObject and also the new ways in PowerShell RC2 using [WMI] (name or Path) or [WmiSearcher] use a WQL query.

As last example I show using [WmiClass] like in the script, as I can only filter client site this is not a good way to do this, but I added it as an first example of the [WmiClass] (see also how it is used in the Updated script, to make the input more flexible and note other ways to do it)more in next post.  

 

# Use get-WmiObject

PS C:\PowerShell> get-wmiObject win32_service -filter "Name = 'lanmanserver'"

ExitCode : 0
Name : lanmanserver
ProcessId : 1580
StartMode : Auto
State : Running
Status : OK

# a More Short way

PS C:\PowerShell> gwmi win32_service -f "Name = 'lanmanserver'"

ExitCode : 0
Name : lanmanserver
ProcessId : 1580
StartMode : Auto
State : Running
Status : OK

# Using [WMI] and a Path

PS C:\PowerShell> [WMI]'\\.\root\cimv2:Win32_Service.Name="lanmanserver"'

ExitCode : 0
Name : lanmanserver
ProcessId : 1580
StartMode : Auto
State : Running
Status : OK

# only the Name of the Class

PS C:\PowerShell> [WMI]'Win32_Service.Name="lanmanserver"'

ExitCode : 0
Name : lanmanserver
ProcessId : 1580
StartMode : Auto
State : Running
Status : OK

# Using a [WMISearcher] for using a WQL query to get to the Object

PS C:\PowerShell> [wmiSearcher]"Select * from Win32_Service where Name='lanmanserver'"

Scope : System.Management.ManagementScope
Query : System.Management.ObjectQuery
Options : System.Management.EnumerationOptions
Site :
Container :

PS C:\PowerShell> ([wmiSearcher]"Select * from Win32_Service where Name='lanmanserver'").get()

ExitCode : 0
Name : lanmanserver
ProcessId : 876
StartMode : Auto
State : Running
Status : OK

# Note that you can use get-Services in PowerShell but this uses .NET not WMI

PS C:\PowerShell> get-service lanmanserver

Status Name DisplayName
------ ---- -----------
Running lanmanserver Server

PS C:\PowerShell> get-service lanmanserver | gm

TypeName: System.ServiceProcess.ServiceController

Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = ServiceName

PS C:\PowerShell> [wmiClass]'win32_service'

Win32_Service

# Using WmiClass (filter client site not efficient)

PS C:\PowerShell> $win32_service = [wmiClass]'win32_service'
PS C:\PowerShell> $win32_service.psbase.GetInstances() |? {$_.name -eq 'lanmanserver'}

ExitCode : 0
Name : lanmanserver
ProcessId : 1580
StartMode : Auto
State : Running
Status : OK

You can see that RC2 makes you very flexible, Here the updated script using [wmiClass] directlyin the Parameter set.

for the Rest I only added a couple of PsBases and changed the topmost hack .

 

# WmiDatagrid.PS1
# Shows Instances of WMIClass in Datagrid 
# PowerShell RC2 Update
#
# /\/\o\/\/ 2006 
# http://www.ThePowerShellGuy.com

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms"

function get-WMIDatagrid { 
  Param ([wmiCLass] $WmiClass = {Throw "No WMIClass given"}) 

  $MOC = $WmiClass.PSBase.GetInstances() 

  trap{"Class Not found";break

  $DT =  new-object  System.Data.DataTable 
  $DT.TableName = $wmiClass 

  $i = 0 
  $MOC | foreach { 
    $MO = $_ 

    # Make a DataRow 
    $DR = $DT.NewRow() 

    $MO.PSBase.properties | foreach { 

      $prop = $_ 

      If ($i -eq 0)  { 
        # Only On First Row make The Headers 

        $Col =  new-object System.Data.DataColumn 
        $Col.ColumnName = $prop.Name.ToString() 
        $prop.PSBase.get_Qualifiers() | foreach { 
          If ($_.Name.ToLower() -eq "key") { 
            $Col.ColumnName = $Col.ColumnName + "*" 
          } 
        } 
        $DT.Columns.Add($Col)  
      } 

      #fill dataRow  

      if ($prop.value -eq $null) { 
        $DR.Item($prop.Name) = "[empty]" 
      } 
      ElseIf ($prop.IsArray) { 
        $DR.Item($prop.Name) =[string]::Join($prop.value ,";"
      } 
      Else { 
        $DR.Item($prop.Name) = $prop.value 
        #Item is Key try again with * 
        trap{$DR.Item("$($prop.Name)*") = $prop.Value.tostring();continue
      } 
    } 
    # Add the row to the DataTable 
    $DT.Rows.Add($DR) 
    $i += 1 
  } 
    
  $form = new-object "System.Windows.Forms.Form" 
  $form.Size = new-object System.Drawing.Size @(800,400
  $DG = new-object "System.windows.forms.DataGrid" 
  $DG.CaptionText = "$wmiClass $($DT.Count)" 
  $DG.AllowSorting = $True 
  $DG.DataSource = $DT 
  $DG.Dock = [System.Windows.Forms.DockStyle]::Fill 

  #show the Form 

  $form.text = "$wmiCLass" 
  $form.Controls.Add($DG) 
  $Form.Add_Shown({$form.Activate()}) 
  $form.showdialog() 


set-alias gwd get-WMIDatagrid 

More very cool RC2 WMI stuff in next part of this series about calling WMI Methods !!, there the real power of the changes made in WMI will show ! 

Enjoy,

Greetings, /\/\o\/\/

Tags : Monad PowerShell




Comments:
Blogger G/\/\E
First of all your blog about WMI support in PowerShell is one of the best I found after several hours of investigation.

I could use much for a current project. One thing that took me some time to figure out is what to do if WMI requires certain privilegtes, especially SeSecurityPrivilege.

Regards,
G/\/\E
 
Blogger /\/\o\/\/
hiya g/\/\e,

Thanks,

You can use a managementScope for this,(options property).

More in next post about WMI,
(I will post this before the weekend )

you can also find some information here :

http://mow001.blogspot.com/2005/11/wmi-help-part-3-methods.html

See how I connect to WMI there in the optioins you have an enabe alll privileges setting.

gr /\/\o\/\/
 
Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?