英文:
Adding NSLookup to Ping Script
问题
以下是您要翻译的内容:
I have this ping script that works well for what I need. I want to add to this but not sure how. I want it to output like a NSLookup. Sometimes I have the host name for the printer and would like it to output the IP if one is found and add that to another column.
First EX:
Get-Content "C:\Users\Username\Desktop\New folder\hnames.txt" | ForEach-Object { #Change User to your name and after desktop where you have IPs you want to ping
if(Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue){
$status = 'Alive'
}
else {
$status = 'Dead'
}
$dns = Resolve-DnsName $_ -DnsOnly -ErrorAction SilentlyContinue
# output 1 object with two separate properties
[pscustomobject]@{
Status = $status
Target = $_
IPAddress = $dns.IPAddress -join ', '
}
} |Export-Csv 'C:\Users\Username\Desktop\New folder\output.csv' -NoTypeInformation #change this to where you want the export to go, Change Output to what you want to save as
Second EX:
Get-Content "C:\Users\Username\Desktop\New folder\hnames.txt" | ForEach-Object { #Change User to your name and after desktop where you have IPs you want to ping
if(Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue){
$status = 'Alive'
}
else {
$status = 'Dead'
}
# output 1 object with two separate properties
[pscustomobject]@{
Status = $status
Target = $_
IPAddress = $success.Address.IPAddressToString
}
} |Export-Csv 'C:\Users\Username\Desktop\New folder\output.csv' -NoTypeInformation #change this to where you want the export to go, Change Output to what you want to save as
英文:
I have this ping script that works well for what I need. I want to add to this but not sure how. I want it to output like a NSLookup. Sometimes I have the host name for the printer and would like it to output the IP if one is found and add that to another column.
First EX:
Get-Content "C:\Users\Username\Desktop\New folder\hnames.txt" | ForEach-Object { #Change User to your name and after desktop where you have IPs you want to ping
if(Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue){
$status = 'Alive'
}
else {
$status = 'Dead'
}
$dns = Resolve-DnsName $_ -DnsOnly -ErrorAction SilentlyContinue
# output 1 object with two separate properties
[pscustomobject]@{
Status = $status
Target = $_
IPAddress = $dns.IPAddress -join ', '
}
} |Export-Csv 'C:\Users\Username\Desktop\New folder\output.csv' -NoTypeInformation #change this to where you want the export to go, Change Output to what you want to save as
Second EX:
Get-Content "C:\Users\Username\Desktop\New folder\hnames.txt" | ForEach-Object { #Change User to your name and after desktop where you have IPs you want to ping
if(Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue){
$status = 'Alive'
}
else {
$status = 'Dead'
}
# output 1 object with two separate properties
[pscustomobject]@{
Status = $status
Target = $_
IPAddress = $success.Address.IPAddressToString
}
} |Export-Csv 'C:\Users\Username\Desktop\New folder\output.csv' -NoTypeInformation #change this to where you want the export to go, Change Output to what you want to save as
答案1
得分: 1
如果您正在执行主机名到IP地址的转换,您可以直接使用Test-Connection
的输出,例如:
Get-Content ..... | ForEach-Object {
$status = 'Dead'
if($success = Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue) {
$status = 'Alive'
}
[pscustomobject]@{
Status = $status
Target = $_
IPAddress = $success.Address.IPAddressToString
}
} | Export-Csv ..... -NoTypeInformation
另一个选项是使用Resolve-DnsName
:
Get-Content ..... | ForEach-Object {
$status = 'Dead'
if(Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue) {
$status = 'Alive'
}
$dns = Resolve-DnsName $_ -ErrorAction SilentlyContinue
[pscustomobject]@{
Status = $status
Target = $_
IPAddress = $dns.IPAddress -join ', '
}
} | Export-Csv ..... -NoTypeInformation
英文:
If you're doing hostname to IP Address you could use the output from Test-Connection
already, for example:
Get-Content ..... | ForEach-Object {
$status = 'Dead'
if($success = Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue) {
$status = 'Alive'
}
[pscustomobject]@{
Status = $status
Target = $_
IPAddress = $success.Address.IPAddressToString
}
} | Export-Csv ..... -NoTypeInformation
Another option is to use Resolve-DnsName
:
Get-Content ..... | ForEach-Object {
$status = 'Dead'
if(Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue) {
$status = 'Alive'
}
$dns = Resolve-DnsName $_ -ErrorAction SilentlyContinue
[pscustomobject]@{
Status = $status
Target = $_
IPAddress = $dns.IPAddress -join ', '
}
} | Export-Csv ..... -NoTypeInformation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论