Hi,
I am new to powershell, started just two weeks ago. I am trying to write a script to use resolve-dnsname cmdlet for a list of servers from a text file and write the output to a text file including errors. Please assist if you could help me .
7 Spice ups
It seems resolve-dnsname silently fails if it can’t resolve so you won’t get errors but try this
$computers = Get-Content C:\computerlist.txt
foreach ($computer in $computers){
$result = @{"Computer Name" = $computer; "IP Address" = (Resolve-DNSName -Name $computer | Select IPAdress)}
}
$result | Export-Csv -NoTypeInformation C:\export.csv
Blank entries for IP Address will mean not resolved
Above doesn’t work fully. Looks like I need to brush up on Export-Csv but I’ll come back to you if no one else fixes it
Ok sorted it it needs an array declaration. In fact this is probably a little overkill for the situation but it gives an output
$computers = Get-Content D:\computerlist.txt
$export = New-Object System.Collections.ArrayList($null)
foreach ($computer in $computers){
$result = @{'Computer Name' = $computer;'IP Address' = ((Resolve-DNSName -Name $computer).IPAddress)}
$export.add((New-Object psobject -Property $result)) | Out-Null
}
$export | Export-Csv -NoTypeInformation D:\export.csv
1 Spice up
Matt2554,
You’re on the right track, but I would suggest you modify the first version to output custom objects, rather than hand-building an array and appending to it in a loop:
$Computers = Get-Content C:\computerlist.txt
$Result = foreach ($Computer in $Computers){
[PSCustomObject]@{
"ComputerName" = $Computer
"IPAddress" = (Resolve-DNSName -Name $computer | Select-Object IPAdress)
}
}
$Result | Export-Csv C:\export.csv -NoTypeInformation
Notice that I changed the variable assignment to include the entire results of the foreach loop. This will give you an array of custom objects that you can pass to Export-Csv.
2 Spice ups
Also:
Get-Content C:\computerlist.txt | Resolve-DNSName | Select Name,IPAddress
3 Spice ups
Ithink if you do that you will get a double entry on the name because of the way the output appears so first line is host name and second is domain like this
Name IPAddress
---- ---------
host.domain.com ip.of.my.nic
domain.com
Matt, the cmdlet works fine. If you’re getting unexpected results it’s more likely to be something in your computerlist.txt file.
1 Spice up
Just looking at this bit of code - the others were snipped.A more PowerShell way to do this is:
$Export = @()
lI’ts also a LOT faster (assuming my calculations are correct:
1 Spice up
The cmdlet works ok yes but when I run Resolve-DNSName it returns more information than I was expecting. At least it does if you run it against a DC as you get a whole load of extra info
Name : domain.com
QueryType : SOA
TTL : 3600
Section : Authority
NameAdministrator : hostmaster.domain.com
SerialNumber : 31630
TimeToZoneRefresh : 900
TimeToZoneFailureRetry : 600
TimeToExpiration : 86400
DefaultTTL : 0
As the op said he is going to run against server some of which could be DCs I picked my longer solution
That’s what the Select at the end does, it limits it to just the properties you want to see.
it depends on what you’re trying to do. if you’re creating a bunch of arrays/arraylists, using an array would be faster as you mentioned, but i think in Matt2554’s context, he is only creating a single arraylist and adding to it which is (in my experience) usually much faster than using an array with the += operator
$a1 = New-Object System.Collections.ArrayList
$a2 = New-Object System.Collections.ArrayList
$a3 = New-Object System.Collections.ArrayList
$a4 = New-Object System.Collections.ArrayList
1..2 | % {
$null = $a1.Add($(Measure-Command {
$ArrayList = New-Object System.Collections.ArrayList
1..1000 | % {
$null = $ArrayList.Add($_)
}
rv ArrayList
}).TotalSeconds)
$null = $a2.Add($(Measure-Command {
$List = New-Object System.Collections.Generic.List[System.String]
1..1000 | % {
$List.Add($_)
}
$ListArray = $List.ToArray()
rv List
rv ListArray
}).TotalSeconds)
$null = $a3.Add($(Measure-Command {
$Array1 = @()
1..1000 | % {
$Array += $_
}
rv Array1
}).TotalSeconds)
$null = $a4.Add($(Measure-Command {
$Array2 = 1..1000 | % {
$_
}
rv Array2
}).TotalSeconds)
}
@"
Method,Time
ArrayList,$($a1 | measure -Sum | % {$_.sum})
List,$($a2 | measure -Sum | % {$_.sum})
"array +=",$($a3 | measure -Sum | % {$_.sum})
"array = @(for(...){...})",$($a4 | measure -Sum | % {$_.sum})
"@ | ConvertFrom-Csv | sort time | ft -AutoSize
# output:
# Method Time
# ------ ----
# array = @(for(...){...}) 0.0900224
# ArrayList 0.0909059
# List 0.0969492
# array += 0.1094896
Hi All,
Thanks for your replies. Actually i was trying not to use arrays as it got a bit confusing for me. I know, my bad. Isn’t there a simple way to just get the output in text file.
This should give you the list you want. For some reason my resolve-dnsname doesn’t fail silently like yours does, so I had to add -erroraction silently continue.
%uFEFF$computers = Get-Content C:\computerlist.txt
$DnsResult = Foreach( $Computer in $Computers ){
$IpAddress = Resolve-DnsName $Computer -Type A -ErrorAction SilentlyContinue| Select -ExpandProperty IpAddress
If( $IpAddress ){
[PsCustomObject]@{Computername=$Computer;Ipv4Address=$IpAddress}
}
Else{
[PsCustomObject]@{Computername=$Computer;Ipv4Address=$IpAddress}
}
}
$DnsResult | export-csv -NoTypeInformation c:\export.csv