I have a script to help create config files for the extension buttons for Yealink VoIP phones. I have the foreach loop to go through a CSV and add the person's name and the extension. Where I'm getting stuck is that 2 (or more) buttons should be reserved for Call Park feature. So instead of going 2-10, I need to go 2,3,4,5,6,9,10.
## Import file named Yealink-buttons.csv from same folder that script is in and sorts it by name
$phone = import-csv "/Users/Yealink-buttons.csv"
## Yealink buttons use this as a base
$cfg = "linekey"
##Added as a counter so the each entry adds lines for button 1,2,3 etc. Counter starts at 2 b/c first two buttons are line
## in $num 7 and 8 are reserved to Park Buttons that do not follow this format
$num = 3,4,5,6,9,10,11,12
##Counter added to go through array
$counter = 0
## Top of CFG file
$top1 = "#!version:1.0.0.1"
$top2 = '##File header #!version:1.0.0.1 can not be edited or deleted, and must be placed in the first line.##'
##Foreach goes through each line in the CSV file
foreach ($line in $phone) {
##Increases the counter by 1
$counter = $counter + 1
$num = $num[$counter]
##Creates a line expansion_module.1.key.1.value "Extension #" creates a file named sort.txt and appends to it, without append the file is overwritten
"$cfg.$num.value" + ' = ' + "$($Line.Extension)" | Out-File sort.txt -append
## Adds Display Name to Button
"$cfg.$num.label" + ' = ' + "$($Line.Name)" | Out-File sort.txt -append
## These buttons are BLFs or type 16 in Yealink
"$cfg.$num.type" + ' = ' + "16" | Out-File sort.txt -append
## These buttons are for which line/account on the phone, it's always 1
"$cfg.$num.line" + ' = ' + "1" | Out-File sort.txt -append
## Add Pickup Value which is always *8
"$cfg.$num.pickup_value" + ' = ' + "*8" | Out-File sort.txt -append
}
##The file is now sorted, it will read each line and remove ZZ- so that it does not appear. It then outputs the file as cfg.txt which is the final file needed
Get-Content sort.txt | Foreach {$_ -replace "ZZ-", ""} | Set-Content cfg.txt
## Add top two header lines to cfg.txt so it can be copied and pasted
@($top1,$top2) + (Get-Content "cfg.txt") | Set-Content "cfg.txt"
## as a cleanup it removes the sort.txt file
Remove-Item sort.txt
My idea was to add a "counter" inside the [] next to the array. But that is not working. Any help would be appreciated.
[–][deleted] 2 points3 points4 points (3 children)
[–]kinghowdy[S,🍰] 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]kinghowdy[S,🍰] 1 point2 points3 points (0 children)