none
try catch block will not capture errors RRS feed

  • Question

  • Hi guys,

    Im unsure why this is not capturing the errors and writing an error to the screen.

    It is also not appending them to an error file.

    I am trying to check whether a user in the file is licensed and find what licences they have assigned.

    can someone please assist?

    This script is being executed using Powershell v2.0

    import file has a header of :  userprincipalname

    code:

    $users = Import-Csv c:\Temp\Check_ems_licence_user.csv
    
    foreach ($user in $users)  {
    
    try {
    
    Get-MsolUser -userprincipalname $user.userprincipalname | select displayname, userprincipalname, islicensed, licenses | ft -auto
    }
    
    Catch  [Microsoft.Online.Administration.Automation.MicrosoftOnlineException]
    {
    Write-Host "$User does NOT exist!"
    }
    Catch {
    $_ | Out-File C:\Temp\Ems_errors.txt -Append
    }
    }


    Thanks for your assistance.


    • Edited by AngeloAnelloIT Monday, April 29, 2019 12:57 AM changed code block
    Monday, April 29, 2019 12:16 AM

Answers

  • This would be the acceptable way to do this with correct output and formatting.

    Import-Csv c:\Temp\Check_ems_licence_user.csv |
        ForEach-Object {
            
            try {
                Get-MsolUser -userprincipalname $_.userprincipalname -ErrorAction Stop
            }
            Catch [Microsoft.Online.Administration.Automation.MicrosoftOnlineException]{
                Write-Host "$($_.userprincipalname) does NOT exist!"
            } 
            Catch {
                $_ | Out-File C:\Temp\Ems_errors.txt -Append
            }
        
        } |
        Select-Object displayname, userprincipalname, islicensed, licenses | 
        Format-Table -AutoSize

    It is highly like that most code written for MSOL will not work correctly in PS v2.


    \_(ツ)_/

    Monday, April 29, 2019 12:36 AM

All replies

  • In these technical forums you must post correctly formatted code using the code posting tool provided.  Please edit your original post and correct this.  THe code you posted is mostly unreadable in most browsers.

    You cannot "catch" an exception without using the "Stop" erroraction keyword.

    Start by carefully reading the help fot try/catch.

    help about_try_catch

    PowerShell 2 is obsolete and should not be used. YOU can upgrade to WMF 5.1 on any current system.


    \_(ツ)_/

    Monday, April 29, 2019 12:31 AM
  • This would be the acceptable way to do this with correct output and formatting.

    Import-Csv c:\Temp\Check_ems_licence_user.csv |
        ForEach-Object {
            
            try {
                Get-MsolUser -userprincipalname $_.userprincipalname -ErrorAction Stop
            }
            Catch [Microsoft.Online.Administration.Automation.MicrosoftOnlineException]{
                Write-Host "$($_.userprincipalname) does NOT exist!"
            } 
            Catch {
                $_ | Out-File C:\Temp\Ems_errors.txt -Append
            }
        
        } |
        Select-Object displayname, userprincipalname, islicensed, licenses | 
        Format-Table -AutoSize

    It is highly like that most code written for MSOL will not work correctly in PS v2.


    \_(ツ)_/

    Monday, April 29, 2019 12:36 AM
  • Hi JRV,

    Thanks for the update.

    I have correctly formatted the code block and entered it above as suggested.

    I will upgrade the powershell verison and try again as well as reading the help section.

    If there are further issues i will update them here.

    Cheers,

    Monday, April 29, 2019 12:59 AM