Showing posts with label Ip Address. Show all posts
Showing posts with label Ip Address. Show all posts

Wednesday, August 26, 2015

PowerShell - Gathering IPs from servers

 

Needed to gather all the IPs on a bunch of remote servers.

Here is my script to pull all the IPs from a server and dump each IP to a CSV file with each IP on its own row.

 

##### Set Variables #######

$fileCount = 0

$List = @()

$i = 0

 

#get the list of servers to scan from serverlist.txt

$serverlist = "C:\scripts\computers.txt"

 

 

 

#get the count of servers for updates to the user.

 

$fileCount = (Get-Content $serverlist | Measure-Object).Count

 

 

 

#Loop the server list.

foreach ($server in Get-Content $serverlist) {

 

#increment the number of records proccessed

$i++

 

#Write to the screen whats happening.

Write-Progress -activity "Connecting to server $server" -status "Scanning: $i of $($fileCount)" -percentComplete (($i / $fileCount)  * 100)

 

#do a try if there is an issue spit out the error.

try {

 

    #IF there is an error stop and go to the next record.

    $ErrorActionPreference = 'Stop'

  

    $configs=Get-WmiObject -ComputerName $server win32_networkadapterconfiguration  | select IPaddress, ServiceName, Description | where IPaddress -ne $null

 

    Write-Progress -activity "Scanning server $server" -status "Scanning: $i of $($fileCount)" -percentComplete (($i / $fileCount)  * 100)

 

    #Set the array for the output.

      if($configs.IPaddress.Count -eq 1){

      $List += [pscustomobject]@{

            'ComputerName'     = $server

            'IPaddress'        = $configs.IPaddress.Item(0)

            'Description'      = $configs.Description

            }

      }

      else{

       

        for($thecount=1; $theCount -le $configs.IPaddress.Count){

 

            $List += [pscustomobject]@{

            'ComputerName'     = $server

            'IPaddress'        = $configs.IPaddress.Item($thecount - 1)

            'Description'      = $configs.Description.Item($thecount - 1)

            }

            $thecount++

           

        }  

      }

    }

    catch {

        $List += [pscustomobject]@{

        'ComputerName' = $server

        'IPaddress'    = "Error"

        'ServiceName'  = "Error"

        'Description'  = "Error"

    }

  }

 

}

 

 

write-progress -activity "Completed."

 

$randomString = Get-Date -format M.d.yyyy.HH.mm.ss

 

try{

md "c:\scripts\export\"

}

Catch{

}

 

$csvFileName = 'c:\scripts\export\' + $randomString + '_GetServerIPs.csv'

 

$csvFileName

 

$List  | Export-Csv $csvFileName -noType

 

 

Invoke-Item $csvFileName

 

 

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.