添加 NSLookup 到 Ping 脚本

huangapple go评论50阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月14日 03:42:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定