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

 
Member
4 years, 4 months ago.

  • Author
    Posts
  • #20021

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 22
    Rank: Member

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

  • #20026

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 22
    Rank: Member

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

  • #20028

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 22
    Rank: Member

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

  • #20030

    Member
    Points: 0
    Rank: Member

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

The topic ‘Help with exception.’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort