Home > Mobile >  Replacing Azure VMs location names with custom short names
Replacing Azure VMs location names with custom short names

Time:10-29

With this code i want to fetch locations of the VMs in Azure and then replace the locations with short names as shown in hash table. At the end i want to have them in a csv format. But getting the error of System.Collections.Hashtable How can i possibly do it as i am not a programmer and know very little about PowerShell scripting. function get-locations {

$vmDetails = Get-AzVM

$locations = $vmDetails.location

$locations = @ {

"westeurope"         = "weu"

"northeurope"        = "neu"

"germanywestcentral" = "gwc
"

}

$locations | Export-Csv "C:\Azure Virtual Machines details_$(Get-Date -f dd.MM.yyyy).csv" -NoTypeInformation -Force }

CodePudding user response:

In it's simplest form could this be what you are looking for:

$vmDetails = Get-AzVM
$locations = $vmDetails.location                                                                                                     
$THEHASHMAP = @{ "westeurope"  = "weu"; "northeurope" = "neu"; "germanywestcentral" = "gwc"; "centralus" = "cus"}                   
@( foreach ($vm in $vmDetails) {  @{Location=$THEHASHMAP[$vm.location]; VMNAME=$vm.Name} }) | Export-Csv "test4.csv"                        
cat test4.csv                                                                                                    
"VMNAME","Location"
"VM3bvgnig756w5s","cus"
"Dev-Portal-01","weu"

Credits to this previous answer regarding table creation

  • Related