我有3个字符串数组(计算机名称):
$unreachable
$unresponsive
$noremote我正在寻找一种适度优雅的方式来创建一个表,它的列标题为"Unreachable“、"Unresponsive”和"NoRemote“,每一列都是来自各自数组的项的列表。
我可能想得太多了,但我会感谢蜂巢思维的帮助。
发布于 2019-08-02 01:15:33
因此,这就是我最终得到的解决方案(在一个很好的小DC监控脚本中)。为此,感谢@TheMadTechnician。
$DCs = Get-ADDomainController -Filter * | sort-object Name
$unreachable=@()
$unresponsive=@()
$noremoteAD=@()
foreach ($DC in $DCs)
{
$Name = $DC.Name
$dateline=(Get-Date).tostring("yyyy-MM-dd HH:mm:ss - ") + $Name
#Write-Host $dateline
if (!(Test-Connection $Name -quiet))
{
$unreachable+=$Name
continue
}
try
{
$user = get-aduser "Brian.Hampson" -server $Name -properties Name -ErrorAction Stop
#write-host "Good - $Name"
}
catch [Microsoft.ActiveDirectory.Management.ADServerDownException]
{
$noremoteAD+=$Name
}
catch [System.TimeoutException]
{
$unresponsive+=$Name
}
}
$DCCollection = @()
For($i=0;$i -lt ($unreachable,$unresponsive,$noremotead|% Count|sort|select -last 1);$i++)
{
$MyDC = "" | select Unresponsive,Unreachable,NoRemoteAD
if ($i -lt $unreachable.Length) {$MyDC.Unreachable=$unreachable[$i]}
if ($i -lt $Unresponsive.Length) {$MyDC.Unresponsive=$unresponsive[$i]}
if ($i -lt $NoRemoteAD.Length) {$MyDC.NoRemoteAD = $noremoteAD[$i]}
$DCCollection+=$MyDC
}
$head = @"
<style>
@import url('https://fonts.googleapis.com/css?family=Ubuntu')
</style>
<style>
BODY{background-color:peachpuff; font-family: 'Ubuntu', sans-serif;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:thistle}
TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:PaleGoldenrod}
strong{color: green;}
</style>
"@
If ($DCCollection.count -gt 5)
{
$date = (get-date).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss Z")
$body= "<P><H2><B>These are the DC's that have some sort of issue as of $date </B></H2></P>"
$body+=$DCCollection|ConvertTo-Html -Head $head
$from="NoReply-"+$Env:COMPUTERNAME+"@ALSGlobal.com"
$to = @("Some.User@Example.com")
$bcc = @("SomeOtherUser@Example.com")
Send-MailMessage -Bcc $bcc -To $to -From $from -Subject "DCs with issues" -BodyAsHtml $body -SmtpServer "SMTPSERVER.Example.com"
}每晚都会邮寄一个包含服务器名列的漂亮HTML表(以防其他人发现它很有用)
https://stackoverflow.com/questions/57299590
复制相似问题