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 ServerPS C:\PowerShell> get-service lanmanserver | gm
TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = ServiceNamePS 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 .
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
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