如何将文件内容添加到事件日志中

时间:2012-11-14 23:00:58

标签: powershell-v2.0

我有PS脚本将Regkey计数到文本文件。我在Task Scheduler& amp;如果我们能够获取该文件的内容并在任务运行后将其记录到事件查看器中,您能告诉我吗?脚本在下面提到 -

if (!([diagnostics.process]::GetCurrentProcess().Path -match '\\syswow64\\'))
{
$uninstallPath = "\Software\Microsoft\Windows\CurrentVersion\Uninstall\"
$uninstallWow6432Path = "\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
@(
if (Test-Path "HKLM:$uninstallWow6432Path" ) {Get-ChildItem "HKLM:$uninstallWow6432Path"-Recurse | Measure-Object | Out-File "C:\PS Output\Total6432.txt"}
if (Test-Path "HKLM:$uninstallpath" ) {Get-ChildItem "HKLM:$UninstallPath" -Recurse | Measure-Object | Out-File "C:\PS Output\Total32.txt"}
)
}

我尝试了下面提到的以下代码,但我看不到在事件查看器中写的任何内容。能否请你给我一些指导?

if (!([diagnostics.process]::GetCurrentProcess().Path -match '\\syswow64\\'))
{
  $uninstallPath = "\Software\Microsoft\Windows\CurrentVersion\Uninstall\"
  $uninstallWow6432Path = "\Software\Wow6432Node\Microsoft\Windows\CurrentVersion  \Uninstall\"
  @(
  if (Test-Path "HKLM:$uninstallWow6432Path" ) {Get-ChildItem "HKLM:$uninstallWow6432Path"-Recurse | Measure-Object | Out-File "C:\PS Output\Total6432.txt"}
  if (Test-Path "HKLM:$uninstallpath" ) {Get-ChildItem "HKLM:$UninstallPath" -Recurse | Measure-Object | Out-File "C:\PS Output\Total32.txt"}
)

$Content = Get-content -Path 'C:\PS Output\Total6432.txt'
$Content1 = Get-Content -Path 'C:\PS Output\Total32.txt'
{
Write-EventLog -LogName Application -Source $Content AND $Content1 -EventId 3001 -EntryType Information "Count is Registered"
}
}

1 个答案:

答案 0 :(得分:0)

为什么不直接读取文件的内容并使用write-eventlog之类的命令写入脚本末尾的特定事件日志?你遇到什么问题?

看看http://blogs.msdn.com/b/davethompson/archive/2011/10/25/running-a-scheduled-task-after-another.aspx有关一个接一个地运行一个任务的信息,然后你可以创建这样一个第二个任务,它只是读取上面的文件并将它们写入事件日志。

如果这不能解答您的问题,您究竟遇到了什么问题?