Skip to content

Commit

Permalink
upload module
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasPSP committed Oct 30, 2019
1 parent 8641040 commit 0651f77
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 1 deletion.
Binary file added PSOneTools/PSOneTools.psd1
Binary file not shown.
126 changes: 126 additions & 0 deletions PSOneTools/Test-PSOnePort.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
function Test-PSOnePort
{
<#
.SYNOPSIS
Tests a network port on a remote computer
.DESCRIPTION
Tests whether a port on a remote computer is responding.
.EXAMPLE
Test-PSOnePort -ComputerName 127.0.0.1 -Port 4000 -Timeout 1000
Tests whether port 4000 on the local computer is responding,
and waits a maximum of 1000 milliseconds
.EXAMPLE
Test-PSOnePort -ComputerName 127.0.0.1 -Port 4000 -Timeout 1000 -Count 30 -Delay 2000
Tests 30 times whether port 4000 on the local computer is responding,
and waits a maximum of 1000 milliseconds inbetween each test
.EXAMPLE
Test-PSOnePort -ComputerName 127.0.0.1 -Port 4000 -Timeout 1000 -Count 0 -Delay 2000 -ExitOnSuccess
Continuously tests whether port 4000 on the local computer is responding,
waits a maximum of 1000 milliseconds inbetween each test,
and exits as soon as the port is responding
.LINK
https://powershell.one/tricks/network/porttest
#>


param
(
[Parameter(Mandatory)]
[string]
$ComputerName,

# port number to test
[Parameter(Mandatory)]
[int]
$Port,

# timeout in milliseconds
[int]
$Timeout = 500,

# number of tries. A value of 0 indicates countinuous testing
[int]
[ValidateRange(0,1000)]
$Count = 1,

# delay (in milliseconds) inbetween continuous tests
$Delay = 2000,

# when enabled, function returns as soon as port is available
[Switch]
$ExitOnSuccess
)
$ok = $false
$c = 0
$isOnline = $false
$continuous = $Count -eq 0 -or $Count -gt 1
try
{
do
{
$c++
if ($c -gt $Count -and !$continuous) {
# count exceeded
break
}
$start = Get-Date

$tcpobject = [system.Net.Sockets.TcpClient]::new()
$connect = $tcpobject.BeginConnect($computername,$port,$null,$null)
$wait = $connect.AsyncWaitHandle.WaitOne($timeout,$false)

if(!$wait) {
# no response from port
$tcpobject.Close()
$tcpobject.Dispose()

Write-Verbose "Port $Port is not responding..."
if ($continuous) { Write-Host '.' -NoNewline }
} else {
try {
# port is reachable
if ($continuous) { Write-Host '!' -NoNewline }
[void]$tcpobject.EndConnect($connect)
$tcpobject.Close()
$tcpobject.Dispose()

$isOnline = $true
if ($ExitOnSuccess)
{
$ok = $true
$delay = 0
}
}
catch {
# access to port restricted
throw "You do not have permission to contact port $Port."
}
}
$stop = Get-Date
$timeUsed = ($stop - $start).TotalMilliseconds
$currentDelay = $Delay - $timeUsed
if ($currentDelay -gt 100)
{
Start-Sleep -Milliseconds $currentDelay
}

} until ($ok)
}
finally
{
# dispose objects to free memory
if ($tcpobject)
{
$tcpobject.Close()
$tcpobject.Dispose()
}
}
if ($continuous) { Write-Host }

return $isOnline
}
7 changes: 7 additions & 0 deletions PSOneTools/init.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# use this file to define global variables on module scope
# or perform other initialization procedures.
# this file will not be touched when new functions are exported to
# this module.


11 changes: 11 additions & 0 deletions PSOneTools/module.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# DO NOT MODIFY THIS FILE!
# THIS FILE WAS AUTOGENERATED BY ISESTEROIDS AND WILL BE OVERWRITTEN WHEN YOU EXPORT FUNCTIONS TO THIS MODULE.

# USE THIS FILE FOR ADDITIONAL MODULE CODE. THIS FILE WILL NOT BE OVERWRITTEN
# WHEN NEW CONTENT IS PUBLISHED TO THIS MODULE:
. $PSScriptRoot\init.ps1


# LOADING ALL FUNCTION DEFINITIONS:

. $PSScriptRoot\Test-PSOnePort.ps1
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# PSOneTools
Home of PSOneTools, an open-source PowerShell module with tids and bits taken from articles published at https://powershell.one
Home of PSOneTools, an open-source PowerShell module with tids and bits taken from articles published at https://powershell.one.

## About
PSOneTools is a **PowerShell** module and contains various useful **PowerShell** commands from a wide range of disciplines. All commands were taken from articles published at https://powershell.one.

That is why the module is a work in progress. The command set grows over time.

The module is licensed under the MIT license so you are completely free to useand expand on the sources. You can use them freely even commercially. However there are no guarantees tied to the module. Use at own risk, and use as-is.

## Contribute
You are most welcome to help improve the code.

## Installation
While you can download the sources directly from here, it is recommended you use the **PowerShell Gallery** to download and install the module:

```powershell
Install-Module -Name PSOneTools -Scope CurrentUser -Force
```

0 comments on commit 0651f77

Please sign in to comment.