2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator

PowerShell Test Connections

   
<#  
    ScriptsbyScott.com

    .SYNOPSIS
        Test Connection to Each Computer, List in File
    
    .DESCRIPTION
        Test Conn computers and separates results to file
    
    .NOTES  
        Author: Scott Head
        Min PSVer: Powershell 2.0
        Version: 1.0 (7/7/2019) Script Created     
#>
        

$COMPS= Get-Content C:\Temp\Computers.txt

foreach ($COMP in $COMPS) {
        
    Try{
        $IP=test-connection $COMP -Count 1 -ErrorAction Stop | select -ExpandProperty IPv4address
        
        $COMP + "," + $IP | out-file C:\Temp\Comp_Found.txt -Append
        write-Output "$COMP , $IP"
    }
    Catch
    {
        $COMP + ", NotFound" | out-file C:\Temp\Comp_NotFound.txt -Append
        write-Output "$COMP , Not Found"
        
    }

----------------------Test URL Connection-------------------------------

   
    Import-Csv C:\Temp\URL_Review.csv |`

    ForEach-Object {

    $URL=$_.URL.Trim()

    $Name=$_.Name.Trim()

        Try{
            
            $StatusCode=Invoke-webrequest $URL -DisableKeepAlive -UseBasicParsing -Method head -TimeoutSec 3 | Select -ExpandProperty StatusCode                                           
            write-host " $Name OK"

                if ($StatusCode -ne 200){
                    
                        Write-host "$Name URL Issue: $URL"
                    

                        #----------------------------
                        # Configure Mail Variables 
                        #---------------------------

                        $SMTPServer = "MailServer"
                        $SMTPFrom = "URLCheck@YourDomain.com"
                        $SMTPTo = "YourVerizonCell@vtext.com"
                        $SMTPSubject = " $Name URL Check Issue: $URL"
                        $SMTPBody = "$Name URL Issue: $URL $_.Exception.Message"

                        #----------------------------------------------
                        #Send Mail and Attach Custom Named Log File
                        #---------------------------------------------

                        Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody
                }

            }Catch{                                   
                         write-host "$Name URL Issue: $URL $_.Exception.Message"
                    
       
                #----------------------------
                        # Configure Mail Variables 
                        #---------------------------

                        $SMTPServer = "MailServer"
                        $SMTPFrom = "URLCheck@YourDomain.com"
                        $SMTPTo = "YourVerizonCell@vtext.com"
                        $SMTPSubject = " $Name URL Check Issue: $URL"
                        $SMTPBody = "$Name URL Issue: $URL $_.Exception.Message"

                        #----------------------------------------------
                        #Send Mail and Attach Custom Named Log File
                        #---------------------------------------------

                        Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody

                            }

                        }
                        $URL=""
                        $Name=""
                        $StatusCode=""
                        $SMTPServer = ""
                        $SMTPFrom = ""
                        $SMTPTo = ""
                        $SMTPSubject = ""
                        $SMTPBody = ""


<# 

ScriptsByScott.com

.SYNOPSIS     
    Check to see if folder is present or Registery path
.DESCRIPTION 
    Sometimes need to check to see if something exists for error traping
.EXAMPLE  
    Type in the fill folder path or registry path at prompt 
    Value returned is True or False 
#>   

# Test Path to A Folder
Test-Path C:\temp 

# Test Path in Registry 
Test-Path HKLM:\HARDWARE\DESCRIPTION\System

# Test access to network share
Test-Path \\POWERSHELL-PC\Temp

If(Test-Path \\POWERSHELL-PC\Temp){

    Write-Output "Path Exists"

}Else{

    Write-Output "No Path Found"

}