我正在尝试找出 Powershell 脚本的正确编码。我希望它查看我们所有的域控制器并使用最新的 lastlongondate。获取数据后,我希望它通过操作系统对它们进行分类,这样它就会显示 Windows 10 - 100 Server 2008 - 10 ex... 我似乎无法弄清楚如何让它检查所有控制器以获取适当的数据。这是我目前所拥有的。有人看到我做错了什么吗?
#Functions
function ImportADModule
{
Import-Module ActiveDirectory
if (!($?))
{
Add-WindowsFeature RSAT-AD-PowerShell
Import-Module ActiveDirectory
}
}
function GetDN
{
param($domain)
$names = $domain.Split(".")
$bFirst = $true
foreach ($name in $names)
{
if ($bFirst)
{
$dn += "DC=" + $name
$bFirst = $false
}
else { $dn += ",DC=" + $name }
}
return $dn
}
function GetDNs
{
param($domains)
$dns = @{}
foreach ($domain in $domains)
{
$dns.Add($domain, (GetDN -domain $domain))
}
return $dns
}
function GetOSCountsPerDomain
{
param($dns, $enabled, $daysOld)
$osCounts = @{}
$cutOffDate = ((Get-Date).Adddays(-($daysOld))).ToFileTime()
Write-Host "Getting Data" -NoNewline -ForegroundColor Yellow
$filter = "(PwdLastSet -gt {0}) -and (Enabled -eq '{1}')" -f $cutOffDate, $enabled
foreach ($domain in $dns.GetEnumerator())
{
$i = 0
$domains = @{}
Write-Host "." -NoNewline -ForegroundColor Yellow
$computers = Get-ADComputer -Filter $filter -SearchBase $domain.Value -Server $domain.Key -Properties OperatingSystem
foreach ($computer in $computers)
{
if ($computer.OperatingSystem -eq $null) { $os = 'NULL'}
else { $os = $computer.OperatingSystem }
try { $domains.Add(($os + " - " + $osver), 1) }
catch { $domains.Set_Item(($os + " - " + $osver), ($domains.Get_Item($os + " - " + $osver))+1) }
}
$osCounts.Add($domain.Key, $domains)
}
Write-Host
return $osCounts
}
function DisplayOutput
{
param($osCounts)
Write-Host
foreach ($osCount in $osCounts.GetEnumerator())
{
Write-Host $OSCount.Key -ForegroundColor Green
$osCount.Value.GetEnumerator() | Sort-Object Value -Descending | Format-Table -AutoSize
}
}
#Main
#Import AD Module for PowerShell
ImportADModule
#Get list of domains from current forest
$Domains = (Get-ADForest).domains
#Get hash table of domains and distinguished names from current forest
$DNs = GetDNs -domains $Domains
#Get OS counts per domain (specify age here)
$OSCounts = GetOSCountsPerDomain -dns $DNs -enabled $true -daysOld 7
#Display Results
$Body = DisplayOutput -osCounts $OSCounts
#Send email settings
$email = @{
From = "email"
To = "email"
Subject = "Operating System Counts"
SMTPServer = "mail.mail.org"
Body = $body| Out-String
}
send-mailmessage @email
答案1
下面应该可以满足您的要求。我还没有添加“发送电子邮件”部分。只是回答了您关于域控制器及其数量的具体问题。
$domaincontrollers = Get-ADDomainController
$dcinfo = foreach($computer in $domaincontrollers)
{
Get-Adcomputer -Identity $computer.Name -Properties LastLogonDate,OperatingSystem
}
$dcinfo | Group-Object OperatingSystem
更新 - 重新阅读您的问题,我不确定您是否想要获取具有最新上次登录日期的域控制器中的所有计算机。如果是这种情况,要获取具有最新上次登录日期的域控制器,然后获取该服务器中的所有计算机,您可以使用以下内容。感谢这个关联。
$domaincontrollers = Get-ADDomainController
$lastlogondomaincontroller = $domaincontrollers | Foreach-Object {$_.LastLogonDate = $_.LastLogonDate; $_} | `
Group-Object Name | `
Foreach-Object {$_.Name | Sort-Object LastLogonDate | Select-Object -Last 1}
Get-Adcomputer -Filter * -Server $lastlogondomaincontroller -Properties LastLogonDate,OperatingSystem | Group-Object OperatingSystem
更新 2-评论之后。
$domaincontrollers = (Get-ADForest).Domains | ForEach-Object { Get- ADDomainController -Filter * -Server $_ }
$allcomputersfromalldcs = foreach($computer in $domaincontrollers)
{
Get-Adcomputer -filter * -server $computer.Name -Properties LastLogonDate,OperatingSystem
}
$uniquecomputers = $allcomputersfromalldcs | Select-Object Name,OperatingSystem,LastLogonDate | Sort-Object Name,LastLogonDate | Select-Object Name,OperatingSystem,LastLogonDate -Unique
$uniquecomputers | Group-Object OperatingSystem
更新 2 答案获取所有 DC,然后检查每个 DC 上的所有计算机。由于会有重复的机器,$uniquecomputers 获取所有计算机,并按名称和上次登录日期对它们进行排序。然后我使用 -Unique 仅检索单个机器名称。
然后,您可以使用 Group-Object 获取值。如果您想要电子邮件的实际数字,则可以使用
$uniquecomputers | Group-Object OperatingSystem | Select-Object Count,OperatingSystem
我还将内容存储在变量中,以便您可以在需要时将它们用于您的电子邮件。
希望这就是您所寻找的。
谢谢,蒂姆。