Highlighted
Contributor
Contributor

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

Jump to solution

Hi all,

I have the following script to retrieve the tags from various vcenter servers however I cannot see why the 'else' statement in line 27 is throwing an error as not recognised?

I have checked the structure of the code many times now and cannot see any problems

Code attached

Many thanks in advance guys!

0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

Your closing curly brace for the if-block is at the wrong location.

Try like this

$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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
Highlighted
User Moderator
User Moderator

Your closing curly brace for the if-block is at the wrong location.

Try like this

$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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
Highlighted
Contributor
Contributor

Hi LucD,

Thank you again, you have saved my sanity

I owe you a beer for that one!

Fantastic

0 Kudos