Overriding Out-Default (Part 1)

Out-Default is a hidden PowerShell cmdlet that gets called at the end of each command, and outputs results to the console. You can override this function with your own. If you overwrite it with an empty function, for example, all output is discarded – or results in a “SECRET!” message:

function Out-Default
{
    'SECRET!'
}

This is how you remove custom overrides:

 
PS C:> del function:Out-Default
 

Twitter This Tip! ReTweet this Tip!

Use Out-GridView as Output Window

Typically, Out-GridView opens a window and displays whatever you pipe into the cmdlet:

 
PS C:> Get-Service | Out-GridView 
 

However, with a little trick Out-GridView becomes even more versatile. You can pipe information to the same output window whenever you want to.

First, get yourself an instance of Out-GridView that thinks it is running in a pipeline:

$pipeline = { Out-GridView }.GetSteppablePipeline()
$pipeline.Begin($true)

Now, you can output whatever you want by calling “Process”. Each call to Process() is like piping an element to the cmdlet:

$pipeline.Process('Hello this is awesome!')
Start-Sleep -Seconds 4
$pipeline.Process('You can output any time...')

When you are done, call End() to end the pipeline:

$pipeline.End()

This way, you could log information to a grid view or use it as a generic output window that shows results to the user.


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!

Beer Challenge Results: Shortest Code for Password Analysis

At psconf.eu there was recently a challenge for the shortest code to check for how often a password was previously pwnd (hacked). Here is the result (credits to Daniel Rothgänger):

[Net.ServicePointManager]::SecurityProtocol='Tls12'
'[email protected]'|sc p -N;$a,$b=(FileHash p -A SHA1|% h*)-split'(?<=^.{5})';((irm api.pwnedpasswords.com/range/$a)-split"$b`:(d+)")[1]  

You can either use this chunk of code as brain jogging to find out what it does, or simply use it: it takes a password (i.e. “[email protected]” in our example) and returns a number. The number is how often this particular password has been seen in previous attacks. Any password that has been seen is considered insecure. Only passwords that do not return a number are safe.


Twitter This Tip! ReTweet this Tip!

Geocoding: Sentiment Analysis (Part 5)

Some geocoding APIs provide sophisticated sentiment analysis like in the example below:

"Most important museums of Amsterdam are located on the Museumplein, located at the southwestern side of the Rijksmuseum." |
  ForEach-Object -Begin {$url='https://geocode.xyz'
    $null = Invoke-RestMethod $url -S session
  } -Process {
    Invoke-RestMethod $url -W $session -Method Post -Body @{scantext=$_;geoit='json';sentiment='analysis'}
  }

A text is sent to the API, and the API analyzes geographic content and outputs details for the locations found in the text:

 
sentimentanalysis : @{allsentiments=; sentimentwords=; mainsentiment=}
longt             : 4.88702
matches           : 3
match             : {@{longt=4.88355; location=RIJKSMUSEUM, AMSTERDAM, NL; matchtype=street; 
                    confidence=1.0; MentionIndices=108,26; latt=52.35976}, @{longt=4.88334; 
                    location=MUSEUMPLEIN, AMSTERDAM, NL; matchtype=street; confidence=1.0; 
                    MentionIndices=55,26; latt=52.35747}, @{longt=4.89416; location=Amsterdam,NL; 
                    matchtype=locality; confidence=0.4; MentionIndices=26; latt=52.36105}}
latt              : 52.35943
 

Twitter This Tip! ReTweet this Tip!