I have written a couple of try & Catch blocks when trying to access properties from Azure however the catch doesnt seem to handle the errors.
I was wondering is there anything that needs doing for the Az & MS.graph modules to catch errors? Two examples below work when the correct params are passed however if the wrong params are passed you will get an error which doesnt run the catch code.
$session = Get-AzWvdUserSession -HostPoolName $hostpoolname -ResourceGroupName $AvdRGName | Select-Object * | Where-Object { $_.ActiveDirectoryUserName -eq "domain\$username" } -ErrorAction Stop
if ($session.id -ne $null) {
Write-Host "User is logged into AVD session, please log off user & re-run pipeline"
Write-Host ***********************************
Write-Host "Session detail:" $session.Id
Write-Host ***********************************
exit
} } catch {
$message = $_.Exception.message
Write-Host "Error checking user session in AVD Hostpool" $message }
try {
Write-Host ***********************************
Write-Host "Connecting to MS Graph API......"
Connect-MgGraph -Scopes 'User.Read.All', 'Directory.Read.All'
$userAttributes = Get-MgUser -UserId $username@iamdavidson.co.uk | Select-Object *
$fileShareName = $userAttributes.OnPremisesSecurityIdentifier + "_" + $userAttributes.OnPremisesSamAccountName
Write-Host ***********************************
Write-Host "The folder name for the users FSLogix profile is:" $fileShareName
Write-Host *********************************** } catch {
Write-Host "Error checking getting user properties using MS Graph" }