将不同语句中的列合并到一个变量中 [英] Merge columns from different statements into one variable

查看:106
本文介绍了将不同语句中的列合并到一个变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 

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

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

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

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?

推荐答案

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

When Define $vmOutput, add a placeholder field(like "Private IP" ="" ).

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

When loop the ips, you can add the vm name and ip address to a hashtable.

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

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 

这篇关于将不同语句中的列合并到一个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆