Link to home
Start Free TrialLog in
Avatar of SAM2009
SAM2009Flag for Canada

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
Avatar of Rajkumar Duraisamy
Rajkumar Duraisamy
Flag of India image

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)"
}

Open in new window

$Users | foreach ($User in $Users) {

try {
Set-ADUser ******* your actions
Write-host $_.Users  $_.Manager
}
catch{
   
       Write-host "Users: $User"
    }
}
SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM2009

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.
Avatar of SAM2009

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"
   
    }
}


--------------------------
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of SAM2009

ASKER

Thanks guys!