问题
I'm preparing my VM report for the client consisting of this information:
$VMs = Get-AzureRmVM -status
$vmOutput = $VMs | ForEach-Object {
[PSCustomObject]@{
"VM Name" = $_.Name
"VM Type" = $_.StorageProfile.osDisk.osType
"VM Profile" = $_.HardwareProfile.VmSize
"Environment" = $_.Tags.Environment
"Application" = $_.Tags.Application
"Decommission Date" = $_.Tags.Decomission
"OS Disk Size" = $_.StorageProfile.OsDisk.DiskSizeGB
"Data Disks Total Size" = ($_.StorageProfile.DataDisks.DiskSizeGB | Measure -Sum).Sum
"Data Disks Amount" = ($_.StorageProfile.DataDisks | Measure ).Count
"Managed OS Disk" = ($_.StorageProfile.OSDisk.ManagedDisk | Measure).Count
"Managed Data Disks" = ($_.StorageProfile.DataDisks.ManagedDisk | Measure).Count
"Powerstate" = $_.PowerState
}
}
After this statement an output is saved as CSV file:
$vmOutput | sort "Environment", "VM Type", "VM Profile", "Application" | export-csv VMReport.csv -delimiter ";" -force -notypeinformation
I want to add a private IP addresses per VM to this report. Only solution I found to get such information is through this statement:
foreach($nic in $nics)
{
$vm = $vms | where-object -Property Id -EQ $nic.VirtualMachine.id
$prv = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAddress
Write-Output "$($vm.Name) : $prv"
}
How can I parse information about IP's per VM into $vmOutput variable before the export-csv command in order to include both statement outputs into one file?
回答1:
When Define $vmOutput, add a placeholder field(like "Private IP" =""
).
When loop the ips, you can add the vm name
and ip address
to a hashtable.
At last, you can iterate the $vmOutput, if the vm name
matches that in hashtable, then you can use the ip address
stored in hashtable to replace the one in $vmOutput.
Sample code like below, and works at my side:
$VMs = Get-AzureRmVM -ResourceGroupName "xxx" -Status
$vmOutput = $VMs | ForEach-Object {
[PSCustomObject]@{
"VM Name" = $_.name
"VM Type" = $_.StorageProfile.osDisk.osType
"VM Profile" = $_.HardwareProfile.VmSize
"Environment" = $_.Tags.Environment
"Application" = $_.Tags.Application
"Decommission Date" = $_.Tags.Decomission
"OS Disk Size" = $_.StorageProfile.OsDisk.DiskSizeGB
"Data Disks Total Size" = ($_.StorageProfile.DataDisks.DiskSizeGB | Measure -Sum).Sum
"Data Disks Amount" = ($_.StorageProfile.DataDisks | Measure ).Count
"Managed OS Disk" = ($_.StorageProfile.OSDisk.ManagedDisk | Measure).Count
"Managed Data Disks" = ($_.StorageProfile.DataDisks.ManagedDisk | Measure).Count
"Powerstate" = $_.PowerState
"Private IP" ="" #Add the placeholder
}
}
$vms_temp = Get-AzureRmVM -ResourceGroupName "xxx"
$nics = get-azurermnetworkinterface -ResourceGroupName "xxx"| where VirtualMachine -NE $null
$ips =@{} #define a hashtable to store vm name and it's private ip
foreach($nic in $nics){
$vm = $vms_temp | Where-Object -Property id -EQ $nic.VirtualMachine.id
$prv = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAddress
$ips.Add($vm.Name,$prv)
}
foreach($vm in $vmOutput)
{
#if vm name matches, you can use the ip address stored in hashtable to replace the one in $vmOutput
if($ips.ContainsKey($vm."VM Name"))
{
$vm."Private IP"=$ips[$vm."VM Name"]
}
}
$vmOutput | sort "Environment", "VM Type", "VM Profile", "Application" | export-csv d:\VMReport.csv -delimiter ";" -force -notypeinformation
来源:https://stackoverflow.com/questions/54310382/merge-columns-from-different-statements-into-one-variable