将不同语句中的列合并到一个变量中

时间:2019-01-22 14:25:14

标签: azure powershell networking merge virtual-machine

我正在为客户端准备包含以下信息的VM报告:

$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
    }
}

此语句后,输出另存为CSV文件:

$vmOutput | sort "Environment", "VM Type", "VM Profile", "Application" | export-csv VMReport.csv -delimiter ";" -force -notypeinformation 

我想将每个VM的专用IP地址添加到此报告中。我发现获得此类信息的唯一解决方案是通过以下语句:

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"
}

如何在export-csv命令之前将每个VM的IP信息解析到$ vmOutput变量中,以便将两个语句输出都包含到一个文件中?

1 个答案:

答案 0 :(得分:1)

在定义$ vmOutput时,添加一个占位符字段(例如"Private IP" ="")。

循环ip时,可以将vm nameip address添加到哈希表中。

最后,您可以迭代$ vmOutput,如果vm name与哈希表中的变量匹配,则可以使用哈希表中存储的ip address替换$ vmOutput中的变量。

如下所示的示例代码,可以在我这边工作:

$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