TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

Translate WMIC query to Powershell

Microsoft Windows Powershell

 
 
 
Thread Tools Display Modes
Old 07-12-2007, 06:33 PM   #1
Chad
Guest
 
Posts: n/a
Translate WMIC query to Powershell

I've been using wmic for several years and I'm trying adapt some queries to
Powershell.

I'm having a little trouble with this one which enumerates AD or local
membership:

Here's the wmic query for local and AD respectively
wmic path Win32_GroupUser WHERE
(GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
PartComponent

wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
"Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent

Here's my attempt at a Powershell equivalent

$computer = "LocalServer"
$groupname = "Administrators"
$domain = "LocalServer"
Get-WMIObject -computername $computer Win32_GroupUser | where
{$_.GroupComponent -eq
"\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}

For AD change parameters
$domain = "MyDomain"
$groupname = "MyADGroup"

The Powershell equivalent works fine for local groups but doesn't return
data for AD group query. I know I can use DirectoryServices for AD queries,
but I'd rather stick with using WMI for both local and AD group queries.

Also is there a way to call wmic directly from Powershell like I can in the
in bat files?
 
Old 07-12-2007, 06:34 PM   #2
Chad
Guest
 
Posts: n/a
RE: Translate WMIC query to Powershell


Works great and even helps simplify the rest code I was working on where I
was doing some pattern matching
on the partcomponent. Now I can get the account and domain name without
doing this

"'.*Win32_(?<type>[^.]+)\.Domain="(?<domain>[^"]+)",Name="(?<Name>[^"]+)'"
$($matches.Domain+"\"+$matches.Name)

Thanks

--Chad

"/\/\o\/\/ [MVP]" wrote:

> 4 Possible ways I worked out going from your example :
>
> 1) filter
>
> $g = gwmi Win32_Group -filter "name = 'Administrators' and domain = 'W2008'"
>
> $g.PSBase.GetRelated('Win32_UserAccount')
>
> 2) Query
>
> gwmi -q 'associators of {Win32_Group.Domain="W2008",Name="Administrators"}
> where resultclass = Win32_UserAccount'
>
> 3) Using helper object
>
> $roq = new-object System.Management.RelatedObjectQuery
> $roq.SourceObject = 'Win32_Group.Domain="W2008",Name="Administrators"'
> $roq.RelatedClass = 'Win32_UserAccount'
> $roq.ThisRole = 'GroupComponent'
> $roq.QueryString
> ([wmisearcher]$roq).get()
>
> 4) Direct casting from Path
>
>
> ([wmi]'\\W2008\root\cimv2:Win32_Group.Domain="W2008",Nam e="Administrators"').GetRelated('Win32_UserAcc
> ount')
>
> Hope this helps
>
> Greetings /\/\o\/\/
>
> "Chad" wrote:
>
> > Based on your blog I've spend a couple hours playing with differnt variation
> > of the [WMI] and get-wmiobject syntaxt with the filter parameter, however I'm
> > unable to get this WMI query which works through wmic to work in Powershell.
> > Here's a couple examples of things I tried
> >
> > [WMI]'\\.\root\cimv2:Win32_GroupUser.GroupComponent="Wi n32_Group.Domain=`"Z002`",Name=`"Administrators`"" '
> >
> > get-wmiObject win32_Group -filter
> > "GroupComponent='Win32_Group.Domain="Z002",Name="A dministrators"'"
> >
> > I can get this one to work, but I can't filter on GroupComponent or filter
> > on more than one attribute using get-wmiobject.
> >
> > get-wmiObject win32_Group -filter "Domain='Z002'"
> >
> > Thanks
> >
> > --Chad
> >
> >
> >
> > "/\/\o\/\/ [MVP]" wrote:
> >
> > > Updated Link with the PowerShell RTM version :
> > >
> > > http://mow001.blogspot.com/2006/10/p...c2-series.html
> > >
> > > ( Forgot I used this example to show the RC2 changes ;-) )
> > >
> > > Greetings /\/\o\/\/
> > > http://thePowerShellGuy.com
> > >
> > >
> > > "/\/\o\/\/ [MVP]" wrote:
> > >
> > > > Chad,
> > > >
> > > > I discuss Queries like this on my old blog here :
> > > >
> > > > http://mow001.blogspot.com/2005/10/l...-from-msh.html
> > > >
> > > > I think post this might help you out
> > > >
> > > > (Take care this post was still in a beta version you might need to add
> > > > PSbase in some places )
> > > >
> > > > if you have more questions after reading this post just drop them here
> > > >
> > > > Greetings /\/\o\/\/
> > > >
> > > > "Chad" wrote:
> > > >
> > > > > Thanks for tip on using back ticks, I should have looked at how strings were
> > > > > expanding. The odd thing is, that it "worked" with local groups using the
> > > > > double double quotes. After changing the syntax to use a backtick, the local
> > > > > enumeration still works however when I attempt to enumerate an AD group it
> > > > > hangs. My guess is that Powershell is applying the where condition after the
> > > > > WMIObject returns all of the domain's windows groups, probably not a good
> > > > > speedy thing for a large domain.
> > > > >
> > > > > Get-WMIObject Win32_GroupUser | where {$_.GroupComponent -eq
> > > > > "\\MyMachine\root\cimv2:Win32_Group.Domain=`"$doma in`",Name=`"$groupname`""}
> > > > >
> > > > > The wmic syntax works correclty for both local and AD group enumeration. How
> > > > > do I get Powershell to apply the where condition as part of the WMI query as
> > > > > I have done with wmic?
> > > > >
> > > > >
> > > > > "Bob Landau" wrote:
> > > > >
> > > > > > Chad,
> > > > > >
> > > > > > I'm not yet a PS expert but nevertheless I'd like to give you some advice on
> > > > > > what I've learned.
> > > > > >
> > > > > > 1) Use Echo, CAT, Write-Output to see how the PS interpreter is
> > > > > > understanding your expression.
> > > > > >
> > > > > > In your case I think you've 99% there the problem is with the ""$domain""
> > > > > > and any double double quote (hopefully that makes sense) I know it's awkward
> > > > > > to read.
> > > > > >
> > > > > > What you need is to escape or back quote the double quote so replace
> > > > > > `"$domain`" with what you have. What you've told PS is that there is a
> > > > > > embedded string which is empty followed by MyDomain (without quotes) so WMI
> > > > > > is not going to consider it valid.
> > > > > >
> > > > > > If I'm understanding you this should work. I've tried your expression on my
> > > > > > domain and I'm seeing what I consider to be expected.
> > > > > >
> > > > > > thx
> > > > > > bob
> > > > > >
> > > > > > "Chad" wrote:
> > > > > >
> > > > > > > I've been using wmic for several years and I'm trying adapt some queries to
> > > > > > > Powershell.
> > > > > > >
> > > > > > > I'm having a little trouble with this one which enumerates AD or local
> > > > > > > membership:
> > > > > > >
> > > > > > > Here's the wmic query for local and AD respectively
> > > > > > > wmic path Win32_GroupUser WHERE
> > > > > > > (GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
> > > > > > > PartComponent
> > > > > > >
> > > > > > > wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
> > > > > > > "Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent
> > > > > > >
> > > > > > > Here's my attempt at a Powershell equivalent
> > > > > > >
> > > > > > > $computer = "LocalServer"
> > > > > > > $groupname = "Administrators"
> > > > > > > $domain = "LocalServer"
> > > > > > > Get-WMIObject -computername $computer Win32_GroupUser | where
> > > > > > > {$_.GroupComponent -eq
> > > > > > > "\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}
> > > > > > >
> > > > > > > For AD change parameters
> > > > > > > $domain = "MyDomain"
> > > > > > > $groupname = "MyADGroup"
> > > > > > >
> > > > > > > The Powershell equivalent works fine for local groups but doesn't return
> > > > > > > data for AD group query. I know I can use DirectoryServices for AD queries,
> > > > > > > but I'd rather stick with using WMI for both local and AD group queries.
> > > > > > >
> > > > > > > Also is there a way to call wmic directly from Powershell like I can in the
> > > > > > > in bat files?

 
Old 07-12-2007, 06:34 PM   #3
Bob Landau
Guest
 
Posts: n/a
RE: Translate WMIC query to Powershell

Chad,

I'm not yet a PS expert but nevertheless I'd like to give you some advice on
what I've learned.

1) Use Echo, CAT, Write-Output to see how the PS interpreter is
understanding your expression.

In your case I think you've 99% there the problem is with the ""$domain""
and any double double quote (hopefully that makes sense) I know it's awkward
to read.

What you need is to escape or back quote the double quote so replace
`"$domain`" with what you have. What you've told PS is that there is a
embedded string which is empty followed by MyDomain (without quotes) so WMI
is not going to consider it valid.

If I'm understanding you this should work. I've tried your expression on my
domain and I'm seeing what I consider to be expected.

thx
bob

"Chad" wrote:

> I've been using wmic for several years and I'm trying adapt some queries to
> Powershell.
>
> I'm having a little trouble with this one which enumerates AD or local
> membership:
>
> Here's the wmic query for local and AD respectively
> wmic path Win32_GroupUser WHERE
> (GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
> PartComponent
>
> wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
> "Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent
>
> Here's my attempt at a Powershell equivalent
>
> $computer = "LocalServer"
> $groupname = "Administrators"
> $domain = "LocalServer"
> Get-WMIObject -computername $computer Win32_GroupUser | where
> {$_.GroupComponent -eq
> "\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}
>
> For AD change parameters
> $domain = "MyDomain"
> $groupname = "MyADGroup"
>
> The Powershell equivalent works fine for local groups but doesn't return
> data for AD group query. I know I can use DirectoryServices for AD queries,
> but I'd rather stick with using WMI for both local and AD group queries.
>
> Also is there a way to call wmic directly from Powershell like I can in the
> in bat files?

 
Old 07-12-2007, 06:35 PM   #4
Chad
Guest
 
Posts: n/a
RE: Translate WMIC query to Powershell

Thanks for tip on using back ticks, I should have looked at how strings were
expanding. The odd thing is, that it "worked" with local groups using the
double double quotes. After changing the syntax to use a backtick, the local
enumeration still works however when I attempt to enumerate an AD group it
hangs. My guess is that Powershell is applying the where condition after the
WMIObject returns all of the domain's windows groups, probably not a good
speedy thing for a large domain.

Get-WMIObject Win32_GroupUser | where {$_.GroupComponent -eq
"\\MyMachine\root\cimv2:Win32_Group.Domain=`"$doma in`",Name=`"$groupname`""}

The wmic syntax works correclty for both local and AD group enumeration. How
do I get Powershell to apply the where condition as part of the WMI query as
I have done with wmic?


"Bob Landau" wrote:

> Chad,
>
> I'm not yet a PS expert but nevertheless I'd like to give you some advice on
> what I've learned.
>
> 1) Use Echo, CAT, Write-Output to see how the PS interpreter is
> understanding your expression.
>
> In your case I think you've 99% there the problem is with the ""$domain""
> and any double double quote (hopefully that makes sense) I know it's awkward
> to read.
>
> What you need is to escape or back quote the double quote so replace
> `"$domain`" with what you have. What you've told PS is that there is a
> embedded string which is empty followed by MyDomain (without quotes) so WMI
> is not going to consider it valid.
>
> If I'm understanding you this should work. I've tried your expression on my
> domain and I'm seeing what I consider to be expected.
>
> thx
> bob
>
> "Chad" wrote:
>
> > I've been using wmic for several years and I'm trying adapt some queries to
> > Powershell.
> >
> > I'm having a little trouble with this one which enumerates AD or local
> > membership:
> >
> > Here's the wmic query for local and AD respectively
> > wmic path Win32_GroupUser WHERE
> > (GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
> > PartComponent
> >
> > wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
> > "Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent
> >
> > Here's my attempt at a Powershell equivalent
> >
> > $computer = "LocalServer"
> > $groupname = "Administrators"
> > $domain = "LocalServer"
> > Get-WMIObject -computername $computer Win32_GroupUser | where
> > {$_.GroupComponent -eq
> > "\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}
> >
> > For AD change parameters
> > $domain = "MyDomain"
> > $groupname = "MyADGroup"
> >
> > The Powershell equivalent works fine for local groups but doesn't return
> > data for AD group query. I know I can use DirectoryServices for AD queries,
> > but I'd rather stick with using WMI for both local and AD group queries.
> >
> > Also is there a way to call wmic directly from Powershell like I can in the
> > in bat files?

 
Old 07-12-2007, 06:35 PM   #5
/\/\o\/\/ [MVP]
Guest
 
Posts: n/a
RE: Translate WMIC query to Powershell

Chad,

I discuss Queries like this on my old blog here :

http://mow001.blogspot.com/2005/10/l...-from-msh.html

I think post this might help you out

(Take care this post was still in a beta version you might need to add
PSbase in some places )

if you have more questions after reading this post just drop them here

Greetings /\/\o\/\/

"Chad" wrote:

> Thanks for tip on using back ticks, I should have looked at how strings were
> expanding. The odd thing is, that it "worked" with local groups using the
> double double quotes. After changing the syntax to use a backtick, the local
> enumeration still works however when I attempt to enumerate an AD group it
> hangs. My guess is that Powershell is applying the where condition after the
> WMIObject returns all of the domain's windows groups, probably not a good
> speedy thing for a large domain.
>
> Get-WMIObject Win32_GroupUser | where {$_.GroupComponent -eq
> "\\MyMachine\root\cimv2:Win32_Group.Domain=`"$doma in`",Name=`"$groupname`""}
>
> The wmic syntax works correclty for both local and AD group enumeration. How
> do I get Powershell to apply the where condition as part of the WMI query as
> I have done with wmic?
>
>
> "Bob Landau" wrote:
>
> > Chad,
> >
> > I'm not yet a PS expert but nevertheless I'd like to give you some advice on
> > what I've learned.
> >
> > 1) Use Echo, CAT, Write-Output to see how the PS interpreter is
> > understanding your expression.
> >
> > In your case I think you've 99% there the problem is with the ""$domain""
> > and any double double quote (hopefully that makes sense) I know it's awkward
> > to read.
> >
> > What you need is to escape or back quote the double quote so replace
> > `"$domain`" with what you have. What you've told PS is that there is a
> > embedded string which is empty followed by MyDomain (without quotes) so WMI
> > is not going to consider it valid.
> >
> > If I'm understanding you this should work. I've tried your expression on my
> > domain and I'm seeing what I consider to be expected.
> >
> > thx
> > bob
> >
> > "Chad" wrote:
> >
> > > I've been using wmic for several years and I'm trying adapt some queries to
> > > Powershell.
> > >
> > > I'm having a little trouble with this one which enumerates AD or local
> > > membership:
> > >
> > > Here's the wmic query for local and AD respectively
> > > wmic path Win32_GroupUser WHERE
> > > (GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
> > > PartComponent
> > >
> > > wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
> > > "Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent
> > >
> > > Here's my attempt at a Powershell equivalent
> > >
> > > $computer = "LocalServer"
> > > $groupname = "Administrators"
> > > $domain = "LocalServer"
> > > Get-WMIObject -computername $computer Win32_GroupUser | where
> > > {$_.GroupComponent -eq
> > > "\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}
> > >
> > > For AD change parameters
> > > $domain = "MyDomain"
> > > $groupname = "MyADGroup"
> > >
> > > The Powershell equivalent works fine for local groups but doesn't return
> > > data for AD group query. I know I can use DirectoryServices for AD queries,
> > > but I'd rather stick with using WMI for both local and AD group queries.
> > >
> > > Also is there a way to call wmic directly from Powershell like I can in the
> > > in bat files?

 
Old 07-12-2007, 06:35 PM   #6
/\/\o\/\/ [MVP]
Guest
 
Posts: n/a
RE: Translate WMIC query to Powershell

Updated Link with the PowerShell RTM version :

http://mow001.blogspot.com/2006/10/p...c2-series.html

( Forgot I used this example to show the RC2 changes ;-) )

Greetings /\/\o\/\/
http://thePowerShellGuy.com


"/\/\o\/\/ [MVP]" wrote:

> Chad,
>
> I discuss Queries like this on my old blog here :
>
> http://mow001.blogspot.com/2005/10/l...-from-msh.html
>
> I think post this might help you out
>
> (Take care this post was still in a beta version you might need to add
> PSbase in some places )
>
> if you have more questions after reading this post just drop them here
>
> Greetings /\/\o\/\/
>
> "Chad" wrote:
>
> > Thanks for tip on using back ticks, I should have looked at how strings were
> > expanding. The odd thing is, that it "worked" with local groups using the
> > double double quotes. After changing the syntax to use a backtick, the local
> > enumeration still works however when I attempt to enumerate an AD group it
> > hangs. My guess is that Powershell is applying the where condition after the
> > WMIObject returns all of the domain's windows groups, probably not a good
> > speedy thing for a large domain.
> >
> > Get-WMIObject Win32_GroupUser | where {$_.GroupComponent -eq
> > "\\MyMachine\root\cimv2:Win32_Group.Domain=`"$doma in`",Name=`"$groupname`""}
> >
> > The wmic syntax works correclty for both local and AD group enumeration. How
> > do I get Powershell to apply the where condition as part of the WMI query as
> > I have done with wmic?
> >
> >
> > "Bob Landau" wrote:
> >
> > > Chad,
> > >
> > > I'm not yet a PS expert but nevertheless I'd like to give you some advice on
> > > what I've learned.
> > >
> > > 1) Use Echo, CAT, Write-Output to see how the PS interpreter is
> > > understanding your expression.
> > >
> > > In your case I think you've 99% there the problem is with the ""$domain""
> > > and any double double quote (hopefully that makes sense) I know it's awkward
> > > to read.
> > >
> > > What you need is to escape or back quote the double quote so replace
> > > `"$domain`" with what you have. What you've told PS is that there is a
> > > embedded string which is empty followed by MyDomain (without quotes) so WMI
> > > is not going to consider it valid.
> > >
> > > If I'm understanding you this should work. I've tried your expression on my
> > > domain and I'm seeing what I consider to be expected.
> > >
> > > thx
> > > bob
> > >
> > > "Chad" wrote:
> > >
> > > > I've been using wmic for several years and I'm trying adapt some queries to
> > > > Powershell.
> > > >
> > > > I'm having a little trouble with this one which enumerates AD or local
> > > > membership:
> > > >
> > > > Here's the wmic query for local and AD respectively
> > > > wmic path Win32_GroupUser WHERE
> > > > (GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
> > > > PartComponent
> > > >
> > > > wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
> > > > "Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent
> > > >
> > > > Here's my attempt at a Powershell equivalent
> > > >
> > > > $computer = "LocalServer"
> > > > $groupname = "Administrators"
> > > > $domain = "LocalServer"
> > > > Get-WMIObject -computername $computer Win32_GroupUser | where
> > > > {$_.GroupComponent -eq
> > > > "\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}
> > > >
> > > > For AD change parameters
> > > > $domain = "MyDomain"
> > > > $groupname = "MyADGroup"
> > > >
> > > > The Powershell equivalent works fine for local groups but doesn't return
> > > > data for AD group query. I know I can use DirectoryServices for AD queries,
> > > > but I'd rather stick with using WMI for both local and AD group queries.
> > > >
> > > > Also is there a way to call wmic directly from Powershell like I can in the
> > > > in bat files?

 
Old 07-12-2007, 06:36 PM   #7
/\/\o\/\/ [MVP]
Guest
 
Posts: n/a
RE: Translate WMIC query to Powershell



Glad to be of help,
I think you where focusing to much on the helper class for the relation
(Win32_groupuser) .
instead of using the data in the classes itself.

to find all the realtaions a class has you can use the GetRelations() method

$g = gwmi Win32_Group -filter "name = 'Administrators' and domain = 'W2008'"
$g.PSBase.GetRelationships()

PS I:\PowerShell> $g.PSBase.GetRelationships() |% {$_.__class}
Win32_GroupUser
Win32_AccountSID
Win32_VolumeUserQuota
Win32_VolumeUserQuota
Win32_VolumeUserQuota
Win32_VolumeUserQuota
Win32_DiskQuota
Win32_DiskQuota
Win32_DiskQuota
Win32_DiskQuota


Note that the getrelated() used in example 1 and 4 does take more
parameters, using only one makes it much slower and using the Role option (as
in example 3 adding ROld= GroupPart improves speed as the class will not be
checked)
as there is also a relation to diskQuotas this example will show that
performance difference well.


Last point in example 4 I miss the psbase this did work for me as I'm in the
CTP build of V2, for V1 you need to add PSbase.

Greetings /\/\o\/\/


"Chad" wrote:

>
> Works great and even helps simplify the rest code I was working on where I
> was doing some pattern matching
> on the partcomponent. Now I can get the account and domain name without
> doing this
>
> "'.*Win32_(?<type>[^.]+)\.Domain="(?<domain>[^"]+)",Name="(?<Name>[^"]+)'"
> $($matches.Domain+"\"+$matches.Name)
>
> Thanks
>
> --Chad
>
> "/\/\o\/\/ [MVP]" wrote:
>
> > 4 Possible ways I worked out going from your example :
> >
> > 1) filter
> >
> > $g = gwmi Win32_Group -filter "name = 'Administrators' and domain = 'W2008'"
> >
> > $g.PSBase.GetRelated('Win32_UserAccount')
> >
> > 2) Query
> >
> > gwmi -q 'associators of {Win32_Group.Domain="W2008",Name="Administrators"}
> > where resultclass = Win32_UserAccount'
> >
> > 3) Using helper object
> >
> > $roq = new-object System.Management.RelatedObjectQuery
> > $roq.SourceObject = 'Win32_Group.Domain="W2008",Name="Administrators"'
> > $roq.RelatedClass = 'Win32_UserAccount'
> > $roq.ThisRole = 'GroupComponent'
> > $roq.QueryString
> > ([wmisearcher]$roq).get()
> >
> > 4) Direct casting from Path
> >
> >
> > ([wmi]'\\W2008\root\cimv2:Win32_Group.Domain="W2008",Nam e="Administrators"').GetRelated('Win32_UserAcc
> > ount')
> >
> > Hope this helps
> >
> > Greetings /\/\o\/\/
> >
> > "Chad" wrote:
> >
> > > Based on your blog I've spend a couple hours playing with differnt variation
> > > of the [WMI] and get-wmiobject syntaxt with the filter parameter, however I'm
> > > unable to get this WMI query which works through wmic to work in Powershell.
> > > Here's a couple examples of things I tried
> > >
> > > [WMI]'\\.\root\cimv2:Win32_GroupUser.GroupComponent="Wi n32_Group.Domain=`"Z002`",Name=`"Administrators`"" '
> > >
> > > get-wmiObject win32_Group -filter
> > > "GroupComponent='Win32_Group.Domain="Z002",Name="A dministrators"'"
> > >
> > > I can get this one to work, but I can't filter on GroupComponent or filter
> > > on more than one attribute using get-wmiobject.
> > >
> > > get-wmiObject win32_Group -filter "Domain='Z002'"
> > >
> > > Thanks
> > >
> > > --Chad
> > >
> > >
> > >
> > > "/\/\o\/\/ [MVP]" wrote:
> > >
> > > > Updated Link with the PowerShell RTM version :
> > > >
> > > > http://mow001.blogspot.com/2006/10/p...c2-series.html
> > > >
> > > > ( Forgot I used this example to show the RC2 changes ;-) )
> > > >
> > > > Greetings /\/\o\/\/
> > > > http://thePowerShellGuy.com
> > > >
> > > >
> > > > "/\/\o\/\/ [MVP]" wrote:
> > > >
> > > > > Chad,
> > > > >
> > > > > I discuss Queries like this on my old blog here :
> > > > >
> > > > > http://mow001.blogspot.com/2005/10/l...-from-msh.html
> > > > >
> > > > > I think post this might help you out
> > > > >
> > > > > (Take care this post was still in a beta version you might need to add
> > > > > PSbase in some places )
> > > > >
> > > > > if you have more questions after reading this post just drop them here
> > > > >
> > > > > Greetings /\/\o\/\/
> > > > >
> > > > > "Chad" wrote:
> > > > >
> > > > > > Thanks for tip on using back ticks, I should have looked at how strings were
> > > > > > expanding. The odd thing is, that it "worked" with local groups using the
> > > > > > double double quotes. After changing the syntax to use a backtick, the local
> > > > > > enumeration still works however when I attempt to enumerate an AD group it
> > > > > > hangs. My guess is that Powershell is applying the where condition after the
> > > > > > WMIObject returns all of the domain's windows groups, probably not a good
> > > > > > speedy thing for a large domain.
> > > > > >
> > > > > > Get-WMIObject Win32_GroupUser | where {$_.GroupComponent -eq
> > > > > > "\\MyMachine\root\cimv2:Win32_Group.Domain=`"$doma in`",Name=`"$groupname`""}
> > > > > >
> > > > > > The wmic syntax works correclty for both local and AD group enumeration. How
> > > > > > do I get Powershell to apply the where condition as part of the WMI query as
> > > > > > I have done with wmic?
> > > > > >
> > > > > >
> > > > > > "Bob Landau" wrote:
> > > > > >
> > > > > > > Chad,
> > > > > > >
> > > > > > > I'm not yet a PS expert but nevertheless I'd like to give you some advice on
> > > > > > > what I've learned.
> > > > > > >
> > > > > > > 1) Use Echo, CAT, Write-Output to see how the PS interpreter is
> > > > > > > understanding your expression.
> > > > > > >
> > > > > > > In your case I think you've 99% there the problem is with the ""$domain""
> > > > > > > and any double double quote (hopefully that makes sense) I know it's awkward
> > > > > > > to read.
> > > > > > >
> > > > > > > What you need is to escape or back quote the double quote so replace
> > > > > > > `"$domain`" with what you have. What you've told PS is that there is a
> > > > > > > embedded string which is empty followed by MyDomain (without quotes) so WMI
> > > > > > > is not going to consider it valid.
> > > > > > >
> > > > > > > If I'm understanding you this should work. I've tried your expression on my
> > > > > > > domain and I'm seeing what I consider to be expected.
> > > > > > >
> > > > > > > thx
> > > > > > > bob
> > > > > > >
> > > > > > > "Chad" wrote:
> > > > > > >
> > > > > > > > I've been using wmic for several years and I'm trying adapt some queries to
> > > > > > > > Powershell.
> > > > > > > >
> > > > > > > > I'm having a little trouble with this one which enumerates AD or local
> > > > > > > > membership:
> > > > > > > >
> > > > > > > > Here's the wmic query for local and AD respectively
> > > > > > > > wmic path Win32_GroupUser WHERE
> > > > > > > > (GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
> > > > > > > > PartComponent
> > > > > > > >
> > > > > > > > wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
> > > > > > > > "Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent
> > > > > > > >
> > > > > > > > Here's my attempt at a Powershell equivalent
> > > > > > > >
> > > > > > > > $computer = "LocalServer"
> > > > > > > > $groupname = "Administrators"
> > > > > > > > $domain = "LocalServer"
> > > > > > > > Get-WMIObject -computername $computer Win32_GroupUser | where
> > > > > > > > {$_.GroupComponent -eq
> > > > > > > > "\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}
> > > > > > > >
> > > > > > > > For AD change parameters
> > > > > > > > $domain = "MyDomain"
> > > > > > > > $groupname = "MyADGroup"
> > > > > > > >
> > > > > > > > The Powershell equivalent works fine for local groups but doesn't return
> > > > > > > > data for AD group query. I know I can use DirectoryServices for AD queries,
> > > > > > > > but I'd rather stick with using WMI for both local and AD group queries.
> > > > > > > >
> > > > > > > > Also is there a way to call wmic directly from Powershell like I can in the
> > > > > > > > in bat files?

 
Old 07-12-2007, 06:38 PM   #8
/\/\o\/\/ [MVP]
Guest
 
Posts: n/a
RE: Translate WMIC query to Powershell



Glad to be of help,
I think you where focusing to much on the helper class for the relation
(Win32_groupuser) .
instead of using the data in the classes itself.

to find all the realtaions a class has you can use the GetRelations() method

$g = gwmi Win32_Group -filter "name = 'Administrators' and domain = 'W2008'"
$g.PSBase.GetRelationships()

PS I:\PowerShell> $g.PSBase.GetRelationships() |% {$_.__class}
Win32_GroupUser
Win32_AccountSID
Win32_VolumeUserQuota
Win32_VolumeUserQuota
Win32_VolumeUserQuota
Win32_VolumeUserQuota
Win32_DiskQuota
Win32_DiskQuota
Win32_DiskQuota
Win32_DiskQuota


Note that the getrelated() used in example 1 and 4 does take more
parameters, using only one makes it much slower and using the Role option (as
in example 3 adding ROld= GroupPart improves speed as the class will not be
checked)
as there is also a relation to diskQuotas this example will show that
performance difference well.


Last point in example 4 I miss the psbase this did work for me as I'm in the
CTP build of V2, for V1 you need to add PSbase.

Greetings /\/\o\/\/


"Chad" wrote:

>
> Works great and even helps simplify the rest code I was working on where I
> was doing some pattern matching
> on the partcomponent. Now I can get the account and domain name without
> doing this
>
> "'.*Win32_(?<type>[^.]+)\.Domain="(?<domain>[^"]+)",Name="(?<Name>[^"]+)'"
> $($matches.Domain+"\"+$matches.Name)
>
> Thanks
>
> --Chad
>
> "/\/\o\/\/ [MVP]" wrote:
>
> > 4 Possible ways I worked out going from your example :
> >
> > 1) filter
> >
> > $g = gwmi Win32_Group -filter "name = 'Administrators' and domain = 'W2008'"
> >
> > $g.PSBase.GetRelated('Win32_UserAccount')
> >
> > 2) Query
> >
> > gwmi -q 'associators of {Win32_Group.Domain="W2008",Name="Administrators"}
> > where resultclass = Win32_UserAccount'
> >
> > 3) Using helper object
> >
> > $roq = new-object System.Management.RelatedObjectQuery
> > $roq.SourceObject = 'Win32_Group.Domain="W2008",Name="Administrators"'
> > $roq.RelatedClass = 'Win32_UserAccount'
> > $roq.ThisRole = 'GroupComponent'
> > $roq.QueryString
> > ([wmisearcher]$roq).get()
> >
> > 4) Direct casting from Path
> >
> >
> > ([wmi]'\\W2008\root\cimv2:Win32_Group.Domain="W2008",Nam e="Administrators"').GetRelated('Win32_UserAcc
> > ount')
> >
> > Hope this helps
> >
> > Greetings /\/\o\/\/
> >
> > "Chad" wrote:
> >
> > > Based on your blog I've spend a couple hours playing with differnt variation
> > > of the [WMI] and get-wmiobject syntaxt with the filter parameter, however I'm
> > > unable to get this WMI query which works through wmic to work in Powershell.
> > > Here's a couple examples of things I tried
> > >
> > > [WMI]'\\.\root\cimv2:Win32_GroupUser.GroupComponent="Wi n32_Group.Domain=`"Z002`",Name=`"Administrators`"" '
> > >
> > > get-wmiObject win32_Group -filter
> > > "GroupComponent='Win32_Group.Domain="Z002",Name="A dministrators"'"
> > >
> > > I can get this one to work, but I can't filter on GroupComponent or filter
> > > on more than one attribute using get-wmiobject.
> > >
> > > get-wmiObject win32_Group -filter "Domain='Z002'"
> > >
> > > Thanks
> > >
> > > --Chad
> > >
> > >
> > >
> > > "/\/\o\/\/ [MVP]" wrote:
> > >
> > > > Updated Link with the PowerShell RTM version :
> > > >
> > > > http://mow001.blogspot.com/2006/10/p...c2-series.html
> > > >
> > > > ( Forgot I used this example to show the RC2 changes ;-) )
> > > >
> > > > Greetings /\/\o\/\/
> > > > http://thePowerShellGuy.com
> > > >
> > > >
> > > > "/\/\o\/\/ [MVP]" wrote:
> > > >
> > > > > Chad,
> > > > >
> > > > > I discuss Queries like this on my old blog here :
> > > > >
> > > > > http://mow001.blogspot.com/2005/10/l...-from-msh.html
> > > > >
> > > > > I think post this might help you out
> > > > >
> > > > > (Take care this post was still in a beta version you might need to add
> > > > > PSbase in some places )
> > > > >
> > > > > if you have more questions after reading this post just drop them here
> > > > >
> > > > > Greetings /\/\o\/\/
> > > > >
> > > > > "Chad" wrote:
> > > > >
> > > > > > Thanks for tip on using back ticks, I should have looked at how strings were
> > > > > > expanding. The odd thing is, that it "worked" with local groups using the
> > > > > > double double quotes. After changing the syntax to use a backtick, the local
> > > > > > enumeration still works however when I attempt to enumerate an AD group it
> > > > > > hangs. My guess is that Powershell is applying the where condition after the
> > > > > > WMIObject returns all of the domain's windows groups, probably not a good
> > > > > > speedy thing for a large domain.
> > > > > >
> > > > > > Get-WMIObject Win32_GroupUser | where {$_.GroupComponent -eq
> > > > > > "\\MyMachine\root\cimv2:Win32_Group.Domain=`"$doma in`",Name=`"$groupname`""}
> > > > > >
> > > > > > The wmic syntax works correclty for both local and AD group enumeration. How
> > > > > > do I get Powershell to apply the where condition as part of the WMI query as
> > > > > > I have done with wmic?
> > > > > >
> > > > > >
> > > > > > "Bob Landau" wrote:
> > > > > >
> > > > > > > Chad,
> > > > > > >
> > > > > > > I'm not yet a PS expert but nevertheless I'd like to give you some advice on
> > > > > > > what I've learned.
> > > > > > >
> > > > > > > 1) Use Echo, CAT, Write-Output to see how the PS interpreter is
> > > > > > > understanding your expression.
> > > > > > >
> > > > > > > In your case I think you've 99% there the problem is with the ""$domain""
> > > > > > > and any double double quote (hopefully that makes sense) I know it's awkward
> > > > > > > to read.
> > > > > > >
> > > > > > > What you need is to escape or back quote the double quote so replace
> > > > > > > `"$domain`" with what you have. What you've told PS is that there is a
> > > > > > > embedded string which is empty followed by MyDomain (without quotes) so WMI
> > > > > > > is not going to consider it valid.
> > > > > > >
> > > > > > > If I'm understanding you this should work. I've tried your expression on my
> > > > > > > domain and I'm seeing what I consider to be expected.
> > > > > > >
> > > > > > > thx
> > > > > > > bob
> > > > > > >
> > > > > > > "Chad" wrote:
> > > > > > >
> > > > > > > > I've been using wmic for several years and I'm trying adapt some queries to
> > > > > > > > Powershell.
> > > > > > > >
> > > > > > > > I'm having a little trouble with this one which enumerates AD or local
> > > > > > > > membership:
> > > > > > > >
> > > > > > > > Here's the wmic query for local and AD respectively
> > > > > > > > wmic path Win32_GroupUser WHERE
> > > > > > > > (GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
> > > > > > > > PartComponent
> > > > > > > >
> > > > > > > > wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
> > > > > > > > "Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent
> > > > > > > >
> > > > > > > > Here's my attempt at a Powershell equivalent
> > > > > > > >
> > > > > > > > $computer = "LocalServer"
> > > > > > > > $groupname = "Administrators"
> > > > > > > > $domain = "LocalServer"
> > > > > > > > Get-WMIObject -computername $computer Win32_GroupUser | where
> > > > > > > > {$_.GroupComponent -eq
> > > > > > > > "\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}
> > > > > > > >
> > > > > > > > For AD change parameters
> > > > > > > > $domain = "MyDomain"
> > > > > > > > $groupname = "MyADGroup"
> > > > > > > >
> > > > > > > > The Powershell equivalent works fine for local groups but doesn't return
> > > > > > > > data for AD group query. I know I can use DirectoryServices for AD queries,
> > > > > > > > but I'd rather stick with using WMI for both local and AD group queries.
> > > > > > > >
> > > > > > > > Also is there a way to call wmic directly from Powershell like I can in the
> > > > > > > > in bat files?

 
Old 07-12-2007, 06:38 PM   #9
Chad
Guest
 
Posts: n/a
RE: Translate WMIC query to Powershell

Based on your blog I've spend a couple hours playing with differnt variation
of the [WMI] and get-wmiobject syntaxt with the filter parameter, however I'm
unable to get this WMI query which works through wmic to work in Powershell.
Here's a couple examples of things I tried

[WMI]'\\.\root\cimv2:Win32_GroupUser.GroupComponent="Wi n32_Group.Domain=`"Z002`",Name=`"Administrators`"" '

get-wmiObject win32_Group -filter
"GroupComponent='Win32_Group.Domain="Z002",Name="A dministrators"'"

I can get this one to work, but I can't filter on GroupComponent or filter
on more than one attribute using get-wmiobject.

get-wmiObject win32_Group -filter "Domain='Z002'"

Thanks

--Chad



"/\/\o\/\/ [MVP]" wrote:

> Updated Link with the PowerShell RTM version :
>
> http://mow001.blogspot.com/2006/10/p...c2-series.html
>
> ( Forgot I used this example to show the RC2 changes ;-) )
>
> Greetings /\/\o\/\/
> http://thePowerShellGuy.com
>
>
> "/\/\o\/\/ [MVP]" wrote:
>
> > Chad,
> >
> > I discuss Queries like this on my old blog here :
> >
> > http://mow001.blogspot.com/2005/10/l...-from-msh.html
> >
> > I think post this might help you out
> >
> > (Take care this post was still in a beta version you might need to add
> > PSbase in some places )
> >
> > if you have more questions after reading this post just drop them here
> >
> > Greetings /\/\o\/\/
> >
> > "Chad" wrote:
> >
> > > Thanks for tip on using back ticks, I should have looked at how strings were
> > > expanding. The odd thing is, that it "worked" with local groups using the
> > > double double quotes. After changing the syntax to use a backtick, the local
> > > enumeration still works however when I attempt to enumerate an AD group it
> > > hangs. My guess is that Powershell is applying the where condition after the
> > > WMIObject returns all of the domain's windows groups, probably not a good
> > > speedy thing for a large domain.
> > >
> > > Get-WMIObject Win32_GroupUser | where {$_.GroupComponent -eq
> > > "\\MyMachine\root\cimv2:Win32_Group.Domain=`"$doma in`",Name=`"$groupname`""}
> > >
> > > The wmic syntax works correclty for both local and AD group enumeration. How
> > > do I get Powershell to apply the where condition as part of the WMI query as
> > > I have done with wmic?
> > >
> > >
> > > "Bob Landau" wrote:
> > >
> > > > Chad,
> > > >
> > > > I'm not yet a PS expert but nevertheless I'd like to give you some advice on
> > > > what I've learned.
> > > >
> > > > 1) Use Echo, CAT, Write-Output to see how the PS interpreter is
> > > > understanding your expression.
> > > >
> > > > In your case I think you've 99% there the problem is with the ""$domain""
> > > > and any double double quote (hopefully that makes sense) I know it's awkward
> > > > to read.
> > > >
> > > > What you need is to escape or back quote the double quote so replace
> > > > `"$domain`" with what you have. What you've told PS is that there is a
> > > > embedded string which is empty followed by MyDomain (without quotes) so WMI
> > > > is not going to consider it valid.
> > > >
> > > > If I'm understanding you this should work. I've tried your expression on my
> > > > domain and I'm seeing what I consider to be expected.
> > > >
> > > > thx
> > > > bob
> > > >
> > > > "Chad" wrote:
> > > >
> > > > > I've been using wmic for several years and I'm trying adapt some queries to
> > > > > Powershell.
> > > > >
> > > > > I'm having a little trouble with this one which enumerates AD or local
> > > > > membership:
> > > > >
> > > > > Here's the wmic query for local and AD respectively
> > > > > wmic path Win32_GroupUser WHERE
> > > > > (GroupComponent="Win32_Group.Domain='MYDOMAIN',Nam e='MYADGROUP'") get
> > > > > PartComponent
> > > > >
> > > > > wmic /NODE:LocaServer path Win32_groupuser WHERE (GroupComponent =
> > > > > "Win32_Group.Domain='LocalServer',Name='MYLOCALGRO UP'") get PartComponent
> > > > >
> > > > > Here's my attempt at a Powershell equivalent
> > > > >
> > > > > $computer = "LocalServer"
> > > > > $groupname = "Administrators"
> > > > > $domain = "LocalServer"
> > > > > Get-WMIObject -computername $computer Win32_GroupUser | where
> > > > > {$_.GroupComponent -eq
> > > > > "\\$computer\root\cimv2:Win32_Group.Domain=""$doma in"",Name=""$groupname"""}
> > > > >
> > > > > For AD change parameters
> > > > > $domain = "MyDomain"
> > > > > $groupname = "MyADGroup"
> > > > >
> > > > > The Powershell equivalent works fine for local groups but doesn't return
> > > > > data for AD group query. I know I can use DirectoryServices for AD queries,
> > > > > but I'd rather stick with using WMI for both local and AD group queries.
> > > > >
> > > > > Also is there a way to call wmic directly from Powershell like I can in the
> > > > > in bat files?

 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 04:13 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional