Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dictionary in Powershell on which I will iterate

I want to create a dictionary, on which later on I would like to iterate.
So the dictonary would look like this:

$dictionary = 
subnet |address
xyz | 10.1.2.0/24 
abc | 10.1.3.0/24
psh | 10.1.4.0/24

And i would like to iterate

foreach ($item in $dictionary){
$dmz = New-AzVirtualNetworkSubnetConfig -Name $item.subnet -AddressPrefix $item.address


}

Does anyone knows how to create such a dictionary/table? As i am not really familiar with powershell, but I would like to create a simple script

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’d recommend using CSV data as they are the esiest to edit by hand

$dictionary = 
@'
subnet,address
"xyz","10.1.2.0/24" 
"abc","10.1.3.0/24"
"psh","10.1.4.0/24"
'@ | 
ConvertFrom-Csv

foreach ($item in $dictionary) {
    $dmz = New-AzVirtualNetworkSubnetConfig -Name $item.subnet -AddressPrefix $item.address
}

Of course you could use a CSV file and import it with Import-CSV. This way you wouldn’t need to edit the script itself. 😉

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading