I have this script to export VM Tags from azure but when i export the csv file the tags values coulmn has System.Object instead of the tag key and its value. Your kind help would be great
# Sign into Azure Portal
connect-azaccount
# Fetch the Virtual Machines from the subscription
$azureVMDetails = get-azvm
# Fetch the NIC details from the subscription
$azureNICDetails = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}
#Fetching Virtual Machine Details
$virtual_machine_object = $null
$virtual_machine_object = @()
#Iterating over the NIC Interfaces under the subscription
foreach($azureNICDetail in $azureNICDetails){
#Fetching the VM Name
$azureVMDetail = $azureVMDetails | ? -Property Id -eq $azureNICDetail.VirtualMachine.id
#Fetching the VM Tags
$vm_tags = Get-AzVM | Select-Object -Property (
@{name='Tags'; expression = {($_.tags.GetEnumerator().ForEach({ '{0} = {1}' -f $_.key, $_.value }) -join ',')}}
)
#VM Details export
$virtual_machine_object_temp = new-object PSObject
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "name" -Value $azureVMDetail.Name
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "comments" -Value $vm_tags
$virtual_machine_object = $virtual_machine_object_temp
}
#Report format and path
$virtual_machine_object | Export-Csv "C:\Users\JOHN\Desktop\Inventory_$(get-date -f dd.MM.yyyy).csv" -NoTypeInformation -Force
CodePudding user response:
Make sure the property value you're adding is the actual Tags
strings you generated in the previous step by passing $vm_tags.Tags
rather than just $vm_tags
.
Additionally, as there might be more than one VM returned by Get-AzVM
, you'll want to -join
all the resulting values together at the end:
$virtual_machine_object_temp | Add-Member -MemberType NoteProperty -Name "comments" -Value ($vm_tags.Tags -join ';')