perform nslookup from PowerShell

2020-07-27 06:40发布

问题:

I'm writing a powershell to exact the ip from a server name, which need me to embed the nslookup code into my powershell

how can I do the intergrating work?

Can any body help me?

Add-PSSnapin Microsoft.SharePoint.PowerShell
$web = Get-SPWeb -Identity “http://nycs00058260/sites/usitp“
$server_status = "PROD"
$list=$web.Lists[”DNS_Status”]
$items = $list.items 
Foreach($item in $items){
    $item_name = $item["Server_name"]  #need to get the ip by this name

    /*nslook up*/
     $item_name.update()

}

回答1:

If you install the PSCX module, it comes with a cmdlet Resolve-Host which handles name lookups.

Absent that, this one-liner will do the job

[System.Net.Dns]::GetHostAddresses("www.msn.com")

You can also pass in an IP address - but the results will be different.

See also http://blogs.msdn.com/b/powershell/archive/2006/06/26/647318.aspx & http://powershell.com/cs/media/p/210.aspx



回答2:

PowerShell 3.0 on Windows 8 and higher comes with a Resolve-DnsName cmdlet that will get this information:

(Resolve-DnsName $server_name)[0].IpAddress


回答3:

Simply use :

Resolve-DnsName monServer | ? { # make selection here } | % { $_.IPAdress }  | select-object -first 1