Help with exception.

Welcome Forums General PowerShell Q&A Help with exception.

This topic contains 7 replies, has 3 voices, and was last updated by  Dave Wyatt 3 years, 12 months ago.

  • Author
    Posts
  • #20021

    C Vue
    Participant

    My Achievements

    No Achievements Yet!

    I'm creating a script to update active directory. How would I go about putting a exception on the code if the attribute "department" has no data or is blank in the csv file?

    Import-CSV $importfile | ForEach-Object{
    $samaccountname = $_.sAMAccountName.ToLower()
    $department = $_.department
    $phone = $_.Phone
    $emailAddress = $_.emailAddress
    $manager = $_.manager.ToLower()

    Try {
    $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$samaccountname)"
    }
    Catch
    {
    write-host "Users did not exist."
    }
    If ($exists)
    {

    If ($department)
    {
    Set-ADUser -Identity $samaccountname -department $department
    }

  • #20024

    Dave Wyatt
    Participant

    My Achievements

    No Achievements Yet!

    To produce your own errors in a script, you can either use Write-Error (for nonterminating errors) or throw (for terminating errors); which one you use depends on whether you want the whole pipeline to abort or not. In this case, the answer is probably "not", so you can continue to process other users, so you'd do something like this:

    Import-CSV $importfile | ForEach-Object{
        $samaccountname = $_.sAMAccountName.ToLower()
        $department = $_.department
        $phone = $_.Phone
        $emailAddress = $_.emailAddress
        $manager = $_.manager.ToLower()
    
        if (-not $department)
        {
            Write-Error "User $samaccountname has no department value in the CSV file."
            return
        }
    
        # etc.
    }
    

    Using the return keyword may look a little bit odd here, but that's how you move into the next input object in a ForEach-Object loop. (This is just like the Process block of a function, in that regard.)

  • #20025

    Adam Bertram
    Participant

    My Achievements

    No Achievements Yet!

    Dave, why did you use 'return' there? I'd typically use 'continue'. Is using 'return' better?

  • #20026

    Dave Wyatt
    Participant

    My Achievements

    No Achievements Yet!

    You would use 'continue' if you're in a foreach ($thing in $things) loop, but when you're in a pipeline with ForEach-Object, using 'continue' or 'break' makes funny things happen. (Usually, it aborts your whole script, unless there happens to be a loop or switch statement in play farther up the call stack which can process those keywords.)

  • #20027

    Adam Bertram
    Participant

    My Achievements

    No Achievements Yet!

    Ah, guess I'm used to using the foreach statement rather than ForEach-Object.

  • #20028

    Dave Wyatt
    Participant

    My Achievements

    No Achievements Yet!

    Yep, if you've already got a collection in memory, that's the way to go. The pipeline has its advantages, though; namely streaming objects so you can process very large data sets without running into OutOfMemory exceptions.

  • #20029

    Adam Bertram
    Participant

    My Achievements

    No Achievements Yet!

    That seemingly is the ONLY advantage. In my experience, it's always slower than other methods.

  • #20030

    Dave Wyatt
    Participant

    My Achievements

    No Achievements Yet!

    True. 🙂 Though you can accomplish some pretty impressive speeds using filters, depending on what you're doing, and still keep memory usage down.

You must be logged in to reply to this topic.

Skip to toolbar