Write-Host "$Eventlog_source is already registered with another Eventlog." Exit } #新建事件日志MyApp,指定事件日志来源为$Eventlog_log,"."代表本机;将变量$ Event_test中的内容写入到MyApp事件日志下 $Applog = New-Object system.diagnostics.eventlog($Eventlog_log,".") #Remove-Eventlog $log #...
Write-Host "$Eventlog_source is already registered with another Eventlog." Exit } #新建事件日志MyApp,指定事件日志来源为$Eventlog_log,"."代表本机;将变量$ Event_test中的内容写入到MyApp事件日志下 $Applog = New-Object system.diagnostics.eventlog($Eventlog_log,".") #Remove-Eventlog $log #...
创建一个名为“WriteToAppLog.ps1”的脚本来实现这个功能,代码如下,后面将根据代码给出相关的说明: if(![system.diagnostics.eventlog]::sourceExists("ps_script")) { $applog = [system.diagnostics.eventlog]::CreateEventSource("ps_script","Application") } $applog = New-Object system.diagnostics.event...
Out-File从管道接受的是data,所以你可以一次性将多个string写到一个文件. 综上,个人推荐平时优先使用out-file,下面是我平时用的一个写log的函数,供大家参考下。 参考链接,大家自己多动手,感受不同命令带来的不同结果: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?
[void]Critical($s){$msg = $this.getDataTime() + " [CRITICAL] " + $sif ($this.log){$this.writeLog('Magenta',$msg)}}} 3. 使用示例 3.1 基本使用 【例】简单测试各个日志方法的使用,代码如下: $SavePath = 'C:\Users\Administrator\Desktop\mylog.log';$logger = [Logger]::new($SavePath...
Write-EventLog [-LogName] <String> [-Source] <String> [[-EntryType] <EventLogEntryType>] [-Category <Int16>] [-EventId] <Int32> [-Message] <String> [-RawData <Byte[]>] [-ComputerName <String>] [<CommonParameters>] Description Cmdlet 會將 Write-EventLog 事件寫入事件記錄檔。 若...
$ ll install_pip file_test > log ls: cannot access 'file_test': No such file or directory 1. 2. 3. 4. 5. 6. 7. 8. 9. 可见,被正确处理的部分可以被>重定向到指定文件中 但是未被正确处理的错误内容依然会被输出到终端 ...
CommandType Name Version Source --- --- --- --- Cmdlet Add-Computer 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Clear-EventLog 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Connect-PSSession 3.0.0.0 Microsoft.PowerShell.Core Cmdlet Enter-PSSession 3.0.0.0 Microsoft.PowerShell.Core Cmdlet Get...
Show-EventLog Start-Transaction Test-ComputerSecureChannel Undo-Transaction Use-Transaction Write-EventLog Microsoft.PowerShell.Utility Convert-String ConvertFrom-String PSDesiredStateConfiguration Disable-DscDebug Enable-DscDebug Get-DscConfiguration Get-DscConfigurationStatus ...
catch { Write-Log $PSItem.ToString() throw } 我们想要重新引发异常,以保留原始执行信息,如源脚本和行号。 如果此时引发新异常,它将隐藏异常开始的位置。重新引发新异常如果捕获到一个异常,但想要引发另一个异常,则应将原始异常嵌套在新异常内。 这样一来,堆栈下游的人就可以将其作为 $PSItem.Exception.Inner...