方法一
$CSVFile = “C:\IPAddress.csv”
$ServerList = “C:\ServerList.txt”
Get-Content $ServerList |Foreach {
$Result = “”|select “ComputerName”,“IPAddress”
$Result.ComputerName = $_
try{
I
P
A
d
d
r
e
s
s
=
(
[
S
y
s
t
e
m
.
N
e
t
.
D
n
s
]
:
:
G
e
t
H
o
s
t
A
d
d
r
e
s
s
e
s
(
IPAddress = ([System.Net.Dns]::GetHostAddresses(
IPAddress=([System.Net.Dns]::GetHostAddresses() | Where-Object {$.AddressFamily -eq ‘InterNetwork’} | Select-Object -ExpandProperty IPAddressToString) -join “;”
$Result.IPAddress = $IPAddress
}catch{
$Result.IPAddress = “N/A”
}
$Result |Export-Csv -Path $CSVFile -NoTypeInformation -Encoding UTF8 -Append
}
方法二:
$Computers = Get-Content C:\ServerList.txt
$DnsResult = Foreach( $Computer in $Computers ){
$IpAddress = Resolve-DnsName $Computer -Type A -ErrorAction SilentlyContinue| Select -ExpandProperty IpAddress
If( KaTeX parse error: Expected '}', got 'EOF' at end of input: …@{Computername=Computer;Ipv4Address=KaTeX parse error: Expected 'EOF', got '}' at position 10: IpAddress}̲ } Else…Computer;Ipv4Address=$IpAddress}
}
}
$DnsResult | export-csv -NoTypeInformation c:\export.csv