Log Success or Failure of powershell commands RRS feed

  • Question

  • Hi Scripting guys,

    I have a small powershell script to add aliases to a Mailbox :-

    foreach($i in get-content .\AddAliases.csv)
    write-host $k" Adding "$i
    set-mailbox directory -emailaddresses @{add = $i}
    add-content -path 'c:\powershellscripts\aliases_added.txt' -value $k" "$i 

    I'd like to add a 'success' or 'failure' comment to the end of each line of the output file, depending on whether the command just run worked or not. Does each command return some sort of exit code that I could test?


    • Edited by Michael986 Wednesday, October 23, 2019 2:27 AM
    Wednesday, October 23, 2019 1:33 AM

All replies

  • Just add it to the end of your output statement.

    Please edit your post and post the code with the tool provided on the edit bar and format the code corer4ctly.


    Wednesday, October 23, 2019 1:39 AM
  • Add what exactly to the end of my output statement? 
    Wednesday, October 23, 2019 2:24 AM
  • First fix your original post then read the help for add-content which will give you its use and examples.


    Wednesday, October 23, 2019 2:26 AM
  • Sorry - original post fixed.

    I've re-read the add-content help, but can't see anything that helps me add a 'success' or 'failure' comment depending on the result of the previous command. Am I missing something?

    Wednesday, October 23, 2019 2:34 AM
  • If you actually read the BP and Style guide you would then format your code so it was readable then you would see the mistakes. The code ppostedcould never run.

    Here is what it looks correctly indented and formatted.

    foreach($i in get-content .\AddAliases.csv){
        write-host "$k Adding $i"
        set-mailbox directory -emailaddresses @{add = $i}
        add-content -path 'c:\powershellscripts\aliases_added.txt' -value "$k $i"
    Notice that the code is missing some pieces.  I fixed the string messages.


    Wednesday, October 23, 2019 2:36 AM
  • To add email addresses and catch errors use "-ErrorAction Stop and place the code in a Try/Catch block. Output your log lines as needed for each condition.


    Wednesday, October 23, 2019 2:42 AM
  • I'll accept that my formatting and coding may not be great, but I can assure you it ran OK exactly as posted in my original posts. But, yes, your version is neater.

    But I'm still missing the important piece - ie how to add 'success' or 'failure' to the 'add-content' line depending on whether the previous command resulted in success or not

    Wednesday, October 23, 2019 2:42 AM
  • The code as posted is missing a closure and the strings may work but are written incorrectly for most string uses.

    Getting away with bad code is not a good way to learn PS correctly.

    Because of the bad formatting and bad coding practices it took me much longer to decode what you were trying to ask.

    Here is the best guess I can make which also code this in a way more normal to PowerShell.

    Adding a CSV extension to a file does not make it a CSV file.  You may have issues with files that are CSV which are loaded with "Import-Csv" .  "Get-Content" is for loading text files.

    Get-content .\AddAliases.csv |
    	ForEach-Object {$count = 0} {
    		write-host "$count Adding $i"
    			set-mailbox directory -emailaddresses @{add = $_} -ErrorAction Stop
    			$status = 'succeeded'
    			$status = 'failed'
    		add-content 'c:\powershellscripts\aliases_added.txt' -value "$count $_ $status"


    Wednesday, October 23, 2019 2:57 AM
  • Ah, ok - I get it, thanks

    I'll have a play around with that - the ErrorAction command is obviously what I was looking for!

    Thanks again

    Wednesday, October 23, 2019 3:15 AM