Windows Vista Forums

method Error in foreach loop
  1. #1


    Wilson Guest

    method Error in foreach loop

    Hi

    I wrote a PS script which gets registry informations about specific drivers.
    Here is the code:
    ----------------------------------------------------------------
    $computer = "nodename1","nodename2"
    foreach($nodename in $computer){
    $PnP = Get-WMIObject Win32_PnPSignedDriver -computer $nodename
    write-host " "
    write-host " "
    write-host "Objects in node $nodename"
    write-host "********************************************"
    Foreach ($driver in $PnP){
    $devicename = $driver.Devicename
    if($devicename.Startswith("Smart Array")){
    write-host $devicename
    $driver.DriverVersion
    write-host "------------------------------"



    }
    }
    }
    --------------------------------------------------------------

    I get all informations i want but there is still a error which i don't know
    how to fix:

    Objects in node nodename1
    ********************************************
    Smart Array 5x and 6x Notification Driver
    6.4.0.32
    -------------------------------------
    Smart Array 5300 Controller
    5.80.0.32
    -------------------------------------
    You cannot call a method on a null-valued expression.
    At line:9 char:34
    + if($devicename.Startswith <<<< ("Smart Array")){


    Objects in node nodename2
    ********************************************
    Smart Array 5x and 6x Notification Driver
    6.4.0.32
    -------------------------------------
    Smart Array 5300 Controller
    5.80.0.32
    -------------------------------------
    You cannot call a method on a null-valued expression.
    At line:9 char:34
    + if($devicename.Startswith <<<< ("Smart Array")){

    It looks like $devicename is empty at the end and the script wants still
    make a loop...
    Hope someone can help me getting this error away :-)

    greetings

    PS: Sorry for my english ;-)

      My System SpecsSystem Spec

  2. #2


    Tao Ma Guest

    Re: method Error in foreach loop

    Hi Wilson,

    Add a condition statement before calling 'Statwith' method on the
    $devicename.

    $devicename = $driver.Devicename
    if (!$devicename) { continue }
    if ($devicename.Startswith("Smart Array"))

    Tao Ma

    "Wilson" <Wilson@xxxxxx> 写入消息新闻:2AF91A85-AF3F-4523-AE91-3E22F172DE3D@xxxxxx

    > Hi
    >
    > I wrote a PS script which gets registry informations about specific
    > drivers.
    > Here is the code:
    > ----------------------------------------------------------------
    > $computer = "nodename1","nodename2"
    > foreach($nodename in $computer){
    > $PnP = Get-WMIObject Win32_PnPSignedDriver -computer $nodename
    > write-host " "
    > write-host " "
    > write-host "Objects in node $nodename"
    > write-host "********************************************"
    > Foreach ($driver in $PnP){
    > $devicename = $driver.Devicename
    > if($devicename.Startswith("Smart Array")){
    > write-host $devicename
    > $driver.DriverVersion
    > write-host "------------------------------"
    >
    > }
    > }
    > }
    > --------------------------------------------------------------
    >
    > I get all informations i want but there is still a error which i don't
    > know
    > how to fix:
    >
    > Objects in node nodename1
    > ********************************************
    > Smart Array 5x and 6x Notification Driver
    > 6.4.0.32
    > -------------------------------------
    > Smart Array 5300 Controller
    > 5.80.0.32
    > -------------------------------------
    > You cannot call a method on a null-valued expression.
    > At line:9 char:34
    > + if($devicename.Startswith <<<< ("Smart Array")){
    >
    >
    > Objects in node nodename2
    > ********************************************
    > Smart Array 5x and 6x Notification Driver
    > 6.4.0.32
    > -------------------------------------
    > Smart Array 5300 Controller
    > 5.80.0.32
    > -------------------------------------
    > You cannot call a method on a null-valued expression.
    > At line:9 char:34
    > + if($devicename.Startswith <<<< ("Smart Array")){
    >
    > It looks like $devicename is empty at the end and the script wants still
    > make a loop...
    > Hope someone can help me getting this error away :-)
    >
    > greetings
    >
    > PS: Sorry for my english ;-)


      My System SpecsSystem Spec

  3. #3


    Wilson Guest

    Re: method Error in foreach loop

    Oh thanks a lot it works now without error

    greetings

    "Tao Ma" wrote:

    > Hi Wilson,
    >
    > Add a condition statement before calling 'Statwith' method on the
    > $devicename.
    >
    > $devicename = $driver.Devicename
    > if (!$devicename) { continue }
    > if ($devicename.Startswith("Smart Array"))
    >
    > Tao Ma
    >
    > "Wilson" <Wilson@xxxxxx> 脨麓脠毛脧没脧垄脨脗脦脜:2AF91A85-AF3F-4523-AE91-3E22F172DE3D@xxxxxx

    > > Hi
    > >
    > > I wrote a PS script which gets registry informations about specific
    > > drivers.
    > > Here is the code:
    > > ----------------------------------------------------------------
    > > $computer = "nodename1","nodename2"
    > > foreach($nodename in $computer){
    > > $PnP = Get-WMIObject Win32_PnPSignedDriver -computer $nodename
    > > write-host " "
    > > write-host " "
    > > write-host "Objects in node $nodename"
    > > write-host "********************************************"
    > > Foreach ($driver in $PnP){
    > > $devicename = $driver.Devicename
    > > if($devicename.Startswith("Smart Array")){
    > > write-host $devicename
    > > $driver.DriverVersion
    > > write-host "------------------------------"
    > >
    > > }
    > > }
    > > }
    > > --------------------------------------------------------------
    > >
    > > I get all informations i want but there is still a error which i don't
    > > know
    > > how to fix:
    > >
    > > Objects in node nodename1
    > > ********************************************
    > > Smart Array 5x and 6x Notification Driver
    > > 6.4.0.32
    > > -------------------------------------
    > > Smart Array 5300 Controller
    > > 5.80.0.32
    > > -------------------------------------
    > > You cannot call a method on a null-valued expression.
    > > At line:9 char:34
    > > + if($devicename.Startswith <<<< ("Smart Array")){
    > >
    > >
    > > Objects in node nodename2
    > > ********************************************
    > > Smart Array 5x and 6x Notification Driver
    > > 6.4.0.32
    > > -------------------------------------
    > > Smart Array 5300 Controller
    > > 5.80.0.32
    > > -------------------------------------
    > > You cannot call a method on a null-valued expression.
    > > At line:9 char:34
    > > + if($devicename.Startswith <<<< ("Smart Array")){
    > >
    > > It looks like $devicename is empty at the end and the script wants still
    > > make a loop...
    > > Hope someone can help me getting this error away :-)
    > >
    > > greetings
    > >
    > > PS: Sorry for my english ;-)
    >
    >
    >

      My System SpecsSystem Spec

method Error in foreach loop problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is my foreach loop only looping through the first object in th Jacob Sampson PowerShell 1 30 Sep 2008
Remove from CSV file using a foreach loop Paul Farrimond PowerShell 2 20 Aug 2008
Foreach and if else loop Jsimpson PowerShell 2 17 Jun 2008
Foreach loop over $NULL in Powershell Jacobu9 PowerShell 2 22 Apr 2007
Output of Powershell ForEach Loop Morkcallorson PowerShell 1 15 Jan 2007