Windows Vista Forums

WMI question
  1. #1


    Jason Guest

    WMI question

    So I'm following along this training CBT, and the following function comes up:
    #######################
    function where-pingable {
    PROCESS {
    $results = get-wmiobject -query "Select statuscode from win32_pingstatus
    where address = '$_'"
    foreach-object ($result in $results) {
    if ($result.Statuscode -eq 0) {
    write-host $_
    break
    }
    }
    }
    }
    #####################
    But when I try to add the function to the environment, I get:
    PS C:\> . ./test.ps1
    Unexpected token 'in' in expression or statement.
    At C:\test.ps1:4 char:31
    + foreach-object ($result in <<<< $results) {

    So I tried to figure out what's going on. When I test the gwmi portion of
    this function with a single IP address:
    #############
    get-wmiobject -query "Select statuscode from win32_pingstatus where address
    = '10.131.1.66'"
    #############
    I get:
    __GENUS : 2
    __CLASS : Win32_PingStatus
    __SUPERCLASS :
    __DYNASTY :
    __RELPATH :
    __PROPERTY_COUNT : 1
    __DERIVATION : {}
    __SERVER :
    __NAMESPACE :
    __PATH :
    StatusCode : 0



    My first question is why aren't I getting just the StatusCode back like I
    asked for? Why am I getting this other junk?

    Thank you for your patience in helping the newbie!
    -Jason

      My System SpecsSystem Spec

  2. #2


    Shay Levy [MVP] Guest

    Re: WMI question

    Hi Jason,


    This line is a mixture of two different foreach methods, it uses the foreach-object
    (used in pipelines) cmdlet with the foreach statement syntax (which is totaly
    wrong):

    foreach-object ($result in $results) {...


    Make it look like:

    foreach ($result in $results) {...


    You can get more help on this here:

    PS > help about_foreach




    As for the __properties, this members are attached to every WMI result, use
    select-object to select only the properties you need.



    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic



    J> So I'm following along this training CBT, and the following function
    J> comes up:
    J> #######################
    J> function where-pingable {
    J> PROCESS {
    J> $results = get-wmiobject -query "Select statuscode from
    J> win32_pingstatus
    J> where address = '$_'"
    J> foreach-object ($result in $results) {
    J> if ($result.Statuscode -eq 0) {
    J> write-host $_
    J> break
    J> }
    J> }
    J> }
    J> }
    J> #####################
    J> But when I try to add the function to the environment, I get:
    J> PS C:\> . ./test.ps1
    J> Unexpected token 'in' in expression or statement.
    J> At C:\test.ps1:4 char:31
    J> + foreach-object ($result in <<<< $results) {
    J> So I tried to figure out what's going on. When I test the gwmi
    J> portion of
    J> this function with a single IP address:
    J> #############
    J> get-wmiobject -query "Select statuscode from win32_pingstatus where
    J> address
    J> = '10.131.1.66'"
    J> #############
    J> I get:
    J> __GENUS : 2
    J> __CLASS : Win32_PingStatus
    J> __SUPERCLASS :
    J> __DYNASTY :
    J> __RELPATH :
    J> __PROPERTY_COUNT : 1
    J> __DERIVATION : {}
    J> __SERVER :
    J> __NAMESPACE :
    J> __PATH :
    J> StatusCode : 0
    J> My first question is why aren't I getting just the StatusCode back
    J> like I asked for? Why am I getting this other junk?
    J>
    J> Thank you for your patience in helping the newbie! -Jason
    J>



      My System SpecsSystem Spec

  3. #3


    Shay Levy [MVP] Guest

    Re: WMI question

    Hi Jason,


    This line is a mixture of two different foreach methods, it uses the foreach-object
    (used in pipelines) cmdlet with the foreach statement syntax (which is totaly
    wrong):

    foreach-object ($result in $results) {...


    Make it look like:

    foreach ($result in $results) {...


    You can get more help on this here:

    PS > help about_foreach




    As for the __properties, this members are attached to every WMI result, use
    select-object to select only the properties you need.



    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic



    J> So I'm following along this training CBT, and the following function
    J> comes up:
    J> #######################
    J> function where-pingable {
    J> PROCESS {
    J> $results = get-wmiobject -query "Select statuscode from
    J> win32_pingstatus
    J> where address = '$_'"
    J> foreach-object ($result in $results) {
    J> if ($result.Statuscode -eq 0) {
    J> write-host $_
    J> break
    J> }
    J> }
    J> }
    J> }
    J> #####################
    J> But when I try to add the function to the environment, I get:
    J> PS C:\> . ./test.ps1
    J> Unexpected token 'in' in expression or statement.
    J> At C:\test.ps1:4 char:31
    J> + foreach-object ($result in <<<< $results) {
    J> So I tried to figure out what's going on. When I test the gwmi
    J> portion of
    J> this function with a single IP address:
    J> #############
    J> get-wmiobject -query "Select statuscode from win32_pingstatus where
    J> address
    J> = '10.131.1.66'"
    J> #############
    J> I get:
    J> __GENUS : 2
    J> __CLASS : Win32_PingStatus
    J> __SUPERCLASS :
    J> __DYNASTY :
    J> __RELPATH :
    J> __PROPERTY_COUNT : 1
    J> __DERIVATION : {}
    J> __SERVER :
    J> __NAMESPACE :
    J> __PATH :
    J> StatusCode : 0
    J> My first question is why aren't I getting just the StatusCode back
    J> like I asked for? Why am I getting this other junk?
    J>
    J> Thank you for your patience in helping the newbie! -Jason
    J>



      My System SpecsSystem Spec

WMI question problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie Question: Basic select-object Question. No Spam PowerShell 3 06 Oct 2009
escape character question - hopefully an easy question Cookiecutter PowerShell 2 29 Jan 2009
HD DVD Question Chris Saunders Vista General 3 24 Dec 2007
vista genral question and ultimate question pedro g Vista General 7 09 Feb 2007
Dual boot system question and family deal discount question Betatesterzz Vista General 2 31 Jan 2007