Friday, March 20, 2015

Get a list of IPs VIA System.Net.Dns

So needed to look up more IPs via scripts again. The last script I used worked great but didn't grab everything because of the environment I am in. So I took the rest of the systems I needed IPs for and noticed I could use nslookup to get the IPs. Of course nslookup is not made for automation so I went looking again. And again powershell came to the rescue.

In the System.Net.Dns Class.

The script i found came from this website.
http://community.spiceworks.com/scripts/show/1201-powershell-script-dns-forward-lookup-script-with-auto-generate-excel-file

Here is the script from that page. It basically grabs a list of names, or ips from text file you supply and it spits back out into an excel document.. SWEEET...


 ###########################################################################
#
# NAME: DNS Forward Lookup Script with Auto Generate Excel File
#
# AUTHOR: J.Malek (www.malekjakir.com) Email: malek dot one zero four zero at gmail dot com
#
# COMMENT: This script can be used to check list of Servers for Forward NSLookup to get the IP Addresses.
# Line#29 - Please Change the path of Servers.txt to your file location.
# NOTE: One server name per line.
#
# VERSION HISTORY:
# 1.0 2/17/2012 -
#
###########################################################################
$ErrorActionPreference = "silentlycontinue"

$a = New-Object -comobject Excel.Application
$a.visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$c.Cells.Item(1,1) = "Server Hostname"
$c.Cells.Item(1,2) = "IP Address"
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$intRow = 2

$colComputers = get-content "C:\SCRIPTS\DNS_Lookup\ForwardLookup\Servers.txt"
foreach ($strComputer in $colComputers)
{
$FWDIP = [System.Net.Dns]::GetHostAddresses($strComputer) | Add-Member -Name HostName -Value $strComputer -MemberType NoteProperty -PassThru | Select HostName, IPAddressToString

$c.Cells.Item($intRow,1) = $FWDIP.Hostname
$c.Cells.Item($intRow,2) = $FWDIP.IPAddressToString
$intRow = $intRow + 1
}
$d.EntireColumn.AutoFit()
cls
Write-Host "######## This Script is completed now ########"
Thank you internet..


Thursday, March 19, 2015

Get IP addresses remotely

So recently had a task to get all the IPs from remote computers. Well it turns out this isn't as easy as it should be. After some major googleing I ended up using a PS script I found here, http://techibee.com/powershell/powershell-get-ip-address-subnet-gateway-dns-serves-and-mac-address-details-of-remote-computer/1367.

Here is the script.

[cmdletbinding()]
param (
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
)            

begin {}
process {
 foreach ($Computer in $ComputerName) {
  if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
   $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
   foreach ($Network in $Networks) {
    $IPAddress  = $Network.IpAddress[0]
    $SubnetMask  = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers  = $Network.DNSServerSearchOrder
    $IsDHCPEnabled = $false
    If($network.DHCPEnabled) {
     $IsDHCPEnabled = $true
    }
    $MACAddress  = $Network.MACAddress
    $OutputObj  = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj
   }
  }
 }
}            

end {}
I then used a txt file to input all the servers into the script. This was in the comments sections so I will add it in here..

"get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto
where c:\temp\computers.txt is the file that contains computers list."

I used this:
get-Content .\computers.txt | .\Get-IpDetails.ps1 | ft -auto
Also remember that you are running a scriptlet so you need to set this unless you have something else done on your system. 
Set-ExecutionPolicy Unrestricted
Enjoy.