Windows Vista Forums

modifying propertie values without foreach loop

  1. #1


    Rudi Bruchez Guest

    modifying propertie values without foreach loop

    Hello,

    I am doing this to change properties without a foreach loop :

    $sql = New-Object ('Microsoft.sqlserver.management.smo.server') .\SQL2005
    $sql.Logins | where {$_.DefaultDatabase -eq "master"} | where
    {$_.DefaultDatabase = "AdventureWorks"}

    is this safe, and is there a better way to do it ?

    --
    Rudi Bruchez
    Consultant independant, MCDBA, MCITP, MCT, MVP SQL Server
    http://www.babaluga.com/
    http://rudi.developpez.com/

      My System SpecsSystem Spec

  2.   


  3. #2


    Tao Ma Guest

    Re: modifying propertie values without foreach loop

    Hi Rudi,
    $sql.Logins | where {$_.DefaultDatabase -eq "master"} | ForEach-Object
    {$_.DefaultDatabase = "AdventureWorks"}

    I think using ForEach-Objects is much better. Where-Object is designed to
    select items from the pipeline and ForEach-Object is designed to do
    something for each item in the pipeline.

    Best regards,
    Tao Ma

    "Rudi Bruchez" <rudi#nospam#at#babaluga.com>
    ??????:%238fhmto3IHA.4500@xxxxxx

    > Hello,
    >
    > I am doing this to change properties without a foreach loop :
    >
    > $sql = New-Object ('Microsoft.sqlserver.management.smo.server') .\SQL2005
    > $sql.Logins | where {$_.DefaultDatabase -eq "master"} | where
    > {$_.DefaultDatabase = "AdventureWorks"}
    >
    > is this safe, and is there a better way to do it ?
    >
    > --
    > Rudi Bruchez
    > Consultant independant, MCDBA, MCITP, MCT, MVP SQL Server
    > http://www.babaluga.com/
    > http://rudi.developpez.com/


      My System SpecsSystem Spec

  4. #3


    Martin Zugec Guest

    Re: modifying propertie values without foreach loop

    Hmmmm, just out of curiosity, why not using ForEach??

    It makes code much more clear and easy to read... Specify with more complex
    scripts...

    Martin

    "Rudi Bruchez" <rudi#nospam#at#babaluga.com> wrote in message
    news:%238fhmto3IHA.4500@xxxxxx

    > Hello,
    >
    > I am doing this to change properties without a foreach loop :
    >
    > $sql = New-Object ('Microsoft.sqlserver.management.smo.server') .\SQL2005
    > $sql.Logins | where {$_.DefaultDatabase -eq "master"} | where
    > {$_.DefaultDatabase = "AdventureWorks"}
    >
    > is this safe, and is there a better way to do it ?
    >
    > --
    > Rudi Bruchez
    > Consultant independant, MCDBA, MCITP, MCT, MVP SQL Server
    > http://www.babaluga.com/
    > http://rudi.developpez.com/

      My System SpecsSystem Spec

  5. #4


    Rudi Bruchez Guest

    Re: modifying propertie values without foreach loop

    Hello,

    Martin Zugec a écrit:

    > Hmmmm, just out of curiosity, why not using ForEach??
    >
    > It makes code much more clear and easy to read... Specify with more
    > complex scripts...
    I guess I like syntactic sugar. One of my language of choice is Perl, I
    like its expressive capabilities. Bad habit for readability, I know.
    I will use the Foreach-Object, thanks Tao Ma, I am learning powershell,
    didn't know that.

    --
    Rudi Bruchez
    Consultant independant, MCDBA, MCITP, MCT, MVP SQL Server
    http://www.babaluga.com/
    http://rudi.developpez.com/

      My System SpecsSystem Spec

  6. #5


    Rudi Bruchez Guest

    Re: modifying propertie values without foreach loop

    In fact I just didn't think about piping to a foreach... stupid.

      My System SpecsSystem Spec

  7. #6


    Karl Prosser[MVP] Guest

    Re: modifying propertie values without foreach loop


    > Hmmmm, just out of curiosity, why not using ForEach??
    >
    you mean foreach{$x in y$)..

    thats useful to have that language support, but its not pipeline
    friendly and it has to be a separate statement. (so you can't do a one
    liner, and have other things around it). Its more about speaking with a
    powershell accent, rather than a vbscript accent.

    -Karl

      My System SpecsSystem Spec


modifying propertie values without foreach loop

Similar Threads
Thread Forum
Why is my foreach loop only looping through the first object in th PowerShell
Remove from CSV file using a foreach loop PowerShell
Foreach and if else loop PowerShell
method Error in foreach loop PowerShell
Foreach loop over $NULL in Powershell PowerShell