$MACAddress = "IHRE MAC-ADRESSE"; $AdditionalIP = "IHRE CLUSTER-IP"; function GetNetworkAdapterByMAC ([string] $MACAddress) { $MACAddress = $MACAddress.Replace(":","").Replace(" ","").Replace("-","").ToLower(); $NetworkAdapter = @(); $NetworkAdapter += Get-NetAdapter | where { $_.MACAddress.Replace(":","").Replace(" ","").Replace("-","").ToLower() -eq $MACAddress}; if($NetworkAdapter.Count -gt 1) { write-host "More than one network card with specified mac address was found!"; return $null; } elseif($NetworkAdapter.Count -lt 1) { write-host "No network card with specified mac address was found!"; return $null; } else { return $NetworkAdapter[0]; } } function IsDHCPEnabledOnMAC([Microsoft.Management.Infrastructure.CimInstance] $NIC) { $IPInterface = @(); If($NIC -ne $null) { $IPInterface += $NIC | Get-NetIPInterface | where {$_.ConnectionState -ne $null}; if($IPInterface.Count -eq 1) { switch($IPInterface.Dhcp.ToString().ToLower()) { "disabled" { return $false;} "enabled" { return $true;} default { write-host "unxpected ip interface dhcp state was found!"; return $null; } } } else { write-host "unexpected ip interface(s) was found!"; return $null; } } else { write-host "No NIC Object was given!"; return $null; } } function Back2DHCP ([Microsoft.Management.Infrastructure.CimInstance] $NIC) { Get-NetRoute -DestinationPrefix "0.0.0.0/0" | Remove-NetRoute -Confirm:$false -ErrorAction SilentlyContinue; $NIC | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$false -ErrorAction SilentlyContinue; $NIC | Set-NetIPInterface -DHCP Enabled; Set-DnsClientServerAddress -InterfaceIndex $NIC.InterfaceIndex -ResetServerAddresses -ErrorAction SilentlyContinue; } function ConvertDynamic2StaticIPConfiguration ([Microsoft.Management.Infrastructure.CimInstance] $NIC) { $IPaddressConfigs = @(); $IPaddressConfigs += $NIC | Get-NetIPConfiguration; if($IPaddressConfigs.Count -ne 1) { write-host "An unxpected dhcp config was found. Unable to perform further actions."; return $null; } $IPaddressConfig = $IPaddressConfigs[0]; $Prefix = ($IPAddress | Get-NetIPAddress).PrefixLength; $DNSClientServerAddresses = $IPaddressConfig | Get-DnsClientServerAddress -AddressFamily IPv4; $NIC | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$false; New-NetIPAddress -IPAddress $IPaddressConfig.IPv4Address[0].IPAddress -InterfaceIndex $NIC.InterfaceIndex -PrefixLength $Prefix -ErrorAction SilentlyContinue | Out-Null; if(!$?) { write-host "Unable to set static ip address."; Back2DHCP -NIC $NIC; return $false; } New-NetRoute –DestinationPrefix "0.0.0.0/0" –InterfaceIndex $NIC.InterfaceIndex –NextHop $IPaddressConfig.IPv4DefaultGateway[0].NextHop -ErrorAction SilentlyContinue | Out-Null; if(!$?) { write-host "Unable to set static default gateway."; Back2DHCP -NIC $NIC; return $false; } Set-DnsClientServerAddress -InterfaceIndex $NIC.InterfaceIndex -ServerAddresses $DNSClientServerAddresses.ServerAddresses -ErrorAction SilentlyContinue | Out-Null; if(!$?) { write-host "Unable to set static DNS server."; Back2DHCP -NIC $NIC; return $false; } return $true; } function AddIPToNetworkAdapter ( [Microsoft.Management.Infrastructure.CimInstance] $NIC, [String] $IPAddress, [bool] $SkipAsSource = $true ) { New-NetIPAddress -InterfaceIndex $NIC.InterfaceIndex -IPAddress $IPAddress -PrefixLength 32 -SkipAsSource $SkipAsSource -ErrorAction SilentlyContinue | Out-Null; } $NIC = GetNetworkAdapterByMAC -MACAddress $MACAddress; If($NIC -ne $null) { $DHCPEnabled = IsDHCPEnabledOnMAC -NIC $NIC; If($DHCPEnabled -ne $null) { If($DHCPEnabled) { $ConversionSuccess = ConvertDynamic2StaticIPConfiguration -NIC $NIC; if($ConversionSuccess -eq $null -or !($ConversionSuccess)) { return "Unable to convert ip configuration. Manual fixing is needed."; } } AddIPToNetworkAdapter -NIC $NIC -IPAddress $AdditionalIP -SkisAsSource $OnlyListening; } }