Formatting the Powershell script object output?

Senior Systems Engineer

The script does is to Go to each of the Azure Subscription and then list the VM properties and then create the PSObjects to capture the output in a meaningful and useful format.

Get-AzSubscription | ForEach-Object {
    $subscriptionId = $_.Id
    $subscriptionName = $_.Name
     
    Set-AzContext -SubscriptionId $subscriptionId

    $VM = Get-AzVM

    # Get NIC id
    $NICId = $VM.NetworkProfile.NetworkInterfaces[0].id
    
    # Get NIC
    $NIC = Get-AzNetworkInterface -ResourceId $NICId
    
    # Get public ip id
    $PIPId = $NIC.IpConfigurations.PublicIpAddress.id
    
    # Get public ip
    $PIP = Get-AzResource -ResourceId $PIPId
    
    # Output
    [pscustomobject]@{
        Subscription             = $subscriptionName
        VMName                   = $VM.Name
        VMRG                     = $VM.ResourceGroupName
        VMLocation               = $VM.Location
        IpAddress                = $PIP.Properties.ipAddress
        PublicIPAllocationMethod = $PIP.Properties.publicIPAllocationMethod
        FQDN                     = $pip.Properties.dnsSettings.fqdn
    }
} | Out-GridView

However, the result is mixed up and not in the correct format like the below:

Out-GridView

enter image description here

Format-Table -Autosize

Subscription             : Corp-Azure-Dev-subs
VMName                   : {prod1-build, corpbuild-image, corpplus-build-server, u2api...}
VMRG                     : {corp-prod1-BUILD, corp-PLUS-BUILD-SETUP, corp-PLUS-BUILD-SETUP, ERP-Acorp-DEV...}
VMLocation               : {eastus, east, centralus, westus...}
IpAddress                : 52.44.66.198
PublicIPAllocationMethod : Static
FQDN                     : 
Steven

I think you're outputting multiple object types. From the looks of it the line:

Set-AzContext -SubscriptionId $subscriptionId

Might be causing the issue. Remember anything you output is fed down the pipe. So, the likely problem is Out-Griview doesn't know what to do with a multi-typed array. Try sending that like to Out-Null to see if it rights the Out-GridView result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related