dimanche 27 septembre 2015

(Powershell) IF statements based on array item inside a foreach loop

I have three variables, each one getting content from their respective files. These three variables are in an array (called $array). I'm running a foreach loop to look at the array, and another one to print a string of commands based on each line coming for each text file.

All works well, except, now I have to add an if statement to write-host a line based on where the address file it came from (my attempt with the if ($_ -like '*beta3*') {write-Host "True"} line didn't pan out.

$beta1addresses = Get-Content betarun1.txt
$beta2addresses = Get-Content betarun2.txt
$beta3addresses = Get-Content betarun3.txt

$array = ($beta1addresses,$beta2addresses,$beta3addresses)

Write-Host "config this address"
foreach ($_ in $array){
foreach ($address in $_){
    Write-Host "edit ""$address"""
    Write-Host "set type fqdn"
if ($_ -like '*beta3*') {write-Host "set comment from_beta3_file"}
    Write-Host "set fqdn ""$address"""
    Write-Host "set visibility disable"
    Write-Host "next"
}}

Here's a demo of the current output (might give a better idea of what I'm trying to do)... only missing set comment from_beta3_file line since these came from that file:

edit "www.beta3yahoo.com"
set type fqdn
set fqdn "www.beta3yahoo.com"
set visibility disable
next
edit "http://ift.tt/1MSZUVD"
set type fqdn
set fqdn "http://ift.tt/1MSZUVD"
set visibility disable
next

Aucun commentaire:

Enregistrer un commentaire