Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Help with foreach-object and passing parameters

Reply
 
Old 11-15-2006   #1 (permalink)
Xanbar


 
 

Help with foreach-object and passing parameters

I'm new to powershell and I'm not quite getting the syntax to pass
parameters. I want to ping a list of servers that are listed in a text
file, and output the results to html...What I've figured out so far is:

import-csv servers.csv | foreach-object -process {get-wmiobject
win32_pingstatus -filter "address='www.yahoo.com'" | select-object
-property address,statuscode,responsetime} | convertto-html

That runs and loops the correct amount of times, and pings the
hard-coded www.yahoo.com. How do I replace the www.yahoo.com with the
values from my file?
Servers.csv contains:

Server
www.yahoo.com
www.google.com
www.lycos.com

I tried:

import-csv servers.txt | foreach-object -process {get-wmiobject
win32_pingstatus -filter "address='www.yahoo.com'"
| select-object -property address,statuscode,responsetime}

But that tried to ping @{Server=www.yahoo.com}, for example.

I did search pretty extensively and have suprisingly not found anyone
else posting about this same exact question.

Any help greatly appreciated!!!


My System SpecsSystem Spec
Old 11-15-2006   #2 (permalink)
Keith Hill [MVP]


 
 

Re: Help with foreach-object and passing parameters


"Xanbar" <darren@hackandchop.com> wrote in message news:1163577149.624753.246340@h54g2000cwb.googlegroups.com...
> I'm new to powershell and I'm not quite getting the syntax to pass
> parameters. I want to ping a list of servers that are listed in a text
> file, and output the results to html...What I've figured out so far is:
>
> import-csv servers.csv | foreach-object -process {get-wmiobject
> win32_pingstatus -filter "address='www.yahoo.com'" | select-object
> -property address,statuscode,responsetime} | convertto-html
>
> That runs and loops the correct amount of times, and pings the
> hard-coded www.yahoo.com. How do I replace the www.yahoo.com with the
> values from my file?


Try this:

import-csv servers.csv |
foreach {gwmi Win32_PingStatus -filter "Address='$($_.Server)'"} |
select address,statuscode,responsetime

address statuscode responsetime
------- ---------- ------------
www.yahoo.com 0 37
www.google.com 0 36
www.lycos.com 0 120

--
Keith
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Passing parameters to a PS script PowerShell
passing [switch] parameters PowerShell
foreach, foreach-object & begin/process/end scriptblock clauses... PowerShell
Difference in semantics of for, foreach and foreach-object PowerShell
passing parameters to script (ps1) PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46