Automation

 View Only
  • 1.  Can anyone tell me why this 'else' statement is not working?…

    Posted Oct 26, 2020 01:15 PM
      |   view attached

    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!

    Attachment(s)

    zip
    GetTags.ps1.zip   937 B 1 version


  • 2.  RE: Can anyone tell me why this 'else' statement is not working?…
    Best Answer

    Posted Oct 26, 2020 01:27 PM

    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



  • 3.  RE: Can anyone tell me why this 'else' statement is not working?…

    Posted Oct 27, 2020 08:29 AM

    Hi LucD,

    Thank you again, you have saved my sanity

    I owe you a beer for that one!

    Fantastic