Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - meaning of this cast ?

Reply
 
Old 11-15-2007   #1 (permalink)
Leo Tohill


 
 

meaning of this cast ?

About this line:

$wmi = [wmiclass]"\\.\ROOT\CIMV2:Win32_Service"

could someone explain to me the meaning of this? I know that it creates an
instance of wmiclass, but what PS language feature is in use?

Thanks,

Leo


My System SpecsSystem Spec
Old 11-15-2007   #2 (permalink)
Shay Levi


 
 

Re: meaning of this cast ?

Hi Leo

Check out the Windows PowerShell team blog, you'll find there various techniques
to get WMI objects and classes.

Improved Support for WMI
http://blogs.msdn.com/powershell/arc...26/647038.aspx


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> About this line:
>
> $wmi = [wmiclass]"\\.\ROOT\CIMV2:Win32_Service"
>
> could someone explain to me the meaning of this? I know that it
> creates an instance of wmiclass, but what PS language feature is in
> use?
>
> Thanks,
>
> Leo
>

My System SpecsSystem Spec
Old 11-15-2007   #3 (permalink)
Karl Prosser[MVP]


 
 

Re: meaning of this cast ?

first to type cast something in powershell you use the typename like this

[double]2

will return a double rather than just an integer.. however if also is
more than just typecasting you can convert such as

[int]"2"

basically the powershell engine will do whatever it can to try to
convert the object into the type specified.. but it goes ever further

certian classes have an ADAPTER, where the adapter has methods to try to
convert things into the object..

i.e
[xml] actually returns a dotnet xmldocument object..
so the adapter allows you to do a few things.. one is convert a string
into an xmldocument

$a = [xml] "<parent><child>something</child></parent>"

additionaly the adapter does other things, in the XML case.. an
XMldocument dotnet object has a set of static methods and properties,
however the adapter in powershell allows you to do

$a.parent.child

which of course is contextual to the contents of the xmldocument.. which
is really cool.

so in your case the wmiclass adapter converts a valid WMI path/query and
returns a dotnet system.management.managementobject .. the [ADSI]
adapter does likewise the an ldap path/query

alternatively you could have used the get-wmiobject cmdlet to do the
same thing

get-WmiObject win32_service -namespace "root/cimv2"

but the great thing about the adapter is you have alot of those WMI
strings hanging around in previous vbscripts or on the internet etc.

also the adapter in wmi is doing other great things, grabbing the
properties on each specific WMI class, and adapting to to the wmi
management object..

i.e

gwmi win32_service |Select-Object *
gwmi win32_process |Select-Object *

and you'll notice different properties specific to a service or a
process, even though the resulting object type is
system.management.managementobject each time.





Leo Tohill wrote:
Quote:

> About this line:
>
> $wmi = [wmiclass]"\\.\ROOT\CIMV2:Win32_Service"
>
> could someone explain to me the meaning of this? I know that it creates an
> instance of wmiclass, but what PS language feature is in use?
>
> Thanks,
>
> Leo
>
My System SpecsSystem Spec
Old 11-15-2007   #4 (permalink)
Leo Tohill


 
 

Re: meaning of this cast ?

very cool. Thanks.

"Karl Prosser[MVP]" wrote:
Quote:

> first to type cast something in powershell you use the typename like this
>
> [double]2
>
> will return a double rather than just an integer.. however if also is
> more than just typecasting you can convert such as
>
> [int]"2"
>
> basically the powershell engine will do whatever it can to try to
> convert the object into the type specified.. but it goes ever further
>
> certian classes have an ADAPTER, where the adapter has methods to try to
> convert things into the object..
>
> i.e
> [xml] actually returns a dotnet xmldocument object..
> so the adapter allows you to do a few things.. one is convert a string
> into an xmldocument
>
> $a = [xml] "<parent><child>something</child></parent>"
>
> additionaly the adapter does other things, in the XML case.. an
> XMldocument dotnet object has a set of static methods and properties,
> however the adapter in powershell allows you to do
>
> $a.parent.child
>
> which of course is contextual to the contents of the xmldocument.. which
> is really cool.
>
> so in your case the wmiclass adapter converts a valid WMI path/query and
> returns a dotnet system.management.managementobject .. the [ADSI]
> adapter does likewise the an ldap path/query
>
> alternatively you could have used the get-wmiobject cmdlet to do the
> same thing
>
> get-WmiObject win32_service -namespace "root/cimv2"
>
> but the great thing about the adapter is you have alot of those WMI
> strings hanging around in previous vbscripts or on the internet etc.
>
> also the adapter in wmi is doing other great things, grabbing the
> properties on each specific WMI class, and adapting to to the wmi
> management object..
>
> i.e
>
> gwmi win32_service |Select-Object *
> gwmi win32_process |Select-Object *
>
> and you'll notice different properties specific to a service or a
> process, even though the resulting object type is
> system.management.managementobject each time.
>
>
>
>
>
> Leo Tohill wrote:
Quote:

> > About this line:
> >
> > $wmi = [wmiclass]"\\.\ROOT\CIMV2:Win32_Service"
> >
> > could someone explain to me the meaning of this? I know that it creates an
> > instance of wmiclass, but what PS language feature is in use?
> >
> > Thanks,
> >
> > Leo
> >
> Very
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
The Meaning of Valentine's Day Chillout Room
meaning of -eq $null PowerShell
Gives new meaning to air bags Vista General
OT: Vista meaning? Vista General
What's the meaning of LOL? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46