Загрузка данных
$Server = "RUKLGDC01.pcmarus.loc"
$Stamp = Get-Date -Format "yyyyMMdd_HHmmss"
$RemoteScriptLocal = "C:\Windows\Temp\DHCP_Leases_$Stamp.ps1"
$RemoteOutLocal = "C:\Windows\Temp\DHCP_Leases_$Stamp.txt"
$RemoteDoneLocal = "C:\Windows\Temp\DHCP_Leases_$Stamp.done"
$RemoteScriptUNC = "\\$Server\C$\Windows\Temp\DHCP_Leases_$Stamp.ps1"
$RemoteOutUNC = "\\$Server\C$\Windows\Temp\DHCP_Leases_$Stamp.txt"
$RemoteDoneUNC = "\\$Server\C$\Windows\Temp\DHCP_Leases_$Stamp.done"
$ScriptBody = @'
$ErrorActionPreference = "Continue"
Import-Module DhcpServer -ErrorAction Stop
$ScopeIds = @(
"10.182.150.0",
"10.182.151.0",
"10.182.152.0",
"10.182.153.0",
"10.182.154.0",
"10.182.155.0"
)
& {
Write-Output "DHCP lease diagnostics"
Write-Output "Server: $env:COMPUTERNAME"
Write-Output "Date: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
foreach ($ScopeId in $ScopeIds) {
Write-Output ""
Write-Output "============================================================"
Write-Output "SCOPE: $ScopeId"
Write-Output "============================================================"
Write-Output ""
Write-Output "=== SCOPE CONFIGURATION ==="
Get-DhcpServerv4Scope -ScopeId $ScopeId |
Format-List ScopeId,Name,State,StartRange,EndRange,SubnetMask,LeaseDuration
Write-Output ""
Write-Output "=== STATISTICS ==="
Get-DhcpServerv4ScopeStatistics -ScopeId $ScopeId |
Format-List ScopeId,PercentageInUse,InUse,Free,Reserved,Pending
Write-Output ""
Write-Output "=== LEASE STATES ==="
$Leases = Get-DhcpServerv4Lease -ScopeId $ScopeId -AllLeases
if ($Leases) {
$Leases |
Group-Object AddressState |
Select-Object Name,Count |
Format-Table -AutoSize
}
else {
Write-Output "Leases were not found."
}
Write-Output ""
Write-Output "=== RESERVATIONS ==="
$Reservations = Get-DhcpServerv4Reservation `
-ScopeId $ScopeId `
-ErrorAction SilentlyContinue
if ($Reservations) {
$Reservations |
Select-Object IPAddress,ClientId,Name,Description |
Format-Table -AutoSize
}
else {
Write-Output "Reservations were not found."
}
Write-Output ""
Write-Output "=== EXPIRED AND DECLINED LEASES ==="
$ProblemLeases = $Leases |
Where-Object {
$_.AddressState -match "Expired|Declined"
}
if ($ProblemLeases) {
$ProblemLeases |
Select-Object IPAddress,HostName,ClientId,AddressState,LeaseExpiryTime |
Sort-Object IPAddress |
Format-Table -AutoSize
}
else {
Write-Output "Expired or declined leases were not found."
}
Write-Output ""
Write-Output "=== ALL LEASES ==="
$Leases |
Select-Object IPAddress,HostName,ClientId,AddressState,LeaseExpiryTime |
Sort-Object IPAddress |
Format-Table -AutoSize
}
} 2>&1 |
Out-String -Width 400 |
Set-Content -LiteralPath "__OUT__" -Encoding UTF8
New-Item -ItemType File -Path "__DONE__" -Force | Out-Null
'@
$ScriptBody = $ScriptBody.Replace("__OUT__", $RemoteOutLocal)
$ScriptBody = $ScriptBody.Replace("__DONE__", $RemoteDoneLocal)
try {
Write-Host "Копирование скрипта на DHCP-сервер..." -ForegroundColor Cyan
Set-Content `
-LiteralPath $RemoteScriptUNC `
-Value $ScriptBody `
-Encoding UTF8 `
-Force
$CommandLine = "powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File `"$RemoteScriptLocal`""
Write-Host "Запуск диагностики на $Server..." -ForegroundColor Cyan
$ProcessClass = [wmiclass]"\\$Server\root\cimv2:Win32_Process"
$Result = $ProcessClass.Create($CommandLine)
if ($Result.ReturnValue -ne 0) {
throw "Ошибка запуска процесса. WMI ReturnValue: $($Result.ReturnValue)"
}
Write-Host "Процесс запущен. PID: $($Result.ProcessId)" -ForegroundColor Green
$Deadline = (Get-Date).AddMinutes(3)
while (
-not (Test-Path $RemoteDoneUNC) -and
(Get-Date) -lt $Deadline
) {
Start-Sleep -Seconds 2
}
if (-not (Test-Path $RemoteDoneUNC)) {
throw "Диагностика не завершилась за 3 минуты."
}
Write-Host "`n=== РЕЗУЛЬТАТ ===`n" -ForegroundColor Green
Get-Content $RemoteOutUNC
}
catch {
Write-Host "Ошибка: $($_.Exception.Message)" -ForegroundColor Red
}
finally {
Remove-Item $RemoteScriptUNC -Force -ErrorAction SilentlyContinue
Remove-Item $RemoteOutUNC -Force -ErrorAction SilentlyContinue
Remove-Item $RemoteDoneUNC -Force -ErrorAction SilentlyContinue
}