Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell日志文件_Powershell_Logging - Fatal编程技术网

Powershell日志文件

Powershell日志文件,powershell,logging,Powershell,Logging,首先,我想指出,我是一名PowerShell剪贴簿制作者,对PowerShell的了解并不十分透彻 我一直在编写一个脚本来安装BGInfo。。。我已经完成了实际的安装和卸载工作,现在我正着手处理日志记录 我发现这篇文章“”非常精彩,并将此功能合并到我的脚本中 Function Write-Log { [CmdletBinding()] Param( [Parameter(Mandatory=$False)] [ValidateSet("LABEL","INFO","WARN","ERROR","

首先,我想指出,我是一名PowerShell剪贴簿制作者,对PowerShell的了解并不十分透彻

我一直在编写一个脚本来安装BGInfo。。。我已经完成了实际的安装和卸载工作,现在我正着手处理日志记录

我发现这篇文章“”非常精彩,并将此功能合并到我的脚本中

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
我想知道的是如何使用函数捕获该命令的任何输出并将其记录在日志文件中

我想我也希望使用这些信息,并将其应用到我想要记录某些内容的任何其他命令中

希望这一切都很清楚,有道理,有人可以帮助我

干杯

戴夫


:)

我相信你指的是这个函数:

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中。您还可以在使用过程中动态更改它,也可以在函数本身中对其进行硬编码


希望有帮助。

还有其他选择,但请查看
开始转录本
,看看它是否满足您的需要。谢谢@MatthewWetmore-我会看一看。:)我无法从mobile上给出正确的答案,但是如果您真的想将命令的输出通过管道传输到日志函数中,那么方法是
valuefrompipline
。明天我会举个例子。另见:我很好奇剪贴簿是什么。我建立了一个小工具,我想与初学者分享,也许剪贴簿。这个工具与这个问题无关,所以我不在这里发布。@WalterMitty我在这个例子中所说的scrapbooker是指我从谷歌搜索结果中进行大量复制和粘贴。喜欢剪贴簿的人会做这样的事情。。。这是同一种函数,但你错过了文章中真正的问题。你提到如果我没有弄错@DavidBuckleyYes,如何捕获任何输出并将其放入日志。。但是你的函数和我的函数做的完全一样。。。这让我可以“发送”消息到日志文件。。。它没有做的是捕获某些命令的输出。。。如果它们是错误或不是,请使用my函数将其发送到日志文件。请重新阅读examples.Oo。那个。只要在stdout出现的任何地方使用
>
,并在stdout之后给出文件路径即可。它将把数据附加到文件中。您还可以使用*pipeline和Out File-path“filepath”*,这也会将输出放在文件中。我不确定您所说的*pipeline和Out File-path是什么意思。你能举例说明吗,特别是在我原来的帖子中关于“复制”的命令。