Simple Script to Automate Task

Sumner, Khalid 20 Reputation points
2023-08-31T15:02:50.1366667+00:00

Just need a script I can run in Powershell that will take a list of PC hostnames, ping them and return the IP addresses.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,388 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dillon Silzer 57,481 Reputation points
    2023-08-31T15:53:36.4233333+00:00

    You can try the following:

    ## Q:\Test\2017\01\21\SO_41785413.ps1
    $FileOut = "C:\Users\YOURUSERNAME\Downloads\Computers.csv"
    ## Ping subnet
    $Subnet = "192.168.50."
    1..254|ForEach-Object{
        Start-Process -WindowStyle Hidden ping.exe -Argumentlist "-n 1 -l 0 -f -i 2 -w 1 -4 $SubNet$_"
    }
    $Computers =(arp.exe -a | Select-String "$SubNet.*dynam") -replace ' +',','|
      ConvertFrom-Csv -Header Computername,IPv4,MAC,x,Vendor|
                       Select Computername,IPv4,MAC
    
    ForEach ($Computer in $Computers){
      nslookup $Computer.IPv4|Select-String -Pattern "^Name:\s+([^\.]+).*$"|
        ForEach-Object{
          $Computer.Computername = $_.Matches.Groups[1].Value
        }
    }
    $Computers
    $Computers | Export-Csv $FileOut -NotypeInformation
    #$Computers | Out-Gridview
    

    This will generate:

    User's image

    Cited from https://stackoverflow.com/questions/41785413/use-powershell-to-get-device-names-and-their-ipaddress-on-a-home-network


    If this is helpful please accept answer.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.