I'm new to PowerShell scripting. I am trying to make a script which resolves a list of DNS names to their IPs and outputs them as a CSV file. When a server is unresolvable it should be output to a separate file.
Here is my code:
$location
$location = Read-Host "CSV Path: "
$Names = Import-Csv -Path $location
foreach($n in $Names)
{
try
{
$output = $n.NAME
$variable1 = [system.net.dns]::resolve($output) | Select
HostName,AddressList
$variable1
$IP = $variable1.AddressList | Select-Object IPAddressToString
$IPN = $IP.IPAddressToString
$csv+=New-Object psobject -Property @{IPAddresse= $IPN;Name=
$output} |
Export-Csv \\filepath\Working.csv -NoTypeInformation
}
catch
{
Write-host "$output is unreachable."
Write-Output "$output" | Export-Csv \\Filepath\Unreachable.csv -
Append -Encoding ASCII
}
}
edit: The code was working quite well but now it says there is a delimiter mistake with the import but i dont know why this all of a sudden comes up as the code worked didnt got any edit and suddenly doesnt work anymore
Here's a simplified/corrected version of your code: