Better DEBUG option ?

This topic contains 4 replies, has 4 voices, and was last updated by  tw h 2 years, 5 months ago.

  • Author
    Posts
  • #25145

    tw h
    Participant

    I wrote a program to update AlternateEmail of some users in Office 365. I created a csv import file, and run this through the following lines:

    Import-Csv -path "users.csv.txt" -Delimiter ";" | foreach {
        $m = $_.AlternateEmailAddresses
        $e = $_.EmailAddress
        $w = "set-MSOlUser -UserPrincipalName $e -AlternateEmail $m"   # there is no -whatif, so to test before running, do a write-host $w
        if [$m -ne ""] {
            set-MSOlUser -UserPrincipalName $e -AlternateEmail $m
        }
    }

    It did run almost right with the input of 500 users. There was just one error:
    [blockquote]set-MSOlUser : Invalid value for parameter. Parameter Name: OtherMail.
    At line:7 char:9
    + set-MSOlUser -UserPrincipalName $e -AlternateEmail $m
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: [:] [Set-MsolUser], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.PropertyValidationException,Microsoft.Online.Administration.Automa
    tion.SetUser
    [/blockquote]
    Just one line in de input line is wrong, only which one ? It would be very helpful if the full line is outputted with expanded variables. I could use -verbose, but that will result in a lot of logging. I just want the complete line where the error occurred, not all those lines where it goes well.
    How can I accomplish this easily ? Is there some extra debug option I don't know of ?

  • #25146

    Lauras Juozulynas
    Participant

    Hi,

    After your line "set-MSOlUser -UserPrincipalName $e -AlternateEmail $m" you can test if it was completed successfully checking variable $? for being true or false.
    $? returns true or false for every last command executed.

    set-MSOlUser -UserPrincipalName $e -AlternateEmail $m
    If ($? -eq $false) {Write-Host "$_"}
    

    Hope this helps.

  • #25147

    Tim Pringle
    Participant

    Yes, would also suggest doing a Write-Output with the variable names before the set-MSO1User command to see what $e and $m are to allow you to find out the specific one that is causing the problem.

  • #25148

    Matt Bloomfield
    Participant

    You should look to introduce some error handling using try catch.

    
    try {
        set-MSOlUser -UserPrincipalName $e -AlternateEmail $m
    }
    catch {
        Write-Warning "Error processing user $e".   
    }
    

    If the command in the try block generates a terminating error, the script will jump to the catch block. In the catch block you can handle the error however you see fit. In the example, it just writes a message to the console, $e will be expanded because the double quotes are used. Instead of writing to the console you could write to a log file.

    Not every error is a terminating error so if you find the catch block is never entered you may need to use -ErrorAction Stop with the command to force a terminating error.

  • #25151

    tw h
    Participant

    The try-catch with -erroraction did the trick. One email address had a space at the end of the field.
    Would it not be nice if powershell wrote the complete command to the error/warning output, with expanded variables ?

    Thanks a lot for the tips.

You must be logged in to reply to this topic.