Скрипт обращается к Active Directory и выводит информацию о количестве хостов, включая распределение по операционным системам. Умеет вылавливать неактивные записи о хостах по суткам без авторизации. За эталон взят период в 120 суток.
Умеет сохранять файл отчёта в директорию со скриптом.
# Заголовок скрипта #
[System.Console]::Title = "Подсчёт хостов в домене"
# Объявление модуля #
Import-Module activedirectory
# Блок переменных
$allpcs = (Get-ADComputer -Filter {enabled -eq "true"}).count
$windowsserver = (Get-ADComputer -Filter {enabled -eq "true" -and OperatingSystem -Like '*Windows Server*'}).count
$windows10 = (Get-ADComputer -Filter {OperatingSystem -like '*Windows 10*'}).count
$windows7 = (Get-ADComputer -Filter {OperatingSystem -like '*Windows 7*'}).count
$windowsxp = (Get-ADComputer -Filter {OperatingSystem -like '*Windows XP*'}).count
$timestamp120 = (Get-Date).AddDays(-120)
$lastlogonpc = (Get-ADComputer -Properties LastLogonDate -Filter {LastLogonDate -lt $timestamp120}).count
$reportfile = "report.txt"
# Блок условий #
if (!$windowsxp) {$windowsxp = 0}
if (!$windows7) {$windows7 = 0}
if (!$windows10) {$windows10 = 0}
# Формулы подсчёта #
$arm = $windows10 + $windows7 + $windowsxp
# Блок вывода #
Write-Host '--------------------------------------------------------' -ForegroundColor Green
Write-Host
Write-Host 'Активных записей компьютеров в домене:' $allpcs
Write-Host 'Из них'
Write-Host ' на ОС Windows Server:' $windowsserver
Write-Host ' на ОС Windows 10:' $windows10
Write-Host ' на ОС Windows 7:' $windows7
Write-Host ' на ОС Windows XP:' $windowsxp
Write-Host 'Всего АРМ пользователей (без учёта серверов):' $arm
Write-Host
Write-Host '--------------------------------------------------------' -ForegroundColor Green
Write-Host
Write-Host 'Количество ПК без авторизации в течении 120 дней:' $lastlogonpc
Write-Host
Write-Host 'Список ПК с последней датой входа более 120 дней:'
Get-ADComputer -Properties LastLogonDate -Filter {LastLogonDate -lt $timestamp120} | Sort LastLogonDate | FT Name, LastLogonDate
Write-Host 'Список ПК выгружен в файл:' $reportfile
Get-ADComputer -Properties LastLogonDate -Filter {LastLogonDate -lt $timestamp120} | Sort LastLogonDate | FT Name, LastLogonDate | Out-File $reportfile
Write-Host
Write-Host '--------------------------------------------------------' -ForegroundColor Green
# Ожидание действия пользователя #
Write-Host
Read-Host -Prompt "Нажмите Enter для выхода"