Powershell日志文件

时间:2017-01-17 04:22:24

标签: powershell logging

首先,我想指出我是PowerShell ScrapBooker,对PowerShell并不是非常了解。

我一直在编写一个安装BGInfo的脚本......我的实际安装和卸载工作非常完美,现在我正在努力让日志整理得很好。

我找到了这篇文章" Create Log File in Powershell"这很精彩,并将此功能合并到我的剧本中。

Function Write-Log {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[ValidateSet("LABEL","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")
IF ($Level -eq "LABEL") {
    $Line = "$Message"
    }
ELSE {
    $Line = "$Stamp $Level $Message"
    }
If($logfile) {
    Add-Content $logfile -Value $Line
}
Else {
    Write-Output $Line
}
}

我需要知道的是如何使用它来记录命令的输出。

例如:

在我的脚本中,我有这个命令:

New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo -Value """$InstPath\Bginfo.exe"" $InstPath\$BGTemplateFile $InstOptions" -PropertyType 'String' -Force

或者这个:

Copy $SourcePath\Bginfo.exe $InstPath

我想知道的是如何使用我的函数从该命令捕获任何输出并将其记录在我的日志文件中。

我想我也想使用这些信息并将其应用于我想要记录的任何其他命令。

希望这一切都清楚明白,有人可以帮助我。

干杯,

戴夫。

:)

1 个答案:

答案 0 :(得分:1)

我相信你指的是这个功能:

function Write-Log 
{ 
    [CmdletBinding()] 
    Param 
    ( 
        [Parameter(Mandatory=$true, 
                   ValueFromPipelineByPropertyName=$true)] 
        [ValidateNotNullOrEmpty()] 
        [Alias("LogContent")] 
        [string]$Message, 

        [Parameter(Mandatory=$false)] 
        [Alias('LogPath')] 
        [string]$Path='C:\Logs\PowerShellLog.log', 

        [Parameter(Mandatory=$false)] 
        [ValidateSet("Error","Warn","Info")] 
        [string]$Level="Info", 

        [Parameter(Mandatory=$false)] 
        [switch]$NoClobber 
    ) 

    Begin 
    { 
        # Set VerbosePreference to Continue so that verbose messages are displayed. 
        $VerbosePreference = 'Continue' 
    } 
    Process 
    { 

        # If the file already exists and NoClobber was specified, do not write to the log. 
        if ((Test-Path $Path) -AND $NoClobber) { 
            Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name." 
            Return 
            } 

        # If attempting to write to a log file in a folder/path that doesn't exist create the file including the path. 
        elseif (!(Test-Path $Path)) { 
            Write-Verbose "Creating $Path." 
            $NewLogFile = New-Item $Path -Force -ItemType File 
            } 

        else { 
            # Nothing to see here yet. 
            } 

        # Format Date for our Log File 
        $FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss" 

        # Write message to error, warning, or verbose pipeline and specify $LevelText 
        switch ($Level) { 
            'Error' { 
                Write-Error $Message 
                $LevelText = 'ERROR:' 
                } 
            'Warn' { 
                Write-Warning $Message 
                $LevelText = 'WARNING:' 
                } 
            'Info' { 
                Write-Verbose $Message 
                $LevelText = 'INFO:' 
                } 
            } 

        # Write log entry to $Path 
        "$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append 
    } 
    End 
    { 
    } 
}

<强>用法:

 Write-Log -Message 'My log Error Message' -Path c:\Logs\Script.log -Level Error

 Write-Log -Message 'My log Warning message' -Level Warn

注意:默认情况下,它存储在C:\ logs \ PowershellLog.log中。您也可以在使用过程中随时更改它,您可以在功能本身进行硬编码。

希望它有所帮助。