Telnet alternative to check if port is open on host

Many people still use telnet (client) for checking if port is open on remote host.
The Powershell cmdlet Test-NetConnection is the new and improved tool for this and by creating a function alias in your profile settings you can have it easily available at any time without remembering the syntax.

1) Open Powershell profile
notepad $profile

2) Add script
function open([string]$arg1, [int]$arg2)
{
     Test-NetConnection -ComputerName $arg1 -Port $arg2 -InformationLevel Detailed
}

3) Save file and close/reopen the Powershell window

4) Test it in your powershell prompt by writing open google.com 80

Powershell Snippets

List local user accounts

Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'"


List local user accounts and parse array

$local_users = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | findstr "Caption";
for ($i = 0; $i -lt $local_users.Count; $i++) {
    Write-Host $local_users[$i].split("\\")[1];
}


List all installed Windows updates

Get-Hotfix-computername <hostname>Select HotfixID, InstalledOn |Sort-Object InstalledOn
Output will look something like:

tail equivalent in Powershell

Get-Content filename.txt -Tail 30

List all AD groups for user

Save the following code to a script called getgroups.ps1:

Param(   
    [Parameter(Mandatory=$false)][string]$username 
) 
(New-Object System.DirectoryServices.DirectorySearcher(
    "(&(objectCategory=User)(samAccountName=$($username)))"
 )).FindOne().GetDirectoryEntry().memberOf

Run the script from powershell and enter username as parameter, like this:

.\getgroups.ps1 johndoe

Create a daemon for Linux

# !/bin/sh
# /etc/init.d/daemond

### BEGIN INIT INFO
# Provides:             daemond
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Skeleton daemon
# Description:          Skeleton daemon
### END INIT INFO

case "$1" in
    start)
        echo 'hello world'
        ;;
    stop)
        killall daemond -q
        ;;
    *)
      echo "Usage: /etc/init.d/checkconnectiond {start|stop}"
      exit 1
      ;;
esac

exit 0

Simple Robocopy backup script

Robocopy syntax to copy all NEW or EDITED files from source to destination dir:

robocopy "%src%" "%dest%" /E /W:1 /R:1 /XC /log+:"robocopy_log.txt"

Description of the parameters:

  • /E = Copies subdirectories. Note that this option includes empty directories. If you wish to exclude empty directories, use /S.
  • /W:1 = Specifies the wait time between retries, in seconds. The default value of N is 30 (wait time 30 seconds).
  • /R:1 = Specifies the number of retries on failed copies. The default value of N is 1,000,000 (one million retries).
  • /XC = Excludes changed files.
  • /log+: = Writes the status output to the log file (appends the output to the existing log file).

Windows batch script to setup a scheduled task to run the robocopy command:

@echo off
title Robocopy Backup
echo.
echo Initial configuration for automatic backup with Robocopy + Task Scheduler
echo.

echo Step 1 - setup Robocopy:
echo.
set /p src=Backup FROM dir: 
set /p dest=Backup TO dir: 
(
echo cd %userprofile%\Desktop
echo robocopy "%src%" "%dest%" /E /W:1 /R:1 /XC /log+:"robocopy_log.txt"
)> %userprofile%\robocopy.bat

echo.
echo Step 2 - setup Task Scheduler:
echo.
schtasks /create /tn "Robocopy Backup" /tr "%userprofile%\robocopy.bat" /SC HOURLY
echo.
echo Done
echo.

pause

Copy & paste the text into a text editor and save it as a .bat file to make it executable.

When running this script you will setup a scheduled task which runs the robocopy.bat file every hour. Really great and time-saving if you work in a local folder on your C drive and wish to periodically take incremental backups to a network storage for instance.