I have been automating the provisioning of Hyper-V hosts using SCVMM and PowerShell, and one of the tasks that I had to do involved to set an use powershell to set an ip address , but also use powershell to set an ip subnetmask , and to set the a gateway.
Use Powershell ip address:
I knew the existence of Set-NetIpAddress and the New-NetIpAddress cmdlets but I have never used them before. Now that i wanted to set to use powershell ip address, it was the perfect opportunity to answer the question: How to set an ip address using powershell? So let’s to try them out.
Naturally I tried to use the ‘set-NetIpAddress’, but I kept having the following error:
C:\Users\Stephane> Set-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.2.22
Set-NetIPAddress : No matching MSFT_NetIPAddress objects found by CIM query for instances of the
ROOT/StandardCimv2/MSFT_NetIPAddress class on the CIM server: SELECT * FROM MSFT_NetIPAddress WHERE ((IPAddress LIKE
'192.168.2.22')) AND ((InterfaceIndex = 12)). Verify query parameters and retry.
At line:1 char:1
+ Set-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.2.22
+ CategoryInfo : ObjectNotFound: (MSFT_NetIPAddress:String)[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container][fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" layout="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none" last="no" hover_type="none" link="" border_position="all"][fusion_text][Set-NetIPAddress], CimJobException
+ FullyQualifiedErrorId : CmdletizationQuery_NotFound,Set-NetIPAddress
I soon noticed that the Set-NetIpAddress cmdlet wasn’t really working the way I was expecting.
If you have a closer look at the set-NetIpAddress help on msdn, we will see the following:
The Set-NetIPAddress cmdlet modifies IP address configuration properties of an existing IP address.
To create an IPv4 address or IPv6 address, use the New-NetIPAddress cmdlet.
You can set an ip address using New-NetIpAddress indeed, but there is a caveat. If one is already set, you will actually set a new primary ip address, and the one that existed before, will be shifted to be a secondary IP address. If you call the cmdlet again, you will shift that first IP address to the third position, without actually replacing the ip address. This goes for the gateway address as well actually.
In the end, if you want to replace an ip address with a new one using powershell (or if you want to change a gateway using powershell) you will need to remove the old ip address (or gateway) first, and then set an powershell ip address.
Now, to remove an ip address there is a handy little cmdlets called ‘remove-netipaddress’, which works just as you would expect (unlike set-ipaddress) but to remove the gateway, it is is a bit trickier. In order to remove the gateway using powershell, we will have to remove the route going there. If the gateway you want to remove is 192.168.2.1 you will have to remove the route using remove-netroute as follow:
Get-NetRoute -InterfaceIndex 12 -NextHop 192.168.2.1 [/fusion_text][fusion_text] Remove-NetRoute -Confirm:$false
I ended up writing the function below which, which takes care of all of this additional logic, and give us the possibility to ‘simply’, replace an ip address using powershell.
Read more about powershell ip address:
While doing some researches on automating IP related tasks, I wrote down the following interesting links. If you have a some time, you could go and have visit ^^
Love the functions Test-IsValidIP from Boe prox on the powershell magazine:
Read these other ressources to learn about this and see this from another angle: