IF 语句基于 foreach 循环内的数组项
IF statements based on array item inside a foreach loop
我有三个变量,每个变量都从各自的文件中获取内容。这三个变量在一个数组中(称为 $array)。我是 运行 一个 foreach 循环来查看数组,另一个是根据每个文本文件的每一行打印一串命令。
一切正常,除了,现在我必须添加一个 if 语句来根据地址文件的来源写主机一行(我对 if ($_ -like '*beta3*') {write-Host "True"}
行的尝试没有成功. 主要思路是根据数据类型添加注释字符串--每个文件是一个'data type'.
$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"
}}
这是当前输出的演示(可能让我更好地了解我正在尝试做什么)...只缺少 set comment from_beta3_file
行,因为这些来自该文件:
edit "www.betaXyahoo.com"
set type fqdn
set fqdn "www.betaXyahoo.com"
set visibility disable
next
edit "www.betaXsimbrano.com"
set type fqdn
set fqdn "www.betaXsimbrano.com"
set visibility disable
next
$array
并不真正关心变量的名称 ($beta3addresses
),它曾经引用它现在保存的数据,所以除非 betarun3.txt
文件实际上包含其中包含字符串 "beta3" 的一行,您的 if()
语句将始终 return false.
有几种方法可以解决这个问题,我可能会使用哈希表(如果文件始终不变):
$Addresses = @{
'beta1' = Get-Content '.\betarun1.txt'
'beta2' = Get-Content '.\betarun2.txt'
'beta3' = Get-Content '.\betarun3.txt'
}
Write-Host "config this address"
foreach ($Key in $Addresses.Keys){
foreach ($address in $Addresses[$Key]){
Write-Host "edit ""$address"""
Write-Host "set type fqdn"
if ($Key -eq 'beta3') {
Write-Host "set comment from_beta3_file"
}
Write-Host "set fqdn ""$address"""
Write-Host "set visibility disable"
Write-Host "next"
}
}
如果文件数量不同,您可以检索有关该文件的信息并改用该信息:
foreach($File in Get-ChildItem -Path $PWD -Filter beta*.txt){
foreach($address in Get-Content $File.FullName){
if($File.Name -like '*beta3*'){ # do your thing }
}
}
我有三个变量,每个变量都从各自的文件中获取内容。这三个变量在一个数组中(称为 $array)。我是 运行 一个 foreach 循环来查看数组,另一个是根据每个文本文件的每一行打印一串命令。
一切正常,除了,现在我必须添加一个 if 语句来根据地址文件的来源写主机一行(我对 if ($_ -like '*beta3*') {write-Host "True"}
行的尝试没有成功. 主要思路是根据数据类型添加注释字符串--每个文件是一个'data type'.
$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"
}}
这是当前输出的演示(可能让我更好地了解我正在尝试做什么)...只缺少 set comment from_beta3_file
行,因为这些来自该文件:
edit "www.betaXyahoo.com"
set type fqdn
set fqdn "www.betaXyahoo.com"
set visibility disable
next
edit "www.betaXsimbrano.com"
set type fqdn
set fqdn "www.betaXsimbrano.com"
set visibility disable
next
$array
并不真正关心变量的名称 ($beta3addresses
),它曾经引用它现在保存的数据,所以除非 betarun3.txt
文件实际上包含其中包含字符串 "beta3" 的一行,您的 if()
语句将始终 return false.
有几种方法可以解决这个问题,我可能会使用哈希表(如果文件始终不变):
$Addresses = @{
'beta1' = Get-Content '.\betarun1.txt'
'beta2' = Get-Content '.\betarun2.txt'
'beta3' = Get-Content '.\betarun3.txt'
}
Write-Host "config this address"
foreach ($Key in $Addresses.Keys){
foreach ($address in $Addresses[$Key]){
Write-Host "edit ""$address"""
Write-Host "set type fqdn"
if ($Key -eq 'beta3') {
Write-Host "set comment from_beta3_file"
}
Write-Host "set fqdn ""$address"""
Write-Host "set visibility disable"
Write-Host "next"
}
}
如果文件数量不同,您可以检索有关该文件的信息并改用该信息:
foreach($File in Get-ChildItem -Path $PWD -Filter beta*.txt){
foreach($address in Get-Content $File.FullName){
if($File.Name -like '*beta3*'){ # do your thing }
}
}