Can anyone tell me why this else statement is not working?...

Hi,

I have the following code to retrieve tags from various vcenter servers, I am banging my head against the wall as I just cannot see where the structure of my code is going wrong where the else statement is not recognised,

$FinalList = @()
$lc=1
$vCenters = "vcenter1,vcenter2,vcenter3"
$VCs = $vCenters.split(",")
$User = $env:username+"@"+$env:USERDNSDOMAIN
$Cred = get-credential -UserName $User -Message 'Enter Password for vCenter Servers'
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
foreach ($VC in $VCs) {
                        Write-Host 'Connecting to Vcenter Server '$VC -ForegroundColor Green
                        Connect-VIServer -server $VC -Credential $Cred
                        $VMs = get-vm
                            foreach ($VM in $VMs) {
                                                    $Tags = $VM | Get-TagAssignment
                                                    $Cname = $VM | Select-Object -Property @{Name=’Cluster’;Expression={$_.VMHost.Parent}}
                                                    Write-Host $lc' of'($VMs.count)'Getting detail for Virtual Machine'$VM
                                                    if ($Tags -ne $Null){
                                                                        foreach ($Tag in $Tags.tag) {
                                                                        $VMIdent = [PScustomObject] @{
                                                                        "vCenterServer" = $VC
                                                                        "ClusterName" = $Cname.cluster.name
                                                                        "ESXiHost" = $VM.VMHost
                                                                        "VMName" = $VM.name
                                                                        "PowerState" = $VM.PowerState
                                                                        "OperatingSystem" = $VM.ExtensionData.Guest.GuestFullName
                                                                        "Tag Name" = $Tag.Name
                                                                        "Tag Description" = $Tag.Description}
                                                                            } else {
                                                                        $VMIdent = [PScustomObject] @{
                                                                        "vCenter Server" = $VC
                                                                        "ClusterName" = $Cname.cluster.name
                                                                        "ESXi Host" = $VM.VMHost
                                                                        "VMName" = $VM.name
                                                                        "PowerState" = $VM.PowerState
                                                                        "OperatingSystem" = $VM.ExtensionData.Guest.GuestFullName
                                                                        "Tag Name" = "No Tag"
                                                                        "Tag Description" = "No Tag"}
                                                    }
                                                    $VMIdent | Export-Csv VMTagInfo.csv -NoTypeInformation -append
                                                  }
                        $lc++
                        $FinalList += $VMIdent
                        $VMIdent = $null
                        }
$lc=1
write-host 'Disconnecting from vCenter Server '$VC -ForegroundColor Cyan
Disconnect-VIServer $VC -confirm:$false
}
Write-Host 'Complete ' -ForegroundColor Green

Many thanks all!

I can’t test it but as an educated guess, Get-TagAssignment does not return NULL if there are no tag assignments.

Try piping $Tags to Get-Member to figure out what’s assigned to $Tags when there are no tag assignments returned.

Yeah I must admit, if I pulled the correct objects from Get-TagAssignment then I should ‘hopefully’ not receive any empty values

The reason I used the ‘else’ statement in my code was to evade my export-csv failing due to these

I have become a bit fixated on why the else statement will not work as the structure of the code seems fine to me and it bugs me that I cant get it to work as I am sure that I will need to use ‘else’ again some time in the future, if not in this code then something else

Anyway, many thanks for your reply Matt

The issue is you have the else portion in the wrong spot. It currently reads

If($tags -ne $null){
    Foreach($Tag in $tags.tag){
        ...
    } else {
        ...
    }

Code formatting and indentation can go a long way. Also, you open the same log file to export over and over appending each time as well as create a (what should be) identical list. Instead of doing those it would be better to just capture all output and then export once. I’ve restructured your code to show the recommendations.

$lc=1
$vCenters = "vcenter1,vcenter2,vcenter3"
$VCs = $vCenters.split(",")
$User = $env:username+"@"+$env:USERDNSDOMAIN
$Cred = get-credential -UserName $User -Message 'Enter Password for vCenter Servers'
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

$FinalList = foreach ($VC in $VCs) {
    Write-Host 'Connecting to Vcenter Server '$VC -ForegroundColor Green
    $null = Connect-VIServer -server $VC -Credential $Cred

    foreach ($VM in ($VMs = get-vm)) {
        $Tags = $VM | Get-TagAssignment
        $Cname = $VM | Select-Object -Property @{Name=’Cluster’;Expression={$_.VMHost.Parent}}
        Write-Host $lc' of'($VMs.count)'Getting detail for Virtual Machine'$VM

        if ($Tags -ne $Null){
            foreach ($Tag in $Tags.tag) {
                [PScustomObject] @{
                    "vCenterServer" = $VC
                    "ClusterName" = $Cname.cluster.name
                    "ESXiHost" = $VM.VMHost
                    "VMName" = $VM.name
                    "PowerState" = $VM.PowerState
                    "OperatingSystem" = $VM.ExtensionData.Guest.GuestFullName
                    "Tag Name" = $Tag.Name
                    "Tag Description" = $Tag.Description
                }
            }
        } else {
            [PScustomObject] @{
                "vCenter Server" = $VC
                "ClusterName" = $Cname.cluster.name
                "ESXi Host" = $VM.VMHost
                "VMName" = $VM.name
                "PowerState" = $VM.PowerState
                "OperatingSystem" = $VM.ExtensionData.Guest.GuestFullName
                "Tag Name" = "No Tag"
                "Tag Description" = "No Tag"
            }
        }

        $lc++
    }

    $lc=1
    write-host 'Disconnecting from vCenter Server '$VC -ForegroundColor Cyan
    Disconnect-VIServer $VC -confirm:$false
}

$FinalList | Export-Csv VMTagInfo.csv -NoTypeInformation

Write-Host 'Complete ' -ForegroundColor Green