0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Another good option for this task would be start-transcript and stop-transcript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.