Link to home
Start Free TrialLog in
Avatar of Newguy 123
Newguy 123

asked on

Build a PowerShell script to resolve DNS names for a domains that are placed in a csv file.

Hello Exports. I am trying to build a PS script to resolve DNS names for a few domains. I have the following script:

$domain1 = "test.com"
$notfound = "`tDNS Entry Not Found"
ForEach ($domain in $domain1)      {
      $lyncdiscoverresult = Resolve-DnsName lyncdiscover.$domain1 -Server 8.8.8.8 -ErrorAction SilentlyContinue
if ($lyncdiscoverresult) {
    Write-Host "`tlyncdiscover.$domain1 $(@(foreach ($result in $lyncdiscoverresult.namehost){ '-> '+$result }))" -ForegroundColor Yellow
} else {
    Write-Host $notfound -ForegroundColor Red
}
select-object Name | Export-Csv "C:\Users\mohsan.chaudhary\Desktop\domain5.csv"
}

I get the desired output in powershell just fine, but the exported csv file is empty. How can i populate the csv file with the results. Also, I would like to import a csv file with a list of domains, that i am placing into the first variable like this: $domain1 = import-csv "c\location\domain.csv". The PS output displays 'DNS Entry Not found' for all domains, even when i know some of the domains should be resolvable (when i run separately each domain like above, then it resolves fine, but when i import a csv file that includes the domain, then all domains are unresolvable). How can run the above script against a import-csv and export the results to a csv file?
Avatar of Mahesh
Mahesh
Flag of India image

this is because your output line is beyond loop

put output line in IF action block

i.e.

if ( some condition) {

some action and output line
} else
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try this simple command line. It will give you FQDN of server. Make sure you use powershell version 3 or higher.

[net.dns]::GetHostEntry($Computer).Hostname