param( [Parameter(Mandatory=$True)][string]$MACAddress, [Parameter(Mandatory=$True)][string]$ClusterIP ) function GetNetworkAdapterByMAC( [Parameter(Mandatory=$True)][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!" -ForegroundColor:Red; return $null; } elseif($NetworkAdapter.Count -lt 1) { Write-Host "No network card with specified mac address was found!" -ForegroundColor:Red; return $null; } else { return $NetworkAdapter[0]; } } function IsDHCPEnabledOnMAC( [Microsoft.Management.Infrastructure.CimInstance] $NIC ) { $IPInterface = @(); If($NIC -ne $null) { $IPInterface += $NIC | Get-NetIPInterface -AddressFamily:IPv4 | 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 ($_)!" -ForegroundColor:Red; return $null; } } } else { Write-Host "Unexpected ip interface(s) was found ($($IPInterface.Count))!" -ForegroundColor:Red; return $null; } } else { Write-Host "No NIC Object was given!" -ForegroundColor:Red; return $null; } } function Back2DHCP( [Microsoft.Management.Infrastructure.CimInstance] $NIC ) { If($NIC -ne $null) { Write-Host "Try to rollback DHCP. This could cause network disconnects." -ForegroundColor:Yellow; Sleep 1; Get-NetRoute -DestinationPrefix "0.0.0.0/0" -AddressFamily:IPv4 -ErrorAction:SilentlyContinue | Remove-NetRoute -Confirm:$false -ErrorAction SilentlyContinue; $NIC | Get-NetIPAddress -AddressFamily:IPv4 | Remove-NetIPAddress -Confirm:$false -ErrorAction SilentlyContinue; $NIC | Set-NetIPInterface -DHCP Enabled -AddressFamily:IPv4; Set-DnsClientServerAddress -InterfaceIndex $NIC.InterfaceIndex -ResetServerAddresses -ErrorAction SilentlyContinue; Sleep 2; $WMINWA = $(Get-WmiObject Win32_NetworkAdapterConfiguration)|?{$_.MacAddress -ne $null -and $_.MacAddress.Replace(':','') -eq $NIC.MACAddress.Replace('-','')}; Sleep 2; [int]$Tries = 0; do { $ReturnValue = $WMINWA.ReleaseDHCPLease().ReturnValue; if($ReturnValue -ne 0) { Sleep 1; } } while($Tries++ -lt 5); Sleep 2; do { $ReturnValue = $WMINWA.RenewDHCPLease().ReturnValue; if($ReturnValue -ne 0) { Sleep 1; } } while($Tries++ -lt 5); } } function ConvertDynamic2StaticIPConfiguration( [Microsoft.Management.Infrastructure.CimInstance] $NIC ) { If($NIC -ne $null) { $IPaddressConfigs = @(); $IPaddressConfigs += $NIC | Get-NetIPConfiguration; if($IPaddressConfigs.Count -ne 1) { Write-Host "An unxpected dhcp config was found. Unable to perform further actions." -ForegroundColor:Red; return $null; } Write-Host "Try to enable static IP-address. This could cause network disconnects." -ForegroundColor:Yellow; Sleep 1; $IPaddressConfig = $IPaddressConfigs[0]; $Prefix = ($IPAddressConfig | Get-NetIPAddress -AddressFamily:IPv4).PrefixLength; $DNSClientServerAddresses = $IPaddressConfig | Get-DnsClientServerAddress -AddressFamily IPv4; $NIC | Get-NetIPAddress -AddressFamily:IPv4 | Remove-NetIPAddress -Confirm:$false; New-NetIPAddress -IPAddress $IPaddressConfig.IPv4Address[0].IPAddress -AddressFamily:IPv4 -InterfaceIndex $NIC.InterfaceIndex -PrefixLength $Prefix -ErrorAction SilentlyContinue | Out-Null; if(!$?) { Write-Host "Unable to set static ip address." -ForegroundColor:Red; Back2DHCP -NIC $NIC; return $false; } New-NetRoute -AddressFamily:IPv4 –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." -ForegroundColor:Red; 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." -ForegroundColor:Red; Back2DHCP -NIC $NIC; return $false; } return $true; } else { Write-Host "No NIC Object was given!" -ForegroundColor:Red; return $false; } } function AddIPToNetworkAdapter ( [Microsoft.Management.Infrastructure.CimInstance] $NIC, [Parameter(Mandatory=$True)][String] $IPAddress, [bool] $SkipAsSource = $true ) { If($NIC -ne $null) { $NetIPAddress = Get-NetIPAddress -AddressFamily:IPv4 -InterfaceIndex $NIC.InterfaceIndex -IPAddress $IPaddress -PrefixLength 32 -SkipAsSource:$true -ErrorAction:SilentlyContinue; if($NetIPAddress -eq $null) { $NetIPAddress = New-NetIPAddress -AddressFamily:IPv4 -InterfaceIndex $NIC.InterfaceIndex -IPAddress $IPAddress -PrefixLength 32 -SkipAsSource $SkipAsSource -ErrorAction SilentlyContinue|?{$_.Store -eq 'ActiveStore'}; } if($? -and $NetIPAddress -ne $null) { return $true; } else { Write-Host "Cannot set new IP. Error on 'New-NetIPAddress':`n$($Error[0])" -ForegroundColor:Red; return $false; } } else { Write-Host "Cannot set new IP. No NIC Object was given!" -ForegroundColor:Red; return $false; } } function SetClusterIP( [Parameter(Mandatory=$True)][string]$MACAddress, [Parameter(Mandatory=$True)][string]$ClusterIP ) { $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."; } } $NewIPSuccess = AddIPToNetworkAdapter -NIC $NIC -IPAddress $ClusterIP; if($NewIPSuccess) { Write-Host "ClusterIP is successfully configured." -ForegroundColor:Green; } } } } SetClusterIP -MACAddress $MACAddress -ClusterIP $ClusterIP;