/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Getting and using a SecurityPrincipal from MSH "," Line Breaks "," MSH Clipboard part 2 (of 3 ?) "," MSH TakeOwner working "," MSH Clipboard use workaround "," [MSH] keyboard status "," playing with MY-object from MSH "," MSH access NT provider "," Update-TypeData (Democracy to the types) "," MSH directory watcher with popup-balloon "," ")

Tuesday, October 25, 2005

 


Lets Query WMI from MSH



ls,

as I was answering a post in the NG,
about a vbscript to get dependent services

the (corrected) script looks like this :

Dim UnArgs
Set UnArgs = WScript.Arguments.Unnamed
WScript.Echo UnArgs(0)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & UnArgs(0) & "'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )

for each s in colServiceList
WScript.Echo s.name
next

this is a bit more as the standard WMI get propery, and involves more than 1 WMI-class.

I decided to make it in MSH also :.

at first I came with this :

Function GetDep {
$mos = (new-object system.management.ManagementObjectSearcher)
$mos.Query = "Associators of {Win32_Service.Name='" + $Args[0] + "'} Where AssocClass=Win32_DependentService Role=Antecedent"
$mos.get()
}


(you can not use a Moniker to connect to WMI as in VBscript)

But after that I thought .. this may be correct but not realy MSH.
Hence, I did look at the get-WMIObject function.

first how to get only the wanted Class.

the get-WMIObject uses - filter for this :




then how to get the related class

a Get-Method on the Object revealed the GetRelated Property

hence ,

Function GetServiceDeps {($s = get-wmiObject win32_service -filter "Name = '$($args[0])'").GetRelated("win32_service")}

And Ready ..

Much more Clean, shorter and more MSH

but then again .....maybe NOT..... (c) LSL

I did forget the Role-part this does not matter for "lanmanserver" so I did not see it at first but this will give also the relations the service is dependend on.

OK, lets look again at that Method :

MSH G:\Monad> (get-wmiObject win32_service -filter "Name = 'lanmanserver'").GetRelated.OverloadDefinitions
System.Management.ManagementObjectCollection GetRelated()
System.Management.ManagementObjectCollection GetRelated(String relatedClass)
System.Management.ManagementObjectCollection GetRelated(String relatedClass, String relationshipClass, String relationshipQualifier, String relatedQualifier,String relatedRole, String thisRole, Boolean classDefinitionsOnly, EnumerationOptions options)
(I left out the ASync Methods)
Oops, IMNSHO that is not a nice Overload !!!
(Quiz Question : You know them All  well enough to fill the 3th overload ?)
I do now ;-) (see later in post) and it seems that you can keep the ones you don't need empty.
it is : 
MSH G:\Monad> (get-wmiObject win32_service -filter "Name = 'lanmanserver'").GetRelated($null,"Win32_DependentService",$null,$null,$null,"Antecedent",$false,$null)

But then I did not know what to fill in,
but I saw another Method called



after playing a bit I stopped at :

 ((($s = get-wmiObject win32_service -filter "Name = 'lanmanserver'").GetRelationships("win32_Dependentservice"))  foreach {$_.dependent}).split("=")[1]

What do you think ? , In this case I would more like the ".NET" solution ;-)

But.. help was on the way ;-)

while searching SDK's etc for the WQL language specs,
I stumbled upon System.Management.RelationshipQuery
after playing with it (more about how later) I could only make a



So I needed his brother-class : System.Management.RelatedObjectQuery(System.Management.RelatedObjectQuery is related to GetRelated
and RelationshipQuery to GetRelationships)

.So ... and what can we do with thos 2 classes ?

Translate WQL Query's to Properties and Back !!!

MSH G:\Monad> $roq = new-object System.Management.RelatedObjectQuery
MSH G:\Monad> $roq.QueryString = "associators of {Win32_Service.name='lanmanserver'} where resultclass = win32_service role = Antecedent"
MSH G:\Monad> $roq


IsSchemaQuery : False
SourceObject : Win32_Service.name='lanmanserver'
RelatedClass : win32_service
RelationshipClass :
RelatedQualifier :
RelationshipQualifier :
RelatedRole :
ThisRole : Antecedent
ClassDefinitionsOnly : False
QueryLanguage : WQL
QueryString : associators of {Win32_Service.name='lanmanserver'} wher
e resultclass = win32_service role = Antecedent

So, Now you know How I filled in that 3th Overload of GetRelated ;-)

but at the End, I don't know if i want to, because you can get put the RelatedObjectQuery into a
ManagementObjectSearcher directly. (see last "bonus"-script.)

so until you need more as only the relatedClass (1st/2nd Overload of GetRelated)
get-wmiObject is nice but after that I prefer the "system.management"-Way

And .. as final "Bonus" - script to learn the WMI Queries, I coupled to on my Objectviewer-script, to get a GUI WQL-Querytool :
$roq = new-object System.Management.RelatedObjectQuery
ov($roq)
$mos = new-object system.management.ManagementObjectSearcher($roq)
In the MSHObjectviewer you can change the Properties (or the Query), and you will see the effect in the Query(or the properties). You have to change to another field to see the reaction.

I think now you have enough tools to get some more heavy work done in WMI

Doe er iets leuks mee (have Fun)

gr /\/\o\/\/

PS IF you do not have OV(MSHObject-Viewer), go here : MSH Object Viewer

for more info about WQL queries go to : http://msdn.microsoft.com/library/en-us/wmisdk/wmi/wql_sql_for_wmi.asp?frame=true


Comments: 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?