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. #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

  3. #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

  4. #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

  5. #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

  6. #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
$domain = new-object system.directoryservices.directoryentry("LDAP://ou=domain controllers,dc=mhco,dc=mizehouser,dc=com") $searcher = new-object...
PowerShell
Can't append date to file name through a ForEach Loop?
Can you guys help me out and tell whats wrong with this: $curDate = get-date -uformat "%Y-%m-%d.%H.%M.%S" $test = "C:\test" $GCI = Get-ChildItem...
PowerShell
Remove from CSV file using a foreach loop
I have a list of users in a seperate text file that I want to exclude from the csv file. E.g. List.txt: User1 User3 But if I...
PowerShell
Foreach and if else loop
Ok, I am missing something here... I want to do a loop to check for three things from a ping-host: a reply ("1"), no reply ("0"), and failed to...
PowerShell
method Error in foreach loop
Hi I wrote a PS script which gets registry informations about specific drivers. Here is the code:...
PowerShell
Foreach loop over $NULL in Powershell
I've noticed that in powershell, if I run a foreach loop over a null valued expression, the loop runs at least once. The following code illustrates...
PowerShell
Output of Powershell ForEach Loop
I'm trying to query my root domain for all it's users and produce a csv file that includes the user name and desription. I can use the write-host...
PowerShell