询问者
自动删除禁用账户脚本运行失败,请问是什么原因

问题
-
Get-WinEvent -FilterHashtable @{LogName = 'Security'; Id = 4725} | ForEach-Object {
$xml = [xml] $_.ToXml()
$_ | Select-Object @{Name = 'Name'; Expression = {$xml.Event.EventData.Data[0].'#text'}}, TimeCreated
} | Group-Object Name | Select-Object Name, @{Name = 'TimeCreated'; Expression = {$_.Group.TimeCreated | Sort-Object | Select-Object -Last 1}} |
Where-Object {$_.TimeCreated -le (Get-Date).AddDays(-1)} | ForEach-Object {
try {
if (!(Get-ADUser -Identity $_.Name).Enabled) {
Remove-ADUser -Identity $_.Name -Confirm:$false -WhatIf
}
}
catch {
}
}报错:
Get-WinEvent : 无法检索到有关 Security 日志的信息。错误: 尝试执行未经授权的操作。。
所在位置 D:\jiaoben\根据安全日志自动删除禁用账户.ps1:1 字符: 1
+ Get-WinEvent -FilterHashtable @{LogName = 'Security'; Id = 4725} | ForEach-Objec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-WinEvent], Exception
+ FullyQualifiedErrorId : LogInfoUnavailable,Microsoft.PowerShell.Commands.GetWinEventCommand
Get-WinEvent : 在 localhost 计算机上没有与“Security”匹配的事件日志。
所在位置 D:\jiaoben\根据安全日志自动删除禁用账户.ps1:1 字符: 1
+ Get-WinEvent -FilterHashtable @{LogName = 'Security'; Id = 4725} | ForEach-Objec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Security:String) [Get-WinEvent], Exception
+ FullyQualifiedErrorId : NoMatchingLogsFound,Microsoft.PowerShell.Commands.GetWinEventCommand运行:Get-WinEvent -FilterHashtable @{LogName = 'Security'; Id = 4725} 此条命令是可以显示出日志信息的。
- 已编辑 longlong12 2019年8月22日 2:25
全部回复
-
你好,
谢谢你的提问。
请尝试使用域管理员身份打开powershell运行该脚本。
或者你也可以使用以下脚本来删除禁用账号:
Search-ADAccount -AccountDisabled | where {$_.ObjectClass -eq 'user'} | Remove-ADUser
最好的祝福,
Lee
Just do it.
- 已建议为答案 LeeSeenLiMicrosoft contingent staff 2019年9月6日 12:00