none
Need to print "address" with each iteration in given Powershell script RRS feed

  • Question

  • Hi there,

    I have to print input computer name (address) and alias name (if present) in following script with each iteration to fetch the DNS lookup for the bulk server provided in text file. 

    $InputFile = 'C:\computer.txt'
    $addresses = get-content $InputFile
    $reader = New-Object IO.StreamReader $InputFile
        while($reader.ReadLine() -ne $null){ $TotalIPs++ }
    write-host    ""    
    write-Host "Performing nslookup on each address..."    
            foreach($address in $addresses) {
                ## Progress bar
                $i++
                $percentdone = (($i / $TotalIPs) * 100)
                $percentdonerounded = "{0:N0}" -f $percentdone
                Write-Progress -Activity "Performing nslookups" -CurrentOperation "Working on IP: $address (IP $i of $TotalIPs)" -Status "$percentdonerounded% complete" -PercentComplete $percentdone
                ## End progress bar
                try {
    					[system.net.dns]::resolve($address)| Select HostName,AddressList
                    }
                    catch {
                        Write-host "$address was not found. $_" -ForegroundColor Green
                    }
                }
    write-host    ""            
    write-Host "Pinging each address..."
            foreach($address in $addresses) {
                ## Progress bar
                $j++
                $percentdone2 = (($j / $TotalIPs) * 100)
                $percentdonerounded2 = "{0:N0}" -f $percentdone2
                Write-Progress -Activity "Performing pings" -CurrentOperation "Pinging IP: $address (IP $j of $TotalIPs)" -Status "$percentdonerounded2% complete" -PercentComplete $percentdone2
                ## End progress bar
                    if (test-Connection -ComputerName $address -Count 2 -Quiet ) {  
                        write-Host "$address responded" -ForegroundColor Green 
                        } else 
                        { Write-Warning "$address does not respond to pings"              
                        }  
            }
    write-host    ""        
    write-host "Done!"


    • Edited by Rai01 Tuesday, July 10, 2018 5:25 AM
    • Moved by Bill_Stewart Tuesday, December 11, 2018 8:44 PM This is not "debug/fix/rewrite my script for me" forum
    Tuesday, July 10, 2018 5:24 AM

All replies