none
Powershell - Pulling IP from Hostname File RRS feed

  • Question

  • Hello,

    Long time lurker of these forums, and have picked up a lot of great info here.

    I am basically brand new to Powershell, and just picked it up the other day.  I have experience with batch scripts, and a bit of vbscript, but powershell is brand new to me.  I basically picked a few scripts over the last few day's and pieced them together to get me some good results, on other things, then what my post is about today.

    Please be gentle, I'm new, and as time permits will try to find time to learn.... Until then I'm learning as I go....

    With that said, I am attempting to create a script and am stuck, and was hoping for some help.  Basically, what I am trying to do is pull IP addresses from remote servers across my network from a text file.  So the way I have my script setup is as follows:

    All files exist in the same directory on a server in my organization.  The powershell script, the hostnames.txt file that contains a list of all server names, and the batch script that I use to execute my powershell script with.  I am a domain admin so permissions are not an issue.  The batch file kicks off my powershell script and executes.  What I want to occur is the following:  Script runs, pulls all remote server/host names from the text file (hostnames.txt) (I have already copied the list of server names into this text file) and then saves the output to a .csv file.

    After looking at many scripts I like the following format the best, because it works well with Excel and does away with the "Braces".  Again, I have pieced this script together from many things I have seen on the internet, so I'm sure there are better way's of doing this, however I love the formatting on this.

    Here is my problem:  After the script completes, it only pulls about 5-8 names from my text file (please note that I have about 300 servers listed in my text file).  When the script completes it does it's job and outputs the .csv file and in the format I need, and love :-).  The problem again, is that it only outputs 5-8 servers out of my list, and then quits. 

    For troubleshooting sakes, I have tried running the script with 10 servers (in case there was a bottleneck somewhere). I also tried skipping the server that it kept getting stuck on, and it stopped there anyways, so it wasn't specific to that server in the text file.  I turned on "silentlycontinue" in my script so I would think that an error wouldn't just stop it.  I would love if the script (rather then skipping over the servers that are not online) would just put a "null" value in the output as well.

    ====================================================

    So without further delay, here is my code:

    ====================================================

    $erroractionpreference = "SilentlyContinue"
    $computers = (Get-Content .\hostnames.txt)

    get-wmiobject win32_networkadapterconfiguration -computername $computers -filter "IPEnabled='True'" |
    Select MACAddress,@{Name="IP";Expression={$_.IPAddress[0]}},
    @{Name="DefaultGateway";Expression={$_.DefaultIPGateway[0]}},
    DNSHostname,@{Name="DNSServer";Expression={$_.DNSServerSearchOrder[0]}} |
    Export-CSV .\Results.csv

    ====================================================

    I do realize that the code grabs additional information such as MACaddress and default gateway, but that is easy enough for me to remove from the .CSV file.  Again, more information is fine, as long as it is easily removed.  The information I really need is host name and ip address, the rest I can live without.  I just don't understand why the script stops.

    Thanks for any help that can be given to me.


    • Edited by Tek_Guy Thursday, February 13, 2014 8:05 PM
    Thursday, February 13, 2014 8:04 PM

Answers

  • Hi,

    Rather than querying WMI on the remote machines, I'm getting the IP information from Test-Connection:

    Get-Content .\hostnames.txt | ForEach {
    
        $details = Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue
    
        if ($details) {
    
            $props = @{
                ComputerName = $_
                IP = $details.IPV4Address.IPAddressToString
            }
    
            New-Object PsObject -Property $props
        }
    
        Else {    
            $props = @{
                ComputerName = $_
                IP = 'Unreachable'
            }
    
            New-Object PsObject -Property $props
        }
    
    } | Sort ComputerName | Export-Csv .\IPReport.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    • Marked as answer by Tek_Guy Thursday, February 13, 2014 8:53 PM
    Thursday, February 13, 2014 8:31 PM

All replies