• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

modifying propertie values without foreach loop

R

Rudi Bruchez

#1
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 Computer

T

Tao Ma

#2
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 Computer

M

Martin Zugec

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

R

Rudi Bruchez

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

K

Karl Prosser[MVP]

#6

> 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 Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)