Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.7 kB
2
Indexable
# 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 }

        # Check if $nic.IPAddress is null or empty
        if ($nic.IPAddress) {
            # 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
        } else {
            Write-Host "Error: NIC has no IP address."
        }
    } 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 }

        # Check if $nic.IPAddress is null or empty
        if ($nic.IPAddress) {
            # 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
        } else {
            Write-Host "Error: NIC has no IP address."
        }
    } 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 }

# Check if $nic.IPAddress is null or empty
if ($nic.IPAddress) {
    $originalIPAddress = $nic.IPAddress[0].IPAddress
    $originalSubnetMask = $nic.IPAddress[0].SubnetMask
    $originalGateway = $nic.IPAddress[0].Gateway
    $originalDNSServer = $nic.IPAddress[0].DNSServer
} else {
    Write-Host "Error: NIC has no IP address."
}

# Check if the IP address is obtained via DHCP
$ipConfig = Get-NetIPAddress | Where-Object { $_.InterfaceIndex -eq $nic.InterfaceIndex }
if ($ipConfig.IPAddressType -eq "Dhcp") {
    Write-Host "Error: NIC is using DHCP. Cannot change IP address."
} else {
    # Change the NIC settings
    ChangeNICSettings $interfaceName $newIPAddress $newSubnetMask $newGateway $newDNSServer

    # Start a timer to revert the settings after 30 seconds
    $timer = New-Object System.Timers.Timer
    $timer.Interval = 30000
    $timer.Add_Elapsed({
        RevertNICSettings $interfaceName $originalIPAddress $originalSubnetMask $originalGateway $originalDNSServer
        $timer.Stop()
    })
    $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