Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
2.3 kB
3
Indexable
Never
# Define parameters
param (
    [string]$NicName = "Ethernet", # Replace with the NIC name
    [string]$NewIpAddress = "192.168.1.100",
    [string]$NewSubnetMask = "255.255.255.0",
    [string]$NewGateway = "192.168.1.1",
    [string]$NewDns = "8.8.8.8",
    [int]$RevertDelay = 30
)

# Get the NIC
$nic = Get-NetAdapter -Name $NicName
if (-not $nic) {
    Write-Error "Network adapter '$NicName' not found."
    exit 1
}

# Get current IP configuration
$originalConfig = Get-NetIPAddress -InterfaceAlias $NicName | Select-Object IPAddress, PrefixLength
$originalDns = Get-DnsClientServerAddress -InterfaceAlias $NicName | Select-Object -ExpandProperty ServerAddresses
$originalGateways = Get-NetRoute -InterfaceAlias $NicName | Where-Object DestinationPrefix -eq "0.0.0.0/0" | Select-Object NextHop

# Function to revert settings
function RevertSettings {
    Write-Output "Reverting IP configuration back to original settings..."
    
    # Remove new IP address and gateway
    Remove-NetIPAddress -InterfaceAlias $NicName -IPAddress $NewIpAddress -PrefixLength 24 -Confirm:$false
    Remove-NetRoute -InterfaceAlias $NicName -DestinationPrefix "0.0.0.0/0" -Confirm:$false

    # Add original IP addresses
    foreach ($ip in $originalConfig) {
        New-NetIPAddress -InterfaceAlias $NicName -IPAddress $ip.IPAddress -PrefixLength $ip.PrefixLength -DefaultGateway $originalGateways.NextHop
    }
    
    # Set original DNS servers
    Set-DnsClientServerAddress -InterfaceAlias $NicName -ServerAddresses $originalDns
}

# Function to change settings
function ChangeSettings {
    Write-Output "Changing IP configuration..."
    
    # Remove existing IP addresses and gateways
    Get-NetIPAddress -InterfaceAlias $NicName | Remove-NetIPAddress -Confirm:$false
    Get-NetRoute -InterfaceAlias $NicName | Where-Object DestinationPrefix -eq "0.0.0.0/0" | Remove-NetRoute -Confirm:$false
    
    # Set new IP address and gateway
    New-NetIPAddress -InterfaceAlias $NicName -IPAddress $NewIpAddress -PrefixLength 24 -DefaultGateway $NewGateway
    
    # Set new DNS server
    Set-DnsClientServerAddress -InterfaceAlias $NicName -ServerAddresses $NewDns
}

# Change settings
ChangeSettings

# Wait for the specified time and revert
Start-Sleep -Seconds $RevertDelay
RevertSettings
Leave a Comment