question

MicroTechie-0276 avatar image
0 Votes"
MicroTechie-0276 asked RichMatheisen-8856 edited

Error Handling with Powershell script

Dear All,

I need your little assistance with the below code, it is working fine. However, I want that each successful change and error should be recorded in a log file. IF the modification is successful then it should record a log success message & should record an error in case of a failure.

 $List = Import-csv c:\new\input.csv
 ForEach ($User in $List)
 {
 Get-ADUser -Identity $User.samaccountname | Set-ADAccountExpiration -TimeSpan (New-TimeSpan -Days 100)
 }



windows-server-powershellwindows-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MicroTechie-0276 avatar image
0 Votes"
MicroTechie-0276 answered RichMatheisen-8856 edited

@RichMatheisen-8856 :
Thank You very much for helping me with this Code. Just to gain a better understanding, you have used try & catch block.

1# Try will run for every identity every time & in case there is an error encountered then only catch will be executed once?
2# Let's say if I input 100 samaccountnames & then there is either an error with 1 account (at line 50) between will the script terminate at 50 or will continue to complete the operation for the rest of the samaccountnames after 51?

3# Also "-ErrorAction Stop" has been used twice so that in the first case if the samaccountname is inexistant then it writes the error. Second time it is used again so that incase there is an error while updating the attribute then it records that error too.


4# Can it also catch the errors realtime instead of me defining the errors in the blocks?

5# Where does it writes the log file output?Where can I define and how.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

  1. The Catch block will be executed for any error occurring in the Try block.

  2. The Catch block will be executed and then the script will continue -- unless you decide to "throw" an exception or execute an "exit". In other words, only the 50th user will not have their account expiration date set.

  3. That's correct. An error in either cmdlet will cause the Catch block to be executed. There's no sense in running the Set-ADAccountExpiration cmdlet if there's no user!

  4. If you don't specify the exception on the Catch then the Catch will be run for any exceptions. Think of it as a default condition.

  5. You get to specify the path of the log file. You can use "Out-File" (with 'append', of course), or Add-Content, or "echo 'this is an error' >filepath", or any other means of adding text to a file.

Based only on the questions you've asked I'm going to assume you know little about PowerShell scripting. If that's so, please download this (free) PDF and work through at least the 1st half of the book. Do the exercises. You'll learn a lot. Move on the the 2nd half of the book when you're ready.

Windows-PowerShell-4


1 Vote 1 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

Something like this:

 $List = Import-csv c:\new\input.csv
  ForEach ($User in $List) {
     Try{
         Get-ADUser -Identity $User.samaccountname -ErrorAction Stop | 
             Set-ADAccountExpiration -TimeSpan (New-TimeSpan -Days 100) -ErrorAction Stop
         # Write 'Success' to log file here
     }
     Catch{
         # Write 'Failure' to log file here
     }
 }

You'll have to define what you want written to the log file, and the log file itself.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.