我创建了一个 PowerShell 脚本,如果检测到新用户帐户添加到我们 AD 中的域管理组,则会通过电子邮件发送。当我运行 PowerShell 窗口时它可以工作,但确实会抛出错误。我正在尝试自动化并每 5 分钟运行一次,直到通过域控制器上的任务计划程序将用户从域管理员组中删除。下面是我的代码,下面是它抛出的错误,但正如之前提到的,它在通过 PowerShell 窗口运行时确实成功运行。
PowerShell 脚本
#Enter the list of approved admin accounts.
#Domain admin accounts
$domainAdminsList = "Administrator", "Admin2", "admin3"
#Get the members of the "Domain Admins" group
$actualAdmins = Get-ADGroupMember -Identity "Domain Admins" | Select-Object -ExpandProperty SamAccountName
$adminsList = $domainAdminsList
$accountType = "Domain"
#Compare the admin accounts vs the adminsList, and if an account exists then add it to $rogueAdmins
$actualAdmins | ForEach-Object {
$adminName = $_
$matchFound = $false
foreach ($account in $adminsList) {
if ($adminName -like $account) {
$matchFound = $true
break
}
}
if (-not $matchFound) {
$rogueAdmins += $adminName
}
else {
$goodAdmins += $adminName
}
}
if ($rogueAdmins.count -gt 0) {
# Import the required module for this script
#Import-Module MSOnline
# Your credentials
$User = "email account"
$Credential = Get-StoredCredential -Target "stored email"
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Credential.Password
# Create a session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
# Import the session
Import-PSSession $Session -DisableNameChecking
# Email details
$EmailTo = "[email protected]"
$EmailFrom = "[email protected]"
$Subject = "ALERT: The Domain Admins group has been modified"
$Body = "A new user has been added or removed from the Domain Admin user's group. Please confirm this is a legitimate change and not malicious by checking the Domain Admin group for the user: $rogueAdmins. This message will repeat every 5 minutes until this has been resolved."
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
# Send the email
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $UserCredential
# Remove the session
Remove-PSSession $Session
exit 1
}
else {
Write-Output "Good news! The $accountType Admins group only contains these approved users:"
$goodAdmins
exit
}
它抛出的错误
New-PSSession : [outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following error message : The WinRM client received an HTTP server error status (500), but the
remote service did not include any other information about the cause of the failure. For more information, see the about_Remote_Troubleshooting Help topic.
At C:\1st_setup\Scripts\admin-check2.ps1:46 char:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Conne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : WinRMHttpError,PSSessionOpenFailed
Import-PSSession : Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At C:\1st_setup\Scripts\admin-check2.ps1:49 char:18
+ Import-PSSession $Session -DisableNameChecking
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Import-PSSession], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportPSSessionCommand
Remove-PSSession : Cannot validate argument on parameter 'Id'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At C:\1st_setup\Scripts\admin-check2.ps1:63 char:18
+ Remove-PSSession $Session
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-PSSession], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.RemovePSSessionComman
任务计划程序历史记录显示它正在运行,但随后它一直挂起并且没有任何反应。就好像被卡住了一样。下面我包含了任务计划程序设置的图像。
我已经尝试调整任务计划程序设置,以在它继续运行时强制停止,但它仍然继续运行。我也使用过不同的帐户,也出现了同样的问题。目前它正在使用我的域管理员帐户来获取任务计划程序权限。如果有人知道如何让它每 5 分钟运行一次,我将不胜感激。谢谢!