Skip to content

Instantly share code, notes, and snippets.

@figueroadavid
Created March 6, 2024 19:56
Show Gist options
  • Select an option

  • Save figueroadavid/1173a508969d5081b0a015062a0361f0 to your computer and use it in GitHub Desktop.

Select an option

Save figueroadavid/1173a508969d5081b0a015062a0361f0 to your computer and use it in GitHub Desktop.
Converts an existing DHCP address configuration to a local static IP configuration
function ConvertFrom-DHCPToStatic {
<#
.SYNOPSIS
This script takes an existing DHCP configuration and converts it to a static IP configuration
.DESCRIPTION
The script uses several functions to retrieve the necessary data from the existing configuration,
removes that configuration, and installs the new configuration.
.NOTES
This script was built with the idea that there will only be one "physical" NIC in the server that is up and
running when the script is run. It has not been tested in other scenarios
.EXAMPLE
PS C:\> ConvertFrom-DHCPToStatic -Verbose
VERBOSE: Running from the console; proceeding with the script
VERBOSE: The Adapter Config is:
@{Name=Ethernet 2; ifIndex=3; MacAddress=3E-DD-2D-E2-AF-63}
VERBOSE: The SubnetMask is 24, and the IPAddress Family is IPv4
VERBOSE: The IPConfiguration is
@{IPaddress=10.207.221.92; PrefixLength=24}
VERBOSE: Removing the existing configuration
VERBOSE: Setting the Adapter's ip configuration
IPAddress : 10.207.221.92
InterfaceIndex : 3
InterfaceAlias : Ethernet 2
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Tentative
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore
IPAddress : 10.207.221.92
InterfaceIndex : 3
InterfaceAlias : Ethernet 2
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Invalid
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : PersistentStore
VERBOSE: Setting the Adapter's DNS configuration
#>
[cmdletbinding()]
param()
$VolatileEnviorment = Get-Item -Path 'Registry::HKEY_CURRENT_USER\Volatile Environment'
$SessionKey = $VolatileEnviorment.GetSubKeyNames()
$MySessionName = (Get-ItemProperty -Path ('Registry::HKEY_CURRENT_USER\Volatile Environment\{0}' -f $SessionKey) -Name SESSIONNAME).SESSIONNAME
if ($MySessionName -ne 'Console') {
Write-Warning 'Not on the console session, not proceeding; this script should not be run through an RDP session'
return
}
else {
Write-Verbose -Message 'Running from the console; proceeding with the script'
}
$Adapter = Get-NetAdapter -Physical | Where-Object Status -eq 'Up'
$Message = 'The Adapter Config is:{0}{1}' -f [System.Environment]::NewLine, ($($Adapter | Select-Object -Property Name,ifIndex, MacAddress) -join [System.Environment]::NewLine)
Write-Verbose -Message $Message
$NetIP = Get-NetIPAddress -InterfaceIndex $Adapter.InterfaceIndex
$Message = 'The SubnetMask is {0}, and the IPAddress Family is {1}' -f $NetIP.PrefixLength, $NetIP.AddressFamily
Write-Verbose -Message $Message
$IPConfig = Get-NetIPConfiguration -InterfaceIndex $Adapter.InterfaceIndex
$Message = 'The IPConfiguration is{0}{1}' -f [System.Environment]::NewLine, $($NetIP | Select-Object -Property IPaddress, PrefixLength)
Write-Verbose -Message $Message
$AdapterConfig = @{
InterfaceIndex = $Adapter.InterfaceIndex
IPAddress = $IPConfig.IPV4Address.IPAddress
DefaultGateway = $IPConfig.IPV4DefaultGateway.NextHop
PrefixLength = $NetIP.PrefixLength
AddressFamily = $NetIP.AddressFamily
}
Write-Verbose -Message 'Removing the existing configuration'
Remove-NetIPAddress -InterfaceIndex $Adapter.InterfaceIndex -Confirm:$false
Write-Verbose -Message "Setting the Adapter's ip configuration"
New-NetIPAddress @AdapterConfig
$DNSConfig = @{
InterfaceIndex = $Adapter.InterfaceIndex
ServerAddress = $IPConfig.DNSServer.GetEnumerator().ServerAddresses
}
Write-Verbose -Message "Setting the Adapter's DNS configuration"
Set-DnsClientServerAddress @DNSConfig
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment