LucD
Leadership
Leadership

I don't think you should use a try-catch construction in your 2nd script.
There is no terminating error in that try-block that would help you achieve what you are trying to do.

Also, the disconnect from the ESXi node at the end of the 2nd script seems out of place.

Try something like this for the 2nd part.

$AdminCredential = Import-Clixml -Path /VCA_AdminCred.xml
Connect-VIServer -Server $vcenter -Credential $AdminCredential | Out-Null
Write-Host "Connected to $vcenter"
$hosts = Get-VMHost

Foreach ($vmhost in $hosts) {
    $status = (Get-VMHost -name $vmhost).ExtensionData.Config.LockdownMode # checks lockdown mode
    Write-Host "$status is set on $vmhost"
    if ($status -eq "lockdownDisabled") {
        (Get-VMHost $vmhost | Get-View).EnterLockdownMode() # sets lockdown mode to enabled.
        Write-Host "Lockdown is now set to enabled on $vmhost"
    }
    else
        Write-Host "Lockdown is already set to enabled on $vmhost"
    }
    
    $HostAccess = Get-View -Id $vmhost.ExtensionData.ConfigManager.HostAccessManager
    $HostAccess.UpdateLockdownExceptions($username)
    Write-Host "$username has been added to the Exception Users List on $vmhost"
}


#Write-Host "Disconnecting from $esxihost"
#Disconnect-VIServer -Server $esxihost -Force -confirm:$false
Write-Host "Disconnecting from $vcenter"
Disconnect-VIServer -Server $vcenter -Force -confirm:$false


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

Reply
0 Kudos