![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||