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 - using foreach-object as a parameter for another script

Reply
 
Old 09-24-2009   #1 (permalink)
Ranga


 
 

using foreach-object as a parameter for another script

Get-Content c:\Admin\PS\servers.txt | Foreach-Object

I have list of servernames stored in servers.txt file, for each server, i
want to pass the servername as a parameter to another .ps1 script, How ?

Advance thanks for your replies

My System SpecsSystem Spec
Old 09-24-2009   #2 (permalink)
Chris Dent


 
 

Re: using foreach-object as a parameter for another script

Ranga wrote:
Quote:

> Get-Content c:\Admin\PS\servers.txt | Foreach-Object
>
> I have list of servernames stored in servers.txt file, for each server, i
> want to pass the servername as a parameter to another .ps1 script, How ?
>
> Advance thanks for your replies
Like this:

Get-Content C:\Admin\Ps\Servers.txt | ForEach-Object { Write-Host $_ }

In the example above, you would replace Write-Host with whatever you
want to call. $_ is the current object in the pipeline and will be an
individual server name.

Chris
My System SpecsSystem Spec
Old 09-24-2009   #3 (permalink)
tojo2000


 
 

Re: using foreach-object as a parameter for another script

On Sep 24, 11:51*am, Ranga <Ra...@newsgroup> wrote:
Quote:

> Get-Content c:\Admin\PS\servers.txt | Foreach-Object
>
> I have list of servernames stored in servers.txt file, for each server, i
> want to pass the servername as a parameter to another .ps1 script, How ?
>
> Advance thanks for your replies
In general, you've almost got it. Get-Content returns an array with
one line per array (after stripping newlines from the end). When it
sends it down the pipeline, Foreach-Object will assign it to the
temporary variable $_.

Get-Content c:\Admin\PS\servers.txt | Foreach-Object {some_script.ps1
$_}

My System SpecsSystem Spec
Old 09-25-2009   #4 (permalink)
Ranga


 
 

Re: using foreach-object as a parameter for another script

thank you.

"tojo2000" wrote:
Quote:

> On Sep 24, 11:51 am, Ranga <Ra...@newsgroup> wrote:
Quote:

> > Get-Content c:\Admin\PS\servers.txt | Foreach-Object
> >
> > I have list of servernames stored in servers.txt file, for each server, i
> > want to pass the servername as a parameter to another .ps1 script, How ?
> >
> > Advance thanks for your replies
>
> In general, you've almost got it. Get-Content returns an array with
> one line per array (after stripping newlines from the end). When it
> sends it down the pipeline, Foreach-Object will assign it to the
> temporary variable $_.
>
> Get-Content c:\Admin\PS\servers.txt | Foreach-Object {some_script.ps1
> $_}
>
>
My System SpecsSystem Spec
Old 09-25-2009   #5 (permalink)
Vadims Podans [MVP]


 
 

Re: using foreach-object as a parameter for another script

With PowerShell V2 you can type just:

Get-Content servers.txt | .\some_script.ps1

What you need to make it workable? For the particular argument you should
allow pipeline input for this argument and put code into Process {}
scriptoblock. Here is your some_script.ps1 file example:
param (
[Parameter(ValueFromPipeline = $true)][string]$server
)
process {
# here must be placed all necessary code
}
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"tojo2000" <tojo2000@newsgroup> rakstija zinojuma
"news:ae512382-59c6-49aa-a77b-d9106ec97a40@newsgroup"...
Quote:

> On Sep 24, 11:51 am, Ranga <Ra...@newsgroup> wrote:
Quote:

>> Get-Content c:\Admin\PS\servers.txt | Foreach-Object
>>
>> I have list of servernames stored in servers.txt file, for each server, i
>> want to pass the servername as a parameter to another .ps1 script, How ?
>>
>> Advance thanks for your replies
>
> In general, you've almost got it. Get-Content returns an array with
> one line per array (after stripping newlines from the end). When it
> sends it down the pipeline, Foreach-Object will assign it to the
> temporary variable $_.
>
> Get-Content c:\Admin\PS\servers.txt | Foreach-Object {some_script.ps1
> $_}
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Oddity with ForEach-Object or is it me? PowerShell
foreach, foreach-object & begin/process/end scriptblock clauses... PowerShell
Passing a HashTable object as a parameter to a script PowerShell
Difference in semantics of for, foreach and foreach-object PowerShell
Peculiarity/bug of foreach-object 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