创建一个名为“WriteToAppLog.ps1”的脚本来实现这个功能,代码如下,后面将根据代码给出相关的说明: if(![system.diagnostics.eventlog]::sourceExists("ps_script")) { $applog = [system.diagnostics.eventlog]::CreateEventSource("ps_script","Application") } $applog = New-Object system.diagnostics.event...
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-EventLog cmdlet 将事件写入事件日志。 若要将事件写入事件日志,事件日志必须存在于计算机上,并且必须为事件日志注册源。 包含 EventLog 名词(EventLog cmdlet)的 cmdlet 仅适用于经典事件日志。 若要从 Windows Vista 和更高版本的 Windows作系统中使用 Windows
Write-EventLog和New-Object System.Diagnostics.EventLog是两种不同的方法来写入日志。以下是它们之间的主要区别: Write-EventLog是PowerShell中的一个内置命令,用于写入事件日志。它提供了一个简单的方法来写入事件日志,不需要额外的代码。它可以直接在PowerShell脚本中使用。 New-Object System.Diagnostics.EventLog是使...
$Event_test = "PS Test for powershell to write." $Applog.writeEntry($Event_test) 之上输出的事件日志默认级别为“Information”,除此之外还有“Error”、“Warning”、“ SuccessAudit”和“FailureAudit”几种级别。 使用如下代码可以将其设置为警告。
Function Write-Log { [cmdletbinding()]Param([Parameter(Position=0)][ValidateNotNullOrEmpty()][string]$Message,[string]$Path= "$env:SHELL_LOCATION" + "\log\shell.log",[string]$level = "info")$LogFile = $Path $setLevel = 'debug';switch -wildcard ($setLevel){ 'info'{ switch -wild...
Set-Content is designed for string processing. If you pipe non-string objects to Set-Content, it converts the object to a string before writing it. To write objects to files, use Out-File. 文件不存在时创建文件 Set-Content在有时候是不会创建文件的,比如针对一个空的文件夹,如下createfile.txt...
运行Write-AdminAuditLog cmdlet 时,Comment 参数中提供的值包含在日志条目中。为使Write-AdminAuditLog cmdlet 能够写入审核日志,它必须包含在管理员审核日志记录所记录的 cmdlet 列表中。您必须先获得权限,然后才能运行此 cmdlet。 虽然本主题中列出了此 cmdlet 的所有参数,但如果这些参数并未包含在分配给您的权限中...
[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...
默认写入的是Information级别的Log。如果想要制定Level,就要加上EntryType参数: write-eventLog -LogName Game -Message "GayShyFool invoked 'My Dad is a Gang' method." -Source CS -id 2-EntryType Warning 要直接在PowerShell里面读取EventLog,用的是get-eventlog命令:get-eventlog-LogName Game -Source CS...