В соответствии с ошибкой: сбой вызова метода, поскольку [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"
}
}
}