Загрузка данных


Import-Module ActiveDirectory

$prefix = 'pl-ws-'
$digits = 6

# Все компьютеры в домене с именами pl-ws-NNNNNN (любой регистр, любая вложенность)
$computers = Get-ADComputer -Filter "Name -like '$prefix*'" -Properties Name |
    Where-Object { $_.Name -match "^$prefix(\d{$digits})$" }

# Занятые номера
$used = [System.Collections.Generic.HashSet[int]]::new()
foreach ($c in $computers) {
    [void]$used.Add([int]$Matches[1])
}

if ($used.Count -eq 0) {
    Write-Host "Компьютеров с именами $prefix****** не найдено." -ForegroundColor Yellow
    return
}

$max = ($used | Measure-Object -Maximum).Maximum

# Свободные номера от 1 до максимального
$free = foreach ($i in 1..$max) {
    if (-not $used.Contains($i)) { '{0}{1:D6}' -f $prefix, $i }
}

Write-Host "Занято имён:        $($used.Count)"
Write-Host "Максимальный номер: $max ($('{0}{1:D6}' -f $prefix, $max))"
Write-Host "Свободных в промежутке: $(@($free).Count)" -ForegroundColor Green
Write-Host ""

$free