1

I have the following code and I cannot loop the items using PowerShell.

$ipAddress = @('107.20.253.26', '107.20.178.220', '8.8.8.8')

for($i=0; $i -le $ipAddress.count; $i++) {

    $resolve = nslookup $i | Format-list
    $resolve | Out-File $resolveFile

}

1 Answer 1

2

For me, this loops over the IPs fine. There is an easier way to do in PowerShell though using foreach.
And remove Format-Table; it is useful when writing to host but just turned your nslookup result to Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData datatype.
Use -Append with Out-File to avoid overwriting previous result.

$ipAddress = @('107.20.253.26', '107.20.178.220', '8.8.8.8')

foreach($ip in $ipAddress) {

    # remove Format-Table
    $resolve = nslookup $ip

    # Add Append flag so that you are not overwriting previous contents on each loop
    $resolve | Out-File $resolveFile -Append

}
Sign up to request clarification or add additional context in comments.

1 Comment

Great solution. Thank you very much. This worked really great and effortlessly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.