Arrays 如何使用调用表达式在模板内使用run命令?
我正在为Arista switch configs创建一个文本文件模板,我刚刚研究了如何使用Invoke表达式替换模板中的变量 现有的解决方案适用于奇异变量,但我也希望使用数组,稍后,我将使用一些ForEach循环 我需要知道如何让这个方法在我的模板中实际执行操作,或者,在将“调用的表达式”转储到文件之前编辑它 当前脚本片段:Arrays 如何使用调用表达式在模板内使用run命令?,arrays,powershell,Arrays,Powershell,我正在为Arista switch configs创建一个文本文件模板,我刚刚研究了如何使用Invoke表达式替换模板中的变量 现有的解决方案适用于奇异变量,但我也希望使用数组,稍后,我将使用一些ForEach循环 我需要知道如何让这个方法在我的模板中实际执行操作,或者,在将“调用的表达式”转储到文件之前编辑它 当前脚本片段: #Variables $peerlink1int = "49" $stamps = @("208","209","210","211") $filename = ('S
#Variables
$peerlink1int = "49"
$stamps = @("208","209","210","211")
$filename = ('SwitchConfig' + $(get-date -f 'MM-dd-yyyy HH-mm'))
$destination_file = ".\$filename.txt"
$Base = Get-Content '.\Arista\01AristaBase.txt' | out-string
$revisedBase = Invoke-Expression "`"$Base`""
$revisedBase | Out-File -append $destination_file
interface Ethernet $peerlink1int
switchport trunk allowed vlan $Stamps[0]-$Stamps[-1],1111,1234
源文件片段:
#Variables
$peerlink1int = "49"
$stamps = @("208","209","210","211")
$filename = ('SwitchConfig' + $(get-date -f 'MM-dd-yyyy HH-mm'))
$destination_file = ".\$filename.txt"
$Base = Get-Content '.\Arista\01AristaBase.txt' | out-string
$revisedBase = Invoke-Expression "`"$Base`""
$revisedBase | Out-File -append $destination_file
interface Ethernet $peerlink1int
switchport trunk allowed vlan $Stamps[0]-$Stamps[-1],1111,1234
输出结果:
interface Ethernet 49
switchport trunk allowed vlan 208 209 210 212[0]-208 209 210 211[-1],1111,1234
我所期望的是:
interface Ethernet 49
switchport trunk allowed vlan 208-211,1111,1234
EDIT:顺便说一句,向我提出的另一个方法没有使用Invoke表达式。(无论我走到哪里,都有人说它使用起来不安全。)然而,另一种方法只是替换文本。
我需要一种编辑文本模板的方法,允许我对生成的文本执行一些命令
例如,我需要一个ForEach循环,其模板文本为:
interface Vlan$stamps
description V$stamps
ip address 192.168.$stamps.2/25
vrrp $stampnum ip 192.168.$stamps.1
vrrp $stampnum description Stamp$stamps
并为数组$stamps中的每个项目循环一次,以便您可以获得:
interface Vlan208
description V208
ip address 192.168.208.2/25
vrrp $stampnum ip 192.168.208.1
vrrp $stampnum description Stamp208
interface Vlan209
description V209
ip address 192.168.209.2/25
vrrp $stampnum ip 192.168.209.1
vrrp $stampnum description Stamp209
interface Vlan210
description V210
ip address 192.168.210.2/25
vrrp $stampnum ip 192.168.210.1
vrrp $stampnum description Stamp210
interface Vlan211
description V211
ip address 192.168.211.2/25
vrrp $stampnum ip 192.168.211.1
vrrp $stampnum description Stamp211
这是我对您的不同场景的建议 注意:您不应该使用Invoke Expression,但我想根据您以前的请求传递信息,您想看看如果不使用-
replace
,它将如何工作。我提供了-replace
,在使用调用表达式之前应该考虑这一点
1。替换两个索引项
可以使用字符串上的-replace
方法替换两个索引项。对于这些示例,您可以使用Get Content从文件中读取数据,执行此操作时,请确保将该数据传输到Out String
。否则,新行和其他特殊字符似乎消失了
$stampArray=@(“208”、“209”、“210”、“211”)
$peerlink1int=“49”
$template='接口以太网$peerlink1int
switchport中继允许vlan$Stamps[0]-$Stamps[-1],11111 234'
#或$template=获取文件内容|输出字符串
#你不仅要逃出美元,还要逃出[和]。
$template-替换“\$peerlink1int”、$peerlink1int`
-替换“\$Stamps\[0\]”,$stampArray[0]`
-替换“\$Stamps\[-1\]”,$stampArray[-1]`
|输出文件C:\temp\replaced.txt
或
从上述两个示例中获得的输出为:
interface Ethernet 49
switchport trunk allowed vlan 208-211,1111,1234
2。替换数组中的多个值
有两种方法。1是调用表达式
,它可以调用脚本中的危险表达式,另一个是-replace
方法
$stampArray=@(“208”、“209”、“210”、“211”)
$template='接口Vlan$stamps
说明V$邮票
ip地址192.168.$stamps.2/25
vrrp$stampnum ip 192.168.$stamps.1
vrrp$stampnum说明邮票$stamps'
#使用调用表达式
$stampnum=1
$result=@()
foreach($stampArray中的邮票){
$result+=调用表达式“`$template`”#$stamps和$stampnum将在文档中被替换。
$stampnum++
}
#也可以使用replace命令
$stampnum=1
$result=@()
foreach($stampArray中的邮票){
$result+=$template-替换“\$stamps”、$stamps-替换“\$stampnum”、$stampnum++
}
$result |输出文件C:\temp\replaced.txt
而且,您得到的输出是
interface Vlan208
description V208
ip address 192.168.208.2/25
vrrp 1 ip 192.168.208.1
vrrp 1 description Stamp208
interface Vlan209
description V209
ip address 192.168.209.2/25
vrrp 2 ip 192.168.209.1
vrrp 2 description Stamp209
interface Vlan210
description V210
ip address 192.168.210.2/25
vrrp 3 ip 192.168.210.1
vrrp 3 description Stamp210
interface Vlan211
description V211
ip address 192.168.211.2/25
vrrp 4 ip 192.168.211.1
vrrp 4 description Stamp211
- 的确
- 在可扩展字符串(
“…”
)中,只有简单的变量引用可以直接嵌入(例如,$stamps
),为了嵌入表达式(包括属性访问和索引),您需要将表达式作为一个整体封装在$(…)
中(例如,$($stamps[0])
)-请参阅
- 您最终要寻找的是,PowerShell通过
$ExecutionContext.InvokeCommand.ExpandString()
按需将逐字(文字)字符串扩展为可扩展字符串,从而提供了它(有些模糊)
$revisedBase
现在包含以下内容,这正是您所期望的:
接口以太网
交换机端口中继允许的vlan 208-211111234
一个简单的演示,您也可以在循环中使用该技术,,然后在按需扩展中使用当前变量值:
$template = '`$foo is now: $foo'
foreach ($foo in 1..3) {
$ExecutionContext.InvokeCommand.ExpandString($template)
}
上述印刷品:
$foo现在是:1
$foo现在是:2
$foo现在是:3
我不确定问题到底出在哪里。如果可能的话,还有错误消息,您是否有特殊问题?请查看“输出结果”与“我所期望的”文件01AristaBase.txt是源文件片段的来源,调用表达式不考虑数组[0]和[-1]。它只需将整个阵列打印两次。以下是对您所做操作的警告。您正在读取字符串并在其上运行invoke expression。您可以读入变量,但[x]被视为字符串的一部分,而不是变量。除非您可以在文本文件中执行$($stamps[0]),否则它将无法与invoke expression一起工作,事实似乎就是这样。我认为,也许“调用表达式”以及最初的替换方法无法实现我的最终目标。我的目标是创建5或6个配置模板部分,其中大部分包含纯文本,但也包含一些变量。然后,我想用新值替换变量,或者执行类似于上面的foreach循环的操作。我想知道我是否应该制作一堆小的.ps1文件并使用“echo”来代替??我诚实地处理各种模板/ini文件,这些文件必须根据特定的要求进行更新。我在模板文件中使用-replace并将其替换为-replace。不适用于大内容,但适用于小文件Ihave@n0ttsweet:明白了。一些技巧:使用Get Content-Raw
将文件全文读取,作为一个多行字符串-管道输出到-