Hi again,
I didn’t invest enough time into error handling yet but face an issue I need to fix at the moment.
I got a function which checks against our Active Directory wether a user account, which will be provided via a .CSV file, is valid at all.
Here’s the code:
The .CSV file come like this:
DC = which data center
User = the user account
type = what type of virtual desktop (premium or standard)
DC,User, type
DC B,DomainA\User1,STD
DC A,DomainA\User2,STD
DC A,DomainB\User3,PRM
#################################################
FUNCTION: check if user account is a valid AD user account
#################################################
function verifyuseraccount
{
$struser = $user | select -ExpandProperty user
$usersplit = $struser -split {$_ -eq ""}
write-host “”
write-host “Checking if user account $struser is valid”
if($user -like "*DomainA*")
{
Get-ADUser $usersplit[1] -server domainA.abc.de.com
}
elseif($user -like "*DomainB*")
{
Get-ADUser $usersplit[1] -server domainB.abc.de.com
}
}
The problem is that sometimes the user accounts are misspelled or not existing. I currently do a manual check on the accounts from the .CSV afterwards. How can I tell the function to check for an error and if an error is thrown… sort of goto next user in the list. I remember in school we had some sort of C++ goto command but I don’t get it to work in PS.
Here’s the error when a user account cannot be validated:
Get-ADUser : Cannot find an object with identity: ‘User1’ under: ‘DC=DomainA,DC=ABC,DC=DE,DC=com’.
At C:\Users\myuserpath\Desktop\user-assignment_v2.ps1:36 char:5
regards
Christian
Running the users through a foreach loop with error handling should allow you to accomplish what you need. The following code should at least get you started.
#Requires -Modules ActiveDirectory
function Verify-UserAccount {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string]$CSVPath = 'C:\tmp\users.csv'
)
$UserInfo = Import-Csv -Path $CSVPath
foreach ($User in $UserInfo) {
Write-Verbose -Message "Checking if user account $($User.User) is valid."
try {
Get-ADUser -Server $User.DC -Identity ($User.User -replace '^.*\\') -ErrorAction Stop
}
catch {
Write-Warning -Message "No such user: $($User.User)"
}
}
}
Hi Mike,
thanks for the quick reply. This really got me further.
Although my “verify user function” does now cycle through each user, when I embed it into my script the script still continues even if the output of the “verify user function” is negative meaning that this user is invalid.
Is there a catch for a function within a script as well to catch that negativ output and goto next user in line?
This is a part of the script I’m talking about:
foreach ($user in $users)
{
#DC A (DCN) path
if($user.DC -eq “DC A”)
{
verifyuseraccount
!----> How can I catch the error from the function verifyuseraccount and on error goto next user in list? <-----!
checkforvdi
if($user.type -eq "STD")
{
#get all not assigned machines from XenDesktop
$Xdcomputer = $dcnadcomputer | foreach {get-brokermachine -hostedmachinename $_ -IsAssigned $false -AdminAddress $adminaddress -MaxRecordCount 9999 | where {$_.CatalogName -notlike "*sDD*" -and $_.CatalogName -like "*STANDARD*"}}
#Check if any VDIs are available to assign
if($Xdcomputer.count -gt 0)
{
$randomset = get-random -InputObject $Xdcomputer -Count 1
}
else
{
write-warning "No more $user.type VDIs are availabe in $user.DC, continueing with next user."
break
}
}
Elseif($user.type -eq "PRM")
{
#get all not assigned machines from XenDesktop
$Xdcomputer = $dcnadcomputer | foreach {get-brokermachine -hostedmachinename $_ -IsAssigned $false -AdminAddress $adminaddress -MaxRecordCount 9999 | where {$_.CatalogName -notlike "*sDD*" -and $_.CatalogName -like "*PREMIUM*"}}
#Check if any VDIs are available to assign
if($Xdcomputer.count -gt 0)
{
$randomset = get-random -InputObject $Xdcomputer -Count 1
}
else
{
write-warning "No more $user.type VDIs are availabe in $user.DC, continueing with next user."
break
}
}
Else
{
write-warning "No VDI found"
}
regards
Christian
Mike,
I think I found what I was looking for…
I added continue
try
{
Get-ADUser $usersplit[1] -server domainA.abc.de.com
}
catch
{
Write-Warning -Message “No such user: $($User.User)”
continue
}