perform nslookup from PowerShell

bowang picture bowang · Sep 12, 2012 · Viewed 24.7k times · Source

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()

}

Answer

alroc picture alroc · Sep 12, 2012

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