Untitled
unknown
plain_text
a year ago
2.0 kB
8
Indexable
# 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
$originalGateway = 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
Remove-NetIPAddress -InterfaceAlias $NicName -IPAddress $NewIpAddress -PrefixLength 24 -Confirm:$false
# Add original IP addresses
foreach ($ip in $originalConfig) {
New-NetIPAddress -InterfaceAlias $NicName -IPAddress $ip.IPAddress -PrefixLength $ip.PrefixLength -DefaultGateway $originalGateway.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
Get-NetIPAddress -InterfaceAlias $NicName | Remove-NetIPAddress -Confirm:$false
# Set new IP address
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
Editor is loading...
Leave a Comment