Powershell - 写入Windows事件查看器

时间:2015-08-20 17:15:31

标签: powershell

我有一个小的备份脚本,用于在我的Windows Server 2012计算机上安排备份,但我想在 Windows事件查看器上写一个小注释,以便我得到某种形式的通知一次发生备份。

我知道我应该使用 Write-EventLog 参数,但我一直在弄乱 Windows事件查看器似乎需要的标志。我非常感谢一个包含必要标志的示例,以及它们应该包含的值,以便我可以看到一个简单的事件。

我不确定这是否重要,但这是我的Powershell脚本。

$backup = New-WBPolicy
Set-WBPolicy $backup

$date = Get-Date
$date_check = $date.AddDays(-7)
$day = $date.DayOfWeek
$month = $date.Month
$year = $date.Year
$check_year = $date_check.Year
$check_month = $date_check.Month
$check_day = $date_check.DayOfWeek

$date_target = "\\DC01\Backup\$year\$month\$day"
$move_date_target= "\\DC01\Backup\$year\$month"
$check_date_target = "\\DC01\Backup\$check_year\$check_month\$check_day"

$target = New-WBBackupTarget -NetworkPath $date_target
$file_spec = New-WBFileSpec -FileSpec "C:\Windows\SYSVOL"
Add-WBBackupTarget -Policy $backup -Target $target
Add-WBFileSpec -Policy $backup -FileSpec $file_spec
$check_folder = Get-Item $check_date_target


if(-not (Test-Path "\\DC01\Backup\$year"))
{
    New-Item -ItemType directory -Path "\\DC01\Backup\$year"
}

if(-not (Test-Path "\\DC01\Backup\$year\$month"))
{
    New-Item -ItemType directory -Path "\\DC01\Backup\$year\$month"
}

if($date_target -match "[S|s]unday")
{
    if(Test-Path "$date_target\WindowsImageBackup")
    {
        Move-Item "$date_target\WindowsImageBackup" "$move_date_target\WindowsImageBackup-$year-$month-$day"
    }
}


if(-not (Test-Path "\\DC01\Backup\$year\$month\$day"))
{
    New-Item -ItemType directory -Path "\\DC01\Backup\$year\$month\$day"
}

Start-WBBackup $backup

1 个答案:

答案 0 :(得分:2)

您可以获得每个powershell命令的在线帮助。所以在你的情况下

get-help write-eventlog -online

你应该找到这样一个例子:

write-eventlog -computername Server01 -logname Application 
    -source MyApp -eventID 3001 
    -message "MyApp added a user-requested feature to the display."

您可能需要EventSource,如果缺少,您可以创建一个(只需要一次):

$source ="MyApp"
if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) {
    [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
}