强制Powershell将重复值添加到主报表中

问题描述 投票:0回答:1

我有一个创建公共IP报告的脚本。我的一个虚拟机有两个网卡连接到我的问题。我的脚本是以某种方式设计的,即VM名称是唯一键,所有其他数据都将基于此键关联并分配给主表。有什么办法可以解决这个问题?这是我的脚本:

#Data collection
$PIP = Get-AzPublicIpAddress

$pipOutput = $PIP | ForEach-Object {
[PSCustomObject]@{
"IP Name" = $_.Name
"IP Address" = $_.IpAddress
"Resource Group Name" = $_.ResourceGroupName
"Location" = $_.Location
"VM Name" = ""
"Network Interface" = ""
"Application" = $_.Tag.Application
"Environment" = $_.Tag.Environment
"Role" = $_.Tag.Role
"Decommission" = $_.Tag.Decommission
"Funding" = $_.Tag.Funding
"Provisioning State" = $_.ProvisioningState
"Allocation Method" = $_.PublicIpAllocationMethod
"Version" = $_.PublicIpAddressVersion
"Idle Timeout In Minutes" = $_.IdleTimeoutInMinutes
}
}

$VMs = Get-AzVm -status
$all = @{}
$end = @{}

foreach ($vm in $VMs) {
    $VMNetworkInterfaceSplitToArray = $vm.NetworkProfile.NetworkInterfaces.id.Split('/')
    $VMNetworkInterfaceObject = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $VMNetworkInterfaceSplitToArray[$VMNetworkInterfaceSplitToArray.Count - 1]
    try {
        $VMPublicIpSplitToArray = ($VMNetworkInterfaceObject.IpConfigurations.publicipaddress.Id).Split('/')
        $vm_Public_IP = (Get-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $VMPublicIpSplitToArray[$VMPublicIpSplitToArray.Count - 1]).IpAddress 
    }
    catch {
        $vm_Public_IP = "n/a"
    }

    if($vm_Public_IP -ne "n/a")
    {
    $all.Add($vm_Public_IP,$vm.name)
    $end.Add($vm_Public_IP,$VMNetworkInterfaceObject.name)
    }

    }

foreach ($ipadd in $pipOutput)
{
    if($all.ContainsKey($ipadd."IP Address"))
    {
        $ipadd."VM Name"=$all[$ipadd."IP Address"]
    }
    else
    {
        $ipadd."VM Name"="n/a"
    }
}

foreach ($ipadd in $pipOutput)
{
    if($end.ContainsKey($ipadd."IP Address"))
    {
        $ipadd."Network Interface"=$end[$ipadd."IP Address"]
    }
    else
    {
        $ipadd."Network Interface"="n/a"
    }
}
azure powershell azure-virtual-machine
1个回答
1
投票

根据错误:方法调用失败,因为[System.Management.Automation.PSObject]不包含名为'op_Addition'的方法,建议您创建一个数组来保存所有PSCustomObject。例如

Connect-AzAccount

$PIP = Get-AzPublicIpAddress

$pipOutput = $PIP | ForEach-Object {
[PSCustomObject]@{
"IP Name" = $_.Name
"IP Address" = $_.IpAddress
"Resource Group Name" = $_.ResourceGroupName
"Location" = $_.Location
"VM Name" = ""
"Network Interface" = ""
"Application" = $_.Tag.Application
"Environment" = $_.Tag.Environment
"Role" = $_.Tag.Role
"Decommission" = $_.Tag.Decommission
"Funding" = $_.Tag.Funding
"Provisioning State" = $_.ProvisioningState
"Allocation Method" = $_.PublicIpAllocationMethod
"Version" = $_.PublicIpAddressVersion
"Idle Timeout In Minutes" = $_.IdleTimeoutInMinutes
}
}

$vms =Get-AzVM 
$all =@()
Foreach($vm in $vms){

    $VMNetworkInterfaceToArray = $vm.NetworkProfile.NetworkInterfaces.id
     foreach($r in $VMNetworkInterfaceToArray ){

        $resource = Get-AzResource -ResourceId $r
        $VMNetworkInterfaceObject=Get-AzNetworkInterface -Name $resource.Name -ResourceGroupName $resource.ResourceGroupName
        try{
        $VMPublicIp=Get-AzResource -ResourceId $VMNetworkInterfaceObject.IpConfigurations.publicipaddress.Id
         $vm_Public_IP = (Get-AzPublicIpAddress -ResourceGroupName $VMPublicIp.ResourceGroupName -Name $VMPublicIp.Name).IpAddress 
         }catch {
            $vm_Public_IP = "n/a"
         }

         if($vm_Public_IP -ne "n/a")
        {
             #create custom object to save the details your need
             $obj= [PSCustomObject]@{"VmName"=$vm.Name
                         "VMPublicIP"=$vm_Public_IP
                         "VMNetworkInterface"=$VMNetworkInterfaceObject.Name
                       }
             # save the object into array
             $all +=$obj

        }
     }
}
Foreach($ipadd in $pipOutput){

   foreach($a in $all){

     if($a.VMPublicIP=$ipadd.'IP Address'){
       $ipadd.'VM Name' =$a.VmName
       $ipadd.'Network Interface'=$a.VMNetworkInterface

     }else{

       $ipadd.'VM Name' ="n/a"
       $ipadd.'Network Interface'="n/a"

     }


   }

}


更新

请按以下方式更改脚本

Foreach($ipadd in $pipOutput){

   foreach($a in $all){

     if($ipadd.'IP Address'.Equals($a.VMPublicIP)){

     $ipadd.'VM Name' =$a.VmName
       $ipadd.'Network Interface'=$a.VMNetworkInterface

     }else{

       $ipadd.'VM Name' ="n/a"
       $ipadd.'Network Interface'="n/a"

     }

   }

}
© www.soinside.com 2019 - 2024. All rights reserved.