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


PS C:\Windows\system32> $Server = "RUKLGDC01.pcmarus.loc"
$Stamp  = Get-Date -Format "yyyyMMdd_HHmmss"

$RemoteScriptLocal = "C:\Windows\Temp\DHCP_Extend_$Stamp.ps1"
$RemoteOutLocal    = "C:\Windows\Temp\DHCP_Extend_$Stamp.txt"
$RemoteDoneLocal   = "C:\Windows\Temp\DHCP_Extend_$Stamp.done"

$RemoteScriptUNC = "\\$Server\C$\Windows\Temp\DHCP_Extend_$Stamp.ps1"
$RemoteOutUNC    = "\\$Server\C$\Windows\Temp\DHCP_Extend_$Stamp.txt"
$RemoteDoneUNC   = "\\$Server\C$\Windows\Temp\DHCP_Extend_$Stamp.done"

$ScriptBody = @'
$ErrorActionPreference = "Stop"
Import-Module DhcpServer

$Stamp = Get-Date -Format "yyyyMMdd_HHmmss"
$BackupDir = "C:\DHCP_Backup\ExtendRanges_$Stamp"
$BackupFile = Join-Path $BackupDir "DHCP_Config.xml"

$Scopes = @(
    @{ ScopeId = "10.182.150.0"; Start = "10.182.150.1"; End = "10.182.150.249" },
    @{ ScopeId = "10.182.151.0"; Start = "10.182.151.1"; End = "10.182.151.249" },
    @{ ScopeId = "10.182.152.0"; Start = "10.182.152.1"; End = "10.182.152.249" },
    @{ ScopeId = "10.182.153.0"; Start = "10.182.153.1"; End = "10.182.153.249" },
    @{ ScopeId = "10.182.154.0"; Start = "10.182.154.1"; End = "10.182.154.249" },
    @{ ScopeId = "10.182.155.0"; Start = "10.182.155.1"; End = "10.182.155.249" }
)

& {
    Write-Output "============================================================"
    Write-Output " DHCP SCOPE EXTENSION"
    Write-Output "============================================================"
    Write-Output "Server: $env:COMPUTERNAME"
    Write-Output "Date:   $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"

    New-Item -ItemType Directory -Path $BackupDir -Force | Out-Null

    Write-Output "`n=== DHCP BACKUP ==="
    Export-DhcpServer `
        -ComputerName localhost `
        -File $BackupFile `
        -Leases `
        -Force

    Write-Output "Backup created: $BackupFile"

    Write-Output "`n=== BEFORE CHANGE ==="
    foreach ($Item in $Scopes) {
        Get-DhcpServerv4Scope -ScopeId $Item.ScopeId |
            Select-Object ScopeId,Name,State,StartRange,EndRange,SubnetMask,LeaseDuration |
            Format-List

        Get-DhcpServerv4ScopeStatistics -ScopeId $Item.ScopeId |
            Select-Object ScopeId,PercentageInUse,InUse,Free,Reserved,Pending |
            Format-List
    }

    Write-Output "`n=== APPLY CHANGES ==="
    foreach ($Item in $Scopes) {
        Write-Output "Changing $($Item.ScopeId): $($Item.Start) - $($Item.End)"

        Set-DhcpServerv4Scope `
            -ScopeId $Item.ScopeId `
            -StartRange $Item.Start `
            -EndRange $Item.End

        Write-Output "Completed: $($Item.ScopeId)"
    }

    Write-Output "`n=== AFTER CHANGE ==="
    foreach ($Item in $Scopes) {
        Get-DhcpServerv4Scope -ScopeId $Item.ScopeId |
            Select-Object ScopeId,Name,State,StartRange,EndRange,SubnetMask,LeaseDuration |
            Format-List

        Get-DhcpServerv4ScopeStatistics -ScopeId $Item.ScopeId |
            Select-Object ScopeId,PercentageInUse,InUse,Free,Reserved,Pending |
            Format-List
    }

    Write-Output "`n=== DHCP SERVICE ==="
    Get-Service DHCPServer |
        Select-Object Name,Status,StartType |
        Format-List

    Write-Output "`nCHANGE COMPLETED SUCCESSFULLY"

} 2>&1 |
    Out-String -Width 300 |
    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`""

    $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(5)

    while (
        -not (Test-Path $RemoteDoneUNC) -and
        (Get-Date) -lt $Deadline
    ) {
        Start-Sleep -Seconds 2
    }

    if (-not (Test-Path $RemoteDoneUNC)) {
        throw "Операция не завершилась за 5 минут."
    }

    Write-Host "`n=== РЕЗУЛЬТАТ ===`n" -ForegroundColor Green
    Get-Content $RemoteOutUNC
}
catch {
    Write-Host "Ошибка: $($_.Exception.Message)" -ForegroundColor Red

    if (Test-Path $RemoteOutUNC) {
        Write-Host "`n=== ЧАСТИЧНЫЙ ВЫВОД ===`n" -ForegroundColor Yellow
        Get-Content $RemoteOutUNC
    }
}
finally {
    Remove-Item $RemoteScriptUNC -Force -ErrorAction SilentlyContinue
    Remove-Item $RemoteOutUNC    -Force -ErrorAction SilentlyContinue
    Remove-Item $RemoteDoneUNC   -Force -ErrorAction SilentlyContinue
}
Копирование скрипта на DHCP-сервер...
Процесс запущен. PID: 712

=== РЕЗУЛЬТАТ ===

============================================================
 DHCP SCOPE EXTENSION
============================================================
Server: RUKLGDC01
Date:   2026-07-22 10:21:04

=== DHCP BACKUP ===
Backup created: C:\DHCP_Backup\ExtendRanges_20260722_102104\DHCP_Config.xml

=== BEFORE CHANGE ===


ScopeId       : 10.182.150.0
Name          : KA-USERS-IP12-VL150
State         : Active
StartRange    : 10.182.150.1
EndRange      : 10.182.150.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.150.0
PercentageInUse : 84,73895
InUse           : 211
Free            : 38
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.151.0
Name          : KA-USERS-IP12-VL151
State         : Active
StartRange    : 10.182.151.1
EndRange      : 10.182.151.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.151.0
PercentageInUse : 61,84739
InUse           : 154
Free            : 95
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.152.0
Name          : KA-USERS-IP12-VL152
State         : Active
StartRange    : 10.182.152.1
EndRange      : 10.182.152.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.152.0
PercentageInUse : 68,27309
InUse           : 170
Free            : 79
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.153.0
Name          : KA-USERS-IP12-VL153
State         : Active
StartRange    : 10.182.153.1
EndRange      : 10.182.153.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.153.0
PercentageInUse : 65,06024
InUse           : 162
Free            : 87
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.154.0
Name          : KA-USERS-IP12-VL154-LUR
State         : Active
StartRange    : 10.182.154.1
EndRange      : 10.182.154.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.154.0
PercentageInUse : 72,69077
InUse           : 181
Free            : 68
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.155.0
Name          : KA-USERS-IP12-VL155-LUR
State         : Active
StartRange    : 10.182.155.1
EndRange      : 10.182.155.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.155.0
PercentageInUse : 77,51004
InUse           : 193
Free            : 56
Reserved        : 0
Pending         : 0




=== APPLY CHANGES ===
Changing 10.182.150.0: 10.182.150.1 - 10.182.150.249
Completed: 10.182.150.0
Changing 10.182.151.0: 10.182.151.1 - 10.182.151.249
Completed: 10.182.151.0
Changing 10.182.152.0: 10.182.152.1 - 10.182.152.249
Completed: 10.182.152.0
Changing 10.182.153.0: 10.182.153.1 - 10.182.153.249
Completed: 10.182.153.0
Changing 10.182.154.0: 10.182.154.1 - 10.182.154.249
Completed: 10.182.154.0
Changing 10.182.155.0: 10.182.155.1 - 10.182.155.249
Completed: 10.182.155.0

=== AFTER CHANGE ===


ScopeId       : 10.182.150.0
Name          : KA-USERS-IP12-VL150
State         : Active
StartRange    : 10.182.150.1
EndRange      : 10.182.150.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.150.0
PercentageInUse : 84,73895
InUse           : 211
Free            : 38
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.151.0
Name          : KA-USERS-IP12-VL151
State         : Active
StartRange    : 10.182.151.1
EndRange      : 10.182.151.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.151.0
PercentageInUse : 61,84739
InUse           : 154
Free            : 95
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.152.0
Name          : KA-USERS-IP12-VL152
State         : Active
StartRange    : 10.182.152.1
EndRange      : 10.182.152.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.152.0
PercentageInUse : 68,27309
InUse           : 170
Free            : 79
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.153.0
Name          : KA-USERS-IP12-VL153
State         : Active
StartRange    : 10.182.153.1
EndRange      : 10.182.153.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.153.0
PercentageInUse : 65,06024
InUse           : 162
Free            : 87
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.154.0
Name          : KA-USERS-IP12-VL154-LUR
State         : Active
StartRange    : 10.182.154.1
EndRange      : 10.182.154.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.154.0
PercentageInUse : 72,69077
InUse           : 181
Free            : 68
Reserved        : 0
Pending         : 0





ScopeId       : 10.182.155.0
Name          : KA-USERS-IP12-VL155-LUR
State         : Active
StartRange    : 10.182.155.1
EndRange      : 10.182.155.249
SubnetMask    : 255.255.255.0
LeaseDuration : 06:00:00





ScopeId         : 10.182.155.0
PercentageInUse : 77,51004
InUse           : 193
Free            : 56
Reserved        : 0
Pending         : 0




=== DHCP SERVICE ===


Name      : DHCPServer
Status    : Running
StartType : Automatic




CHANGE COMPLETED SUCCESSFULLY


PS C:\Windows\system32>