none
Error and Warning-Noitfications in Powershell RRS feed

  • Question

  • At the Moment I am working on my first Program language and that is Windows Powershell. So I got a Task that I didn´t understant very well. It sounds so: Error and Warning notifications can Interpret rightly. The Topic of this Task is "Debugging". I´m not sure if I must work with parameters, so I hope you guys can help me. I just need an explanation.
    Sunday, January 17, 2016 5:49 AM

Answers

  • They probably want you to log Error and Warning in case something happens.

    In case they want you to log it in the event viewer, read the article here .

    """

    Write-EventLog –LogName Application –Source “My Script” –EntryType Information –EventID 1
     –Message “This is a test message.”

    """

    In case you need to write something to a file you can read different articles online by googling:" Powershell how to log to a file" or read this link.

    """

    Function Write-Log {
        [CmdletBinding()]
        Param(
        [Parameter(Mandatory=$False)]
        [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
        [String]
        $Level = "INFO",
    
        [Parameter(Mandatory=$True)]
        [string]
        $Message,
    
        [Parameter(Mandatory=$False)]
        [string]
        $logfile
        )
    
        $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
        $Line = "$Stamp $Level $Message"
        If($logfile) {
            Add-Content $logfile -Value $Line
        }
        Else {
            Write-Output $Line
        }
    }

    """


    If you find an answer has helped you please use the answer button and/or use that thumbs up! Julio Sanchez-Tirado http://scriptomato.com co-founder | CEO | Pitcher

    Wednesday, July 12, 2017 10:32 AM