We help IT Professionals succeed at work.

Script in VBS to change the IPv4 configuration from DHCP Enabled to DHCP disabled

acunaara
acunaara asked
on
I need an script in VBS to change the IPv4 configuration from "DHCP Enabled" to "DHCP disabled" but keeping all the actual NIC parameters that it has right now.
It must keep the same IP address, subnet, default gateway, dns, etc.

This is because my IP is changing week by week and i want to get them static now but keeping the same values at the moment of running the script in all the servers.
Comment
Watch Question

Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
It's not in the Topics list, but here's a Powershell version anyway that takes care of IP, Netmask, Gateway, DNS. It prompts for confirmation by default before changing the settings; start it with -Force to skip the confirmation.
Param(
	[switch]$Force
)

Function Get-IPSettings([Parameter(ValueFromPipeline=$true)]$Nic) {
	Process {
		$Nic | Select-Object -Property `
			Description,
			@{Name="IPv4Address"; Expression={$_.IPAddress | ? {$_ -match "^\d+\.\d+.\d+.\d+$"}}},
			@{Name="IPv4Subnet"; Expression={$_.IPSubnet | ? {$_ -match "^\d+\.\d+.\d+.\d+$"}}},
			DefaultIPGateway,
			DNSServerSearchOrder
	}
}

$DhcpNics = Get-WmiObject -Namespace "Root\CIMv2" -Query "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='True' AND DHCPEnabled='True'"
"Found the following NIC(s) with DHCP configuration:" | Write-Host -Foregroundcolor White
$DhcpNics | Get-IPSettings | Format-List | Out-String | Write-Host -Foregroundcolor White

If (-Not $Force) {
	$Confirm = Read-Host "Proceed with converting these settings to static [y/N]"
}
If ($Force -Or ($Confirm -eq "y")) {
	ForEach ($Nic In $DhcpNics) {
		Try {
			"Processing '$($Nic.Description)':" | Write-Host -Foregroundcolor White
			$DhcpSettings = Get-IPSettings -Nic $Nic
			"- Static address $($DhcpSettings.IPv4Address -join ",")/$($DhcpSettings.IPv4Subnet -join ",") ..." | Write-Host -NoNewline -Foregroundcolor White
			$Result = $Nic.EnableStatic($DhcpSettings.IPv4Address, $DhcpSettings.IPv4Subnet)
			If ($Result.ReturnValue -eq 0) {
				" OK." | Write-Host -Foregroundcolor Green
				"- Gateway $($DhcpSettings.DefaultIPGateway -join ",") ..." | Write-Host -NoNewline -Foregroundcolor White
				$Result = $Nic.SetGateways($DhcpSettings.DefaultIPGateway)
				If ($Result.ReturnValue -eq 0) {
					" OK." | Write-Host -Foregroundcolor Green
				} Else {
					" ERROR $($Result.ReturnValue)!" | Write-Host -Foregroundcolor Red
					"See 'https://msdn.microsoft.com/en-us/library/aa393301(v=vs.85).aspx' for error details." | Write-Host -Foregroundcolor Yellow
				}
				"- DNS $($DhcpSettings.DNSServerSearchOrder -join ",") ..." | Write-Host -NoNewline -Foregroundcolor White
				$Result = $Nic.SetDNSServerSearchOrder($DhcpSettings.DNSServerSearchOrder)
				If ($Result.ReturnValue -eq 0) {
					" OK." | Write-Host -Foregroundcolor Green
				} Else {
					" ERROR $($Result.ReturnValue)!" | Write-Host -Foregroundcolor Red
					"See 'https://msdn.microsoft.com/en-us/library/aa393295(v=vs.85).aspx' for error details." | Write-Host -Foregroundcolor Yellow
				}
			} Else {
				" ERROR $($Result.ReturnValue)!" | Write-Host -Foregroundcolor Red
				"See 'https://msdn.microsoft.com/en-us/library/aa390383(v=vs.85).aspx' for error details." | Write-Host -Foregroundcolor Yellow
			}
		} Catch {
			" ERROR: $($_.Exception.Message)!" | Write-Host -Foregroundcolor Red
		}
	}
}

Open in new window

Author

Commented:
Excellent. Really what i need! Thanks