
close
close
In today’s PowerShell Problem Solver, we’ll wrap up a series of articles that began with a simple request to update a CSV file of computer information with IP addresses.
As I mentioned at the end of the last article, using Test-Connection to resolve a computer name to IP may not always be an option. So instead of asking the computer for its IP address, we’ll ask DNS. I’m going to use the same CSV file of unrevised computer information.
Our source of computers (Image Credit: Jeff Hicks)
Using Resolve-DnsName (Image Credit: Jeff Hicks)
advertisment
I can update an individual entry like this:Resolving a computer name (Image Credit: Jeff Hicks)
I should point out that this command worked with a Netbios name because the computer was online. If I try to use a pure DNS lookup, then it fails.Verifying the results (Image Credit: Jeff Hicks)
In this situation, I need to provide a fully qualified name.Using a DNS lookup (Image Credit: Jeff Hicks)
I point this out because even though one of the computers in my list doesn't have an IP address, there is a static entry for it in DNS. But I can only get it with a FQDN.Resolving a name with a fully qualified name (Image Credit: Jeff Hicks)
Depending on your situation, you may need to take this into account. In my case, I intend to use a ForEach-Object construct and update any object in my collection that is missing an IP address.Resolving a name only with DNS (Image Credit: Jeff Hicks)
I'm using the –F operator to plug in each computername into the {0} placeholder to build a FQDN. Again, I'm using Write-Host so you can see what is happening.Look at this now:Updating missing IP addresses (Image Credit: Jeff Hicks)
Now I can perform other updates and export to a CSV file as necessary. But what if you don't have the Resolve-DnsName cmdlet? In that case, you'll need to drop down into the .NET Framework and resolve names yourself with the [System.Net.DNS] class. This class has a method called Resolve(). All you need to do is specify a fully qualified host name.Updated results (Image Credit: Jeff Hicks)
It looks like all I need is the AddressList property.Resolving names with System.Net.DNS (Image Credit: Jeff Hicks)
That's not quite right. This looks like a nested object. I bet I can use that IPAddresstoStringProperty.AddressList property (Image Credit: Jeff Hicks)
Yes! That's what I need. Now that I've worked out the commands, I can use ForEach-Object and update my collection of computers.Getting IP Address string (Image Credit: Jeff Hicks)
The output will be very similar to what I did with Resolve-DnsName, and the results are the same.
And I think that about wraps this all up. We started with a simple CSV file but moved beyond that. I hope you are recognizing that instead of trying to manipulate text in a file, I worked with objects in the PowerShell pipeline. Getting data into a text file is pretty easy. But getting the data the way you need it is where the fun lies, and hopefully you've discovered that it doesn't have to be that difficult. As always, comments and questions are welcome.Revised results (Image Credit: Jeff Hicks)
More from Jeff Hicks
advertisment
Petri Newsletters
Whether it’s Security or Cloud Computing, we have the know-how for you. Sign up for our newsletters here.
advertisment
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri
Log in to save content to your profile.
Article saved!
Access saved content from your profile page. View Saved
Join The Conversation
Create a free account today to participate in forum conversations, comment on posts and more.
Copyright ©2019 BWW Media Group