Catching error in Text file

Welcome Forums General PowerShell Q&A Catching error in Text file

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

 
Participant
3 years, 11 months ago.

  • Author
    Posts
  • #26545

    Participant
    Topics: 2
    Replies: 2
    Points: 0
    Rank: Member

    Morning,

    I've built my script and it is working correctly for updating and changing the URL as I require. The second part is if an error is thrown out I want the URL, Date/time and the error to be put in to a text file.

    At the moment it is recording the URL and Date/time for everything and if an error it is capturing date/time, URL and the Error.

    Because the script is running a lot of times I just want it to return a result to the notepad file on occasions where there is an error. What is it I'm doing wrong?

    function New-SPsiteURL{
    [cmdletBinding()]
    param(
        #[Parameter]
        [String]$logFile = 'c:\Powershell Exports\Url to title errors.txt'
        )
    
    foreach ($line in (import-csv "C:\Powershell Exports\Newsitelist.csv")) {
            $line.URL
            $line.title    
        
    #try{   
            get-spweb -identity $line.url -ea silentlycontinue -ev myerr | set-spweb -relativeurl $line.title   
    # }
    #Catch{
            
            Get-date | Out-file $logfile -append
            $line.url | Out-File $LogFile -append
            $myerr | Out-File $logfile -append                  
     #               }
            }
    
    }   
    
    
  • #26546

    Participant
    Topics: 1
    Replies: 64
    Points: 0
    Rank: Member

    Try{}Catch{} only catches terminating errors.

    -ea silentlycontinue will make all errors non-terminating and there by not cought, change this to -ErrorAction Stop to make the errors terminating.

  • #26547

    Participant
    Topics: 4
    Replies: 179
    Points: 26
    Rank: Member

    Also take a look at this nice function. I'm using it in my scripts:

    Function – Write-Log

  • #26603

    Participant
    Topics: 2
    Replies: 2
    Points: 0
    Rank: Member

    Perfect, Thanks for the info. Great help.

    Thomas

The topic ‘Catching error in Text file’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort