SAM2009
asked on
Powershell try catch
Hi,
I run this PowerShell script:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Import-Module ActiveDirectory
$Users = Import-csv -path "C:\Temps\Users.csv"
$Users | % {
Write-host $_.Users $_.Manager
Try{
Set-ADUser -Identity $_.Users -Manager $_.Manager
}
catch{
Write-host "Users: $($_.Users)"
}
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
The result is like:
john robert
eric
Users:
I know the value of eric manager is empty that why the catch give: "Users:"
But why the catch gives just "Users:" and not "Users: eric"? This should contain: $_.Users
I run this PowerShell script:
--------------------------
Import-Module ActiveDirectory
$Users = Import-csv -path "C:\Temps\Users.csv"
$Users | % {
Write-host $_.Users $_.Manager
Try{
Set-ADUser -Identity $_.Users -Manager $_.Manager
}
catch{
Write-host "Users: $($_.Users)"
}
}
--------------------------
The result is like:
john robert
eric
Users:
I know the value of eric manager is empty that why the catch give: "Users:"
But why the catch gives just "Users:" and not "Users: eric"? This should contain: $_.Users
If manager is null.. you want to catch?
You need to have a terminating error for try/catch. I assume you're getting an error in the first place and wand to handle it?
Try {
Set-ADUser -Identity $_.Users -Manager $_.Manager -ErrorAction Stop
} catch {
Write-host "Users: $($_.Users)"
}
$Users | foreach ($User in $Users) {
try {
Set-ADUser ******* your actions
Write-host $_.Users $_.Manager
}
catch{
Write-host "Users: $User"
}
}
try {
Set-ADUser ******* your actions
Write-host $_.Users $_.Manager
}
catch{
Write-host "Users: $User"
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I thought by using "catch" I can catch users who don't have manager value or wrong value (not manager SamAccountName)
Because when manager value is empty or wrong it should give an error that why I put "Write-host "Users: $($_.Users)"" in CATCH part.
Because when manager value is empty or wrong it should give an error that why I put "Write-host "Users: $($_.Users)"" in CATCH part.
ASKER
Weird, same code but I put $_.users in a variable works. Is it possible when there is an error Powershell loses info in $_.Users?
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Import-Module ActiveDirectory
$Users = Import-csv -path "C:\Temps\Users.csv"
$Users | % {
Write-host $_.Users $_.Manager
$Userinfo = $_.Users
Try{
Set-ADUser -Identity $_.Users -Manager $_.Manager
}
catch{
Write-host "Users: $Userinfo"
}
}
--------------------------
--------------------------
Import-Module ActiveDirectory
$Users = Import-csv -path "C:\Temps\Users.csv"
$Users | % {
Write-host $_.Users $_.Manager
$Userinfo = $_.Users
Try{
Set-ADUser -Identity $_.Users -Manager $_.Manager
}
catch{
Write-host "Users: $Userinfo"
}
}
--------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Regarding using try/catch it is a rule of thumb that you use exceptions only for the unexpected, or if there are too many potential issues to properly check for them. If you have the choice of applying some simple checks versus using try/catch, the checks win.
ASKER
Thanks guys!