top of page

PowerShell  | Connection Tests

WinRM Access Check

Invoke-Command: This cmdlet is used to run commands on remote computers using Windows Remote Management (WinRM). It can be employed to test WinRM access by attempting to execute a simple command on a remote machine. For example, using Invoke-Command -ComputerName <RemoteComputerName> -ScriptBlock { Get-Process } can verify if WinRM is properly configured and accessible. If the command executes successfully and returns the expected results, it indicates that WinRM access is functional.

# PowerShell Script Snippet - Simple way to Collect list of WINRM accessible systems

$ListofComputers = Get-Content C:\temp\Servers.txt

$ComputersAccess=Invoke-Command -ComputerName $ListofComputers -ErrorAction SilentlyContinue -ScriptBlock {$env:COMPUTERNAME}

$ComputersAccess

WinRM Access Check (Alternative Test)

# Get list of enabled computers from Active Directory
$ComputerList= Get-Adcomputer -filter * | ?{$_.Enabled -eq $True} | Select -ExpandProperty Name
# Command to Check for Systems online that respond to WinRM
$ComputersConnected=Invoke-Command -ComputerName $ComputerList -ScriptBlock {$Env:COMPUTERNAME}
# Compare full list to see if any did not respond
$Tested=Compare-Object -ReferenceObject $ComputerList -DifferenceObject $ComputersConnected -IncludeEqual
# Filter to get systems that were accessible
$TestPassConn=$Tested | ?{$_.SideIndicator -eq "=="} | Select -ExpandProperty InputObject
# Filter to get systems that are NOT accessible
$TestFailConn=$Tested | ?{$_.SideIndicator -eq "<="} | Select -ExpandProperty InputObject
# Results Out to screen and file
Write-Host '--Failed--'
$TestFailConn | Tee-Object C:\temp\TestFailedWInRM.txt 

# Results Out to screen and file
Write-Host '--Passed--'
$TestPassConn | Tee-Object C:\temp\TestPassWinRM.txt 

URL Website Test

Invoke-WebRequest: This cmdlet allows for sending HTTP and HTTPS requests to web servers and retrieving the responses. It can be used to check the availability and performance of web resources, fetch web content, and even interact with REST APIs. The response includes details such as status code, headers, and content, providing comprehensive information about the web resource's accessibility and health.

# PowerShell Script Snippet - Test Path to Website

$URL="Http://www.Google.com"
$StatusCode=Invoke-webrequest $URL -DisableKeepAlive -UseBasicParsing -Method head -TimeoutSec 3 | Select -ExpandProperty StatusCode        

if ($StatusCode -eq 200){

    write-Host "Website is Online"

}Else{

    Write-Host "The Website is Down"

}

Test Path to Folder

Test-Path: This cmdlet is used to check the existence of a file or directory on a local or networked file system. It returns a boolean value indicating whether the specified path exists. This cmdlet is valuable for verifying that necessary files and directories are available before attempting to perform operations on them.

# PowerShell Script Snippet - Test Path to A Folder
Test-Path C:\temp 

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

# PowerShell Script Snippet - Test Path  to Network Share
Test-Path \\POWERSHELL-PC\Temp

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

    Write-Output "Path Exists"

}Else{

    Write-Output "No Path Found"

}

Looping Ping Test

Test-Connection: This cmdlet functions similarly to the traditional ping command. It sends ICMP echo request packets to a specified target (such as a hostname or IP address) and returns information about the network response, including the response time and packet loss. This is useful for verifying whether a remote machine is reachable over the network.

# PowerShell Script Snippet - Loop and PING test tab delimited output to file
$COMPS=Get-Content C:\temp\MyList.txt

foreach ($COMP in $COMPS){

    Try{
        $IP=test-connection $COMP -Count 1 -ErrorAction Stop | select -ExpandProperty IPv4address        
        $COMP + "`t" + $IP |Tee-Object C:\Temp\Computer_Checker.txt -Append       
    }Catch{

        $COMP + "`tNotFound" | Tee-Object C:\Temp\Computer_Checker.txt -Append
    }
}

Network Connection Test & Port Query

Test-NetConnection: This cmdlet provides a more comprehensive network diagnostic tool compared to Test-Connection. It can test connectivity to a specified port or remote host, perform DNS lookups, and return detailed information about network interfaces and route tracing. It supports various protocols and can be used to diagnose more complex network issues, such as verifying if a specific service on a remote server is reachable.

# PowerShell Script Snippet - Sets the location and port to scan

# Reports back detailed network information.   

Test-NetConnection imap.gmail.com -Port 993 -InformationLevel "Detailed"
Test-NetConnection
smtp.gmail.com -Port 465 -InformationLevel "Detailed"

Test-netConnection.PNG
bottom of page