PowerShell日志记录功能用法

时间:2017-12-06 12:38:15

标签: function powershell logging

我正在尝试记录PowerShell脚本的输出,该脚本是一个简单的Cmdlet串行运行列表,这里有一个示例

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

我找到了这个日志功能,但我不知道如何使用它或调用它以便我可以从每个Cmdlet中获得所有输出

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

我想使用此日志记录来调试我的脚本,因为脚本的输出非常模糊

1 个答案:

答案 0 :(得分:1)

使用内置的cmdlet(例如Out-File)是不可行的,而不是使用定制的日志记录功能使事情变得复杂?

Out-File C:\filename.txt