Powershell convert String to System.Net.IPAddress

Question:

I’m new to powershell and I’m trying to automate creating a DHCP reservation.

So far I’m able to get the IP address like so:

This returns a string like:

However, the Add-DhcpServer4Resrvation cmdlet does not accept an ip address as a string. It requires the IP address be a ‘System.Net.IpAddress’

How do you convert a string to a System,.Net.IPAddress?

According to this link, it should be easy like

However that doesn’t work.

Related question
Powershell, get ip4v address of VM

Answer:

[IPAddress] Wont work if there are spaces in the string

Remove them with Trim()

Source:

Powershell convert String to System.Net.IPAddress by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply