-
Notifications
You must be signed in to change notification settings - Fork 3
/
ConvertFrom-DHCP.ps1
36 lines (34 loc) · 1.32 KB
/
ConvertFrom-DHCP.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<#
.SYNOPSIS
Converts a Windows DHCP address to static
.DESCRIPTION
Using only PowerShell convert a number to CIDR notation
.EXAMPLE
Get-NetIPConfiguration -InterfaceAlias 'vEthernet (external)' | ConvertFrom-DHCP
.PARAMETER InterfaceAlias
The alias of the interface that is to be set to static.
#>
Function ConvertFrom-DHCP {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
param(
[parameter(ValueFromPipelineByPropertyName)]
[string]$InterfaceAlias
)
$interface = Get-NetIPConfiguration -InterfaceAlias $InterfaceAlias
$dns = (Get-DnsClientServerAddress -InterfaceAlias $InterfaceAlias -AddressFamily IPv4).ServerAddresses
#Output
$new_ip_splat = @{
interfacealias = $interfacealias
ipaddress = $interface.ipv4address.ipv4address
defaultgateway = $interface.ipv4defaultgateway.nexthop
PrefixLength = $((Get-NetIPAddress -InterfaceAlias $interface.InterfaceAlias -AddressFamily IPv4).PrefixLength)
}
if ($PSCmdlet.ShouldProcess(($new_ip_splat.GetEnumerator() | ForEach-Object { "{0}`t{1}" -f $_.Name, ($_.Value -join ", ") }))) {
Remove-NetIPAddress -InterfaceAlias $interfacealias -AddressFamily ipv4
New-NetIPAddress @new_ip_splat
Set-DnsClientServerAddress -InterfaceAlias $interfacealias -ServerAddresses $dns
}
else {
exit
}
}