New-NetStaticIPAddress.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
<#PSScriptInfo .Version 1.3 .Guid d03a0e2b-361d-4994-af70-ae2afc40d661 .Author Thomas J. Malkewitz @dotps1 .Tags TCPIP IP NIC .ProjectUri https://github.com/dotps1/PSFunctions .ExternalModuleDependencies NetTCPIP .ReleaseNotes Fixed a typo in "ThrottleLimit" parameter. Added missing "ValidLifeTime" parameter. Added Comment Based Help. #> <# .Synopsis Sets a static IP Address on a Network Adapter. .Description Removes the current NetIPAddress and NetRoute on a given NetAdapter. Sets a new Static NetIPAddress and adds DNS Server values if provided. .Inputs Microsoft.Management.Infrastructure.CimSession Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.AddressFamily Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Store Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Type System.Boolean System.Byte System.Int System.String System.TimeSpan .Outputs Microsoft.Management.Infrastructure.CimInstance .Parameter InterfaceIndex System.Int The index of the NetAdapter to set a NetIPAddress on. .Parameter InterfaceAlias System.String The display name of the NetAdapter to set a NetIPAddress on. .Parameter IPAddress System.String The new IP Address value for the NetAdapter. .Parameter DefaultGateway System.String The IP Address value to set for the Default Gateway of the NetAdapter. .Parameter PrefixLength System.Int The subnet mask for the new NetIPAddress. .Parameter AddressFamily Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.AddressFamily Specifies IPv4 or IPv6 NetIPAddress. .Parameter PolicyStore Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Store Specifies if the NetIPAddress should be applied immediately or after the next reboot. .Parameter PreferredLifetime System.TimeSpan Specifies a preferred lifetime, as a TimeSpan object, for an NetIPAddress. .Parameter ValidLifetime System.TimeSpan Specifies a valid lifetime value, as a TimeSpan object, for an NetIPAddress. .Parameter SkipAsSource System.Boolean If used, the new NetIPAddress will not register with DNS. .Parameter Type Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Type Specifies an NetIPAddress type. .Parameter DnsServerAddress System.String The IP Address of a server running Domain Name Services. .Parameter CimSession Microsoft.Management.Infrastructure.CimSession Runs the cmdlet in a remote session or on a remote computer. .Parameter ThrottleLimit System.Int Specifies the maximum number of concurrent operations that can be established to run the cmdlet. .Example PS C:\> New-NetStaticIPAddress -InterfaceIndex 3 -IPAddress 192.168.1.1 -DefaultGateway 192.168.1.0 -PrefixLength 24 -DnsServerAddress 192.168.1.0 -Confirm:$false IPAddress : 192.168.1.1 InterfaceIndex : 3 InterfaceAlias : Ethernet AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Manual SuffixOrigin : Manual AddressState : Tentative ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : ActiveStore .Notes This will reset the NetAdapter, which means it will break the connection to a system if running remotely. .Link https://dotps1.github.io .Link https://www.powershellgallery.com/packages/New-NetStaticIPAddress .Link https://grposh.github.io #> #requires -Modules NetTCPIP [CmdletBinding( ConfirmImpact = "High", SupportsShouldProcess = $true )] [OutputType( [Microsoft.Management.Infrastructure.CimInstance] )] param ( [Parameter( ParameterSetName = "ByInterfaceIndex", ValueFromPipelineByPropertyName = $true )] [Alias( "ifIndex" )] [Int] $InterfaceIndex, [Parameter( ParameterSetName = "ByInterfaceAlias", ValueFromPipelineByPropertyName = $true )] [Alias( "ifAlias" )] [String] $InterfaceAlias, [Parameter( Mandatory = $true )] [String] $IPAddress, [Parameter( Mandatory = $true )] [String] $DefaultGateway, [Parameter( Mandatory = $true )] [ValidateRange( 8, 32 )] [Alias( "SubnetMask" )] [Byte] $PrefixLength, [Parameter()] [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.AddressFamily] $AddressFamily = "IPv4", [Parameter()] [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Store] $PolicyStore = "ActiveStore", [Parameter()] [TimeSpan] $PreferredLifetime = [TimeSpan]::MaxValue, [Parameter()] [TimeSpan] $ValidLifetime = [TimeSpan]::MaxValue, [Parameter()] [Bool] $SkipAsSource = $false, [Parameter()] [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Type] $Type = "Unicast", [Parameter()] [ValidateCount( 1, 3 )] [String[]] $DnsServerAddress = $null, [Parameter()] [CimSession[]] $CimSession = $null, [Parameter()] [Int] $ThrottleLimit = 0 ) begin { if ($PSBoundParameters.ContainsKey("CimSession")) { $Local:PSDefaultParameterValues = @{ "*:CimSession" = $CimSession "*:ThrottleLimit" = $ThrottleLimit } } } process { switch ($PSCmdlet.ParameterSetName) { "ByInterfaceIndex" { try { $interface = Get-NetAdapter -InterfaceIndex $InterfaceIndex -ErrorAction Stop } catch { Write-Error $_ return } } "ByInterfaceAlias" { try { $interface = Get-NetAdapter -Name $InterfaceAlias -ErrorAction Stop } catch { Write-Error $_ return } } } if ($PSCmdlet.ShouldProcess($interface)) { if ($null -ne (Get-NetIPAddress -InterfaceIndex $interface.InterfaceIndex)) { Remove-NetIPAddress -InterfaceIndex $interface.InterfaceIndex -AddressFamily $AddressFamily -Confirm:${ConfirmPreference} } if ($null -ne (Get-NetRoute -InterfaceIndex $interface.InterfaceIndex)) { Remove-NetRoute -InterfaceIndex $interface.InterfaceIndex -AddressFamily $AddressFamily -Confirm:${ConfirmPreference} } New-NetIPAddress -AddressFamily $AddressFamily -DefaultGateway $DefaultGateway -InterfaceIndex $interface.InterfaceIndex -IPAddress $IPAddress -PolicyStore $PolicyStore -PreferredLifetime $PreferredLifetime -ValidLifetime $ValidLifetime -PrefixLength $PrefixLength -SkipAsSource $SkipAsSource -Type $Type if ($null -ne $DnsServerAddress) { Set-DnsClientServerAddress -InterfaceIndex $interface.InterfaceIndex -ServerAddresses $DnsServerAddress } } } end { if ($null -ne $Local:PSDefaultParameterValues) { $Local:PSDefaultParameterValues.Clear() } } |