Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
2.9 kB
2
Indexable
Never
# Function to change NIC settings
function ChangeNICSettings($interfaceName, $newIPAddress, $newSubnetMask, $newGateway, $newDNSServer) {
    try {
        # Get the NIC object
        $nic = Get-NetAdapter | Where-Object { $_.InterfaceDescription -eq $interfaceName }

        # Set the new IP address, subnet mask, gateway, and DNS server
        $nic.IPAddress | ForEach-Object {
            $ipConfig = $_.IPAddressConfiguration
            $ipConfig.IPAddress = $newIPAddress
            $ipConfig.SubnetMask = $newSubnetMask
            $ipConfig.Gateway = $newGateway
            $ipConfig.DNSServer = $newDNSServer
            . $ipConfig.Set-IPConfiguration
        }

        # Apply the changes
        . $nic.Set-NetAdapter
    } catch {
        Write-Host "Error changing NIC settings: $_"
    }
}

# Function to revert NIC settings to original values
function RevertNICSettings($interfaceName, $originalIPAddress, $originalSubnetMask, $originalGateway, $originalDNSServer) {
    try {
        # Get the NIC object
        $nic = Get-NetAdapter | Where-Object { $_.InterfaceDescription -eq $interfaceName }

        # Set the original IP address, subnet mask, gateway, and DNS server
        $nic.IPAddress | ForEach-Object {
            $ipConfig = $_.IPAddressConfiguration
            $ipConfig.IPAddress = $originalIPAddress
            $ipConfig.SubnetMask = $originalSubnetMask
            $ipConfig.Gateway = $originalGateway
            $ipConfig.DNSServer = $originalDNSServer
            . $ipConfig.Set-IPConfiguration
        }

        # Apply the changes
        . $nic.Set-NetAdapter
    } catch {
        Write-Host "Error reverting NIC settings: $_"
    }
}

# Get the desired interface name, new IP address, subnet mask, gateway, and DNS server
$interfaceName = "Your NIC Name"
$newIPAddress = "192.168.1.100"
$newSubnetMask = "255.255.255.0"
$newGateway = "192.168.1.1"
$newDNSServer = "8.8.8.8"

# Get the original IP address, subnet mask, gateway, and DNS server
$nic = Get-NetAdapter | Where-Object { $_.InterfaceDescription -eq $interfaceName }
$originalIPAddress = $nic.IPAddress[0].IPAddress
$originalSubnetMask = $nic.IPAddress[0].SubnetMask
$originalGateway = $nic.IPAddress[0].Gateway
$originalDNSServer = $nic.IPAddress[0].DNSServer

# Change the NIC settings
ChangeNICSettings $interfaceName $newIPAddress $newSubnetMask $newGateway $newDNSServer

# Start a timer to revert the settings after 30 seconds
$timer = New-Timer -Interval 30000
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier "NICSettingsTimer" -Action {
    RevertNICSettings $interfaceName $originalIPAddress $originalSubnetMask $originalGateway $originalDNSServer
    Remove-EventRegistration -SourceIdentifier "NICSettingsTimer"
}
$timer.Start()

# Allow the user to stop the timer manually
Write-Host "Timer started. Press Enter to stop."
Read-Host
$timer.Stop()
Leave a Comment