Test-NetworkDevices.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.3.131
     Generated on: 12/29/2016 4:50 PM
     Generated by: Francis Setash
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2016
#>


<#
    ===========================================================================
     Created on: 12/29/2016 2:20 PM
     Created by: Francis Setash
     Organization:
     Filename: Test-NetworkDevices.psm1
    -------------------------------------------------------------------------
     Module Name: Test-NetworkDevices
    ===========================================================================
#>



<#
    .SYNOPSIS
        This function will check DNS resolution (IPv4) and ping connectivity for a list of hosts in a csv file.
     
    .DESCRIPTION
        The provided csv file will be iterated through. DNS resolution will be checked, and if successful, a ping will be attempted. Please note, the source CSV file must have a header named "Hostname"
     
    .PARAMETER CsvPath
        The source CSV file. Please note, it MUST have a header named "Hostname"
     
    .PARAMETER OutPath
        The destination CSV file. If a path is not provided it will be placed in the present working directory
     
    .EXAMPLE
        PS C:\> Test-ConnectionFromCsv -CsvPath 'C:\path\to\file.csv' -OutPath 'file.csv'
     
#>

function Test-ConnectionFromCsv
{
    param
    (
        [Parameter(Mandatory = $true)]
        [string]$CsvPath,
        [Parameter(Mandatory = $true)]
        [string]$OutPath
    )
    

    $resultList = New-Object -TypeName System.Collections.ArrayList
    
    $serverList = Import-Csv $CsvPath
    
    
    foreach ($server in $serverList)
    {
        try
        {
            $dns = $(Resolve-DnsName $server.Hostname -QuickTimeout -Type A -ErrorAction Stop).IPAddress
            $pingResult = Test-Connection -Quiet $server.Hostname
        }
        Catch
        {
            $dns = "DNS Failure"
        }
        
        $results = @{
            Hostname = $($server.Hostname)
            pingResult = $pingResult
            DNS = $dns
        }
        
        $resultObj = New-Object -TypeName System.Management.Automation.PSObject -Property $results
        $resultObj | Format-Table
        $resultList.Add($resultObj) | Out-Null
    }
    
    $resultList | Format-Table
    $resultList | Select-object -property Hostname, pingResult, DNS | Export-Csv $OutPath -Force -NoTypeInformation
}


<#
    .SYNOPSIS
        This takes a CSV file and will generate an output CSV with DNS resolution for both IPv4 and IPv6 based on the hostnames provided.
     
    .DESCRIPTION
        This function will specifically take an input CSV file and check DNS resolution for both items; including IPv4 and IPv6. It will be placed in an output CSV file.
     
    .PARAMETER CsvPath
        The source CSV file. Please note, it MUST have a header named "Hostname"
     
    .PARAMETER OutPath
        The destination CSV file. If a path is not provided it will be placed in the present working directory
     
    .EXAMPLE
        PS C:\> Resolve-DnsFromCsv -CsvPath 'C:\path\to\file.csv' -OutPath 'test.csv'
     
#>

function Resolve-DnsFromCsv
{
    param
    (
        [Parameter(Mandatory = $true)]
        [string]$CsvPath,
        [Parameter(Mandatory = $true)]
        [string]$OutPath
    )
    
    $resultList = New-Object -TypeName System.Collections.ArrayList
    
    $serverList = Import-Csv $CsvPath
    
    
    foreach ($server in $serverList)
    {
        try
        {
            $dns = $(Resolve-DnsName $server.Hostname -QuickTimeout -Type A -ErrorAction Stop).IPAddress
            $results = @{
                Hostname = $($server.Hostname)
                IPv4 = $($dns.IP4Address)
                IPv6 = $($dns.IP6Address)
            }
        }
        Catch
        {
            $dns = "DNS Failure"
            $results = @{
                Hostname = $($server.Hostname)
                IPv4 = $dns
                IPv6 = $dns
            }
        }
        
        
        $resultObj = New-Object -TypeName System.Management.Automation.PSObject -Property $results
        $resultObj | Format-Table
        $resultList.Add($resultObj) | Out-Null
    }
    
    $resultList | Format-Table
    $resultList | Select-object -property Hostname, IPv4, IPv6 | Export-Csv $OutPath -Force -NoTypeInformation
}


#Export-ModuleMember -Function Test-ConnectionFromCsv, Resolve-DnsFromCsv