I am creating a text file template for Arista switch configs, and I just worked out how to replace variables in my templates using Invoke-Expression.
The existing solution works fine for singular variables, but I have arrays I'd like to use as well, and later on, I will have some ForEach loops.
I need to know how to get this method to actually perform the operations inside my template, or alternatively, edit the 'invoked expression' before dumping it to a file.
Current script snippet :
#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
Source file snippet :
interface Ethernet $peerlink1int
switchport trunk allowed vlan $Stamps[0]-$Stamps[-1],1111,1234
Output result :
interface Ethernet 49
switchport trunk allowed vlan 208 209 210 212[0]-208 209 210 211[-1],1111,1234
What I expected :
interface Ethernet 49
switchport trunk allowed vlan 208-211,1111,1234
EDIT : As an aside, there was another method proposed to me that did not use Invoke-Expression. (Everywhere I go someone says it's unsafe to use.) However, this other method merely replaces text. I need a method of editing a text template that allows me to perform some commands on the resulting text.
For example, I need a ForEach loop that takes a template text of :
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
And loops one time for every item in the array $stamps so you would get :
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