Forum Discussion
Start-RSJob PowerShell
# I want to use try catch and is not working
Import-Module PoshRSJob
$hashtable = [hashtable]::Synchronized(@{})
$hashtable.host = $host
$hashtable.mutex = [System.Threading.Mutex]::new()
$hashtable.OutputArray = @()
$hashtable.UserList = @("asif", "jon", "carl", "userNotInAD")
$ErrorActionPreference="Stop"
$AddUserScriptBlock = {
Param($user, $hash)
$hash.host.ui.WriteLine("Getting user info for $user")
try
{
$Res=Get-Aduser $user -Properties Name, EmailAddress | Select Name, EmailAddress
$Name=$res.Name
$EmailAddress=$res.EmailAddress
$NewObject01 = new-object PSObject
$NewObject01 | add-member -membertype NoteProperty -name "user" -Value $user
$NewObject01 | add-member -membertype NoteProperty -name "Name" -Value $Name
$NewObject01 | add-member -membertype NoteProperty -name "EmailAddress" -Value $EmailAddress
$hash.mutex.WaitOne()
$hash.OutputArray += $NewObject01
$hash.mutex.ReleaseMutex()
}
catch
{
$Name=$res.Name
$EmailAddress=$res.EmailAddress
$NewObject01 = new-object PSObject
$NewObject01 | add-member -membertype NoteProperty -name "user" -Value $user
$NewObject01 | add-member -membertype NoteProperty -name "Name" -Value $Name
$NewObject01 | add-member -membertype NoteProperty -name "EmailAddress" -Value $EmailAddress
$hash.mutex.WaitOne()
$hash.OutputArray += $NewObject01
$hash.mutex.ReleaseMutex()
}
}
foreach($user in $hashtable.UserList) {
Start-RSJob $AddUserScriptBlock -ArgumentList $user, $hashtable
}
Get-RSJob | Wait-RSJob -ShowProgress | Receive-RSJob
$hashtable.OutputArray | Export-Csv -NoTypeInformation -Path C:\temp\TestOutput.csv
- So... is the try-catch for checking if the user is in AD or not? You could change "$Res=Get-Aduser $user -Properties Name, EmailAddress | Select Name, EmailAddress" to "$Res=Get-Aduser $user -Properties Name, EmailAddress -Erroraction Stop | Select Name, EmailAddress " to stop processing if the user is not found
1 Reply
- So... is the try-catch for checking if the user is in AD or not? You could change "$Res=Get-Aduser $user -Properties Name, EmailAddress | Select Name, EmailAddress" to "$Res=Get-Aduser $user -Properties Name, EmailAddress -Erroraction Stop | Select Name, EmailAddress " to stop processing if the user is not found