The Hairy Lemon
The Hairy Lemon

Reputation: 97

PowerShell Logging Function Usage

I am trying to log the output from a PowerShell script, the script is a simple list of Cmdlets run in series, here's an example of one

New-AzureRmVirtualNetworkGateway -Name VNet1GW -ResourceGroupName My-Resource-Group -Location 'West US' -IpConfigurations $gwipconfig -GatewayType Vpn -VpnType RouteBased -GatewaySku standard

I found this logging function, but I am not sure how to use it or call it so that I can get all the output from each Cmdlet in series

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
    }
}

I would like to use this logging for debugging my scripts as the output from the scripts is very vague

Upvotes: 0

Views: 383

Answers (1)

Adam Thomason
Adam Thomason

Reputation: 1166

Rather than complicate things with a bespoke logging function, is it not feasible to use a built-in cmdlet such as Out-File?

Out-File C:\filename.txt

Upvotes: 1

Related Questions