Scanning Ports

Here is a straightforward way to test ports on a local or remote system. You can even specify a timeout (in milliseconds):

function Get-PortInfo
{
    param
    (
        [Parameter(Mandatory)]
        [Int]
        $Port,
    
        [Parameter(Mandatory)]
        [Int]
        $TimeoutMilliseconds,
        
        [String]
        $ComputerName = $env:COMPUTERNAME
    )
  
    # try and establish a connection to port async
    $tcpobject = New-Object System.Net.Sockets.TcpClient 
    $connect = $tcpobject.BeginConnect($computername,$port,$null,$null)
    
    # wait for the connection no longer than $timeoutMilliseconds 
    $wait = $connect.AsyncWaitHandle.WaitOne($timeoutMilliseconds,$false) 
    
    # return rich information
    $result = @{
        ComputerName = $ComputerName
    }
    
    if(!$wait) { 
        # timeout
        $tcpobject.Close() 
        $result.Online = $false
        $result.Error = 'Timeout'
    } else { 
        try { 
            # try and complete the connection
            $null = $tcpobject.EndConnect($connect)
            $result.Online = $true
        }
        catch { 
            $result.Online = $false
        } 
        $tcpobject.Close() 
    } 
    $tcpobject.Dispose()
    
    [PSCustomObject]$result
} 

Scanning ports is now pretty easy:

 
PS> Get-PortInfo -Port 139 -TimeoutMilliseconds 1000

ComputerName    Online
------------    ------
DESKTOP-7AAMJLF   True
 

PS> Get-PortInfo -Port 139 -TimeoutMilliseconds 1000 -ComputerName storage2

Error   ComputerName Online
-----   ------------ ------
Timeout storage2      False 
 

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Resetting Console Colors

It can be easy to mess up console colors in the PowerShell console. A single call with accidental values, or a script that messed with the colors, can lead to unexpected results. Check for yourself, open a PowerShell console (not an editor!), and run this:

 
PS> [Console]::BackgroundColor = "Green"   
 

To quickly clean up colors, run this:

 
PS> [Console]::ResetColor() 
 

Run Clear-Host afterwards to clean the existing display.


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Solving SSL Connection Problems

Sometimes when you try to access web services (or download internet content in general), PowerShell may bail out and complain about not being able to set up a protected SSL channel.

Let’s take a look at such an issue. Below is a code that is calling a web service connected to the German railway system. It is supposed to list the train stations in a given city:

$city = "Hannover"
$url = "https://rit.bahn.de/webservices/rvabuchung?WSDL"

$object = New-WebServiceProxy -Uri $url -Class db -Namespace test

$request = [test.SucheBahnhoefeAnfrage]::new() 
$request.bahnhofsname = $city
$response = $object.sucheBahnhoefe($request)
$response.bahnhoefe

Instead, it throws a number of exceptions related to SSL connection problems. Typically, these errors stem from two issues:

  • The SSL certificate used by the web site to establish the SSL connection is invalid, expired, or not trustworthy
  • The SSL connection requires a protocol currently not enabled

To solve problems associated with these issues, simply run these lines:

# ignore invalid SSL certificates
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

# allowing SSL protocols
$AllProtocols = [Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[Net.ServicePointManager]::SecurityProtocol = $AllProtocols

Once you do, for the current PowerShell session all is good. The above code now returns the train stations in the Hannover area, and if you dig deeper into the rich object model, you could even order your train tickets using PowerShell.

 
name                  nummer
----                  ------
Hannover Hbf         8000152
Hannover-Linden/Fi.  8002586
Hannover-Nordstadt   8002576
Hannover Bismarckstr 8002580
Hannover-Ledeburg    8002583
Hannover-Vinnhorst   8006089
Hannover-Leinhausen  8002585
Hannover Wiech-Allee 8002591
Hannover Ander.Misb. 8000578
Hannover Flughafen   8002589
Hannover-Kleefeld    8002584
Hannover-Bornum      8002581
Hann Münden          8006707
HannoverMesseLaatzen 8003487 
 

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Deleting the Oldest Log File

If you’re writing log activity to files, you may want to clean up things, so maybe you’d like to always delete the oldest log file when you add a new one.

Here is a simple approach:

# this is the folder keeping the log files
$LogFileDir = "c:myLogFiles"

# find all log files...
Get-ChildItem -Path $LogFileDir -Filter *.log |
  # sort by last change ascending
  # (oldest first)...
  Sort-Object -Property LastWriteTime |
  # take the first (oldest) one
  Select-Object -First 1 | 
  # remove it (remove -whatif to actually delete)
  Remove-Item -WhatIf

If you’d like to keep only the latest 5 files, change like this:

# this is the folder keeping the log files
$LogFileDir = "c:myLogFiles"
$Keep = 5

# find all log files...
$files = @(Get-ChildItem -Path $LogFileDir -Filter *.log)
$NumberToDelete = $files.Count - $Keep

if ($NumberToDelete -gt 0)
{
    $files |
      # sort by last change ascending
      # (oldest first)...
      Sort-Object -Property LastWriteTime |
      # take the first (oldest) one
      Select-Object -First $NumberToDelete | 
      # remove it (remove -whatif to actually delete)
      Remove-Item -WhatIf
}

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Enable Real-Time Streaming with Foreach

Classic foreach loops are the fastest loop available but they come with a severe limitation. Foreach loops do not support streaming. You need to always wait for the entire foreach loop to finish before you can start processing the results.

Here are some example illustrating this. With the code below, you have to wait a long time until you “see” the results:

$result = foreach ($item in $elements)
{
    "processing $item"
    # simulate some work and delay
    Start-Sleep -Milliseconds 50
}

$result | Out-GridView 

You cannot pipe the results directly. The following code produces a syntax error:

$elements = 1..100

Foreach ($item in $elements)
{
    "processing $item"
    # simulate some work and delay
    Start-Sleep -Milliseconds 50
} | Out-GridView 

You could use $() to enable piping, but you would still have to wait for the loop to finish before the results are piped in one big chunk:

$elements = 1..100

$(foreach ($item in $elements)
{
    "processing $item"
    # simulate some work and delay
    Start-Sleep -Milliseconds 50
}) | Out-GridView 

Here is a little-known trick that adds real-time streaming to foreach: simply use a script block!

$elements = 1..100

& { foreach ($item in $elements)
{
    "processing $item"
    # simulate some work and delay
    Start-Sleep -Milliseconds 50
}} | Out-GridView 

Now you “see” the results as they are produced, and enjoy real-time streaming.

Of course, you could as well abandon foreach in the first place, and use the ForEach-Object pipeline cmdlet instead:

$elements = 1..100

$elements | ForEach-Object {
    $item = $_
    
    "processing $item"
    # simulate some work and delay
    Start-Sleep -Milliseconds 50
} | Out-GridView

However, ForEach-Object is much slower than the foreach keyword, and there are cases where you simply can’t use ForEach-Object. For example, in a lot of database code, your code needs to check for end-of-file flags and reads records one by one which exclude ForEach-Object as an option.


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!