I need to write error mesages to value

Welcome Forums General PowerShell Q&A I need to write error mesages to value

This topic contains 5 replies, has 5 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #81559

    Participant
    Points: 1
    Rank: Member

    Hi,

    I have a script which uses a scriptblock to retrieve certificate information from various servers in our environment.
    Some of these servers are unreachable or the information is unobtainable for various reasons and an error is generated as expected and the script will then move on to the next server in the list.

    All information returned successfully is recorded and a .CSV file is created.

    I would like to also store the error message in the .CSV file to investigate the problematic machines and determine the issue of these.

    Can anyone help me to find out how to also send these error messages to my .CSV file?

    Many thanks in advance.

  • #81565

    Participant
    Points: 225
    Helping Hand
    Rank: Participant

    Catch the error message and append to the CSV, using the Add-Content cmdlet.
    'technet.microsoft.com/en-us/library/ee156791.aspx'

  • #81574

    Participant
    Points: 1
    Rank: Member

    Many thanks for your reply postanote.
    I guess reading back through my initial post I can see why you replied with the suggestion you did.

    My script basically writes the returned values from a scriptblock against a list of servers to a variable which is exported to CSV once the list is complete.

    However, I also want the error object information also entered into my list of returned objects if possible?
    It was actually the process of how I do this that I would like to know as I have no experience or great understanding of error handling.

    Many thanks.

    • #81857

      Participant
      Points: 1
      Rank: Member

      Try/Catch blocks are the way to go. Ian has a good example below.

  • #81614

    Participant
    Points: 2
    Rank: Member

    You might look at adding the -ErrorVariable parameter to the cmdlet that might generate the error. If the variable you defined to capture errors has a value, write that into your csv instead of a row of valid data. Does that help?

  • #81746

    Participant
    Points: 21
    Rank: Member

    If I am understanding your problem correctly, then the way I would go about it is to assemble psobjects for each server (including the properties you want) and then output the collection to a csv at the end. That way, you can output an object for all servers, and include the error when it throws an error. The following example pings each server.

    $Computers = Get-Content -Path C:\path\to\computers.txt
    $Output = @()
    Foreach ($Computer in $Computers) {
        try {
            Test-Connection -ComputerName $Computer -Count 1  -ErrorAction Stop | Out-Null
            $Properties = @{
                Computer = $Computer
                Results  = "Success"
            }
            $obj = New-Object -TypeName PSObject -Property $Properties
            $Output = $Output + $obj
        }
        catch {
            $Properties = @{
                Computer = $Computer
                Results  = $_.Exception
            }
            $obj = New-Object -TypeName PSObject -Property $Properties
            $Output = $Output + $obj
        }
    }
    $Output | Export-CSV -Path C:\path\to\output.csv
    

    Hope this helps.

The topic ‘I need to write error mesages to value’ is closed to new replies.