我正在尝试学习编程和Powershell脚本。
我正在处理脚本以收集呈现特定事件ID的服务器列表(在本例中为DC)。
我想添加/更改此代码以获取显示事件ID的服务器列表,而不仅仅是具有服务器描述的txt,就像它现在一样。
使用此代码,我得到一个包含服务器列表和最后一个事件的txt。但我还想在csv中获取活动ID活动的服务器列表。
$DCsList = (Get-ADForest).Domains | %{Get-ADDomainController -Filter * -Server $_} | Select-Object HostName | Sort-Object Hostname
$result = Foreach ($dcs in $DCsList){
try{
Get-WinEvent -computername $dcs -FilterHashtable @{Logname='System';ID=5774} -MaxEvents 1 -ErrorAction SilentlyContinue | fl
}
catch{
}
write-output "$dcs doesn't have the event 5774 on the system log"
}
$result | Out-File "D:\DNSRegEvents.txt"
答案 0 :(得分:0)
我不清楚您希望如何格式化输出,但假设您想要一列为主机名的CSV,而第二列是否存在偶数,则代码将执行此操作:
$DCsList = (Get-ADForest).Domains | %{Get-ADDomainController -Filter * -Server $_} | Select-Object HostName | Sort-Object Hostname
$result = Foreach ($dcs in $DCsList)
{
If ((Get-WinEvent -FilterHashtable @{Logname='System';ID=5774} -MaxEvents 1 -ErrorAction SilentlyContinue).length -gt 0)
{
$Object = New-Object PSObject -Property @{
Server = $dcs.HostName
Result = "Server has event"
}
}
Else
{
$Object = New-Object PSObject -Property @{
Server = $dcs.HostName
Result = "Server does not have event"
}
}
$Object
}
$result | Export-Csv "D:\DNSRegEvents.csv" -NoTypeInformation