Posted On January 10, 2020

PowerShell: Implementation of Anonymous Self Executing Function

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Implementation of Anonymous Self Executing Function

JavaScript has this. It’s a little known secret that PowerShell can do this as well. Why is this significant? Well, there are several ways this can be incoporated into a program. One trick I’ve learned over the years is that anonymous function can be used in lieu of the infamous try/catch function, and much more. Scan my blog for more examples. Here’s a quick illustration for our current view:

.{
write-host "Statement 1";
$condition=Read-Host -Prompt 'Do you want execute Statement 2? (y/n)'
if (!($condition | Select-String -pattern 'y','yes','Y','Yes')){return}
write-host "Statement 2"; # This statement would only be called if the script has not reached the 'return' command
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Windows 10 Cleanup

Update: there's a new script to cleanup windows here. function cleanWindows{ write-host 'Disabling Windows Media…

WordPress Custom CSS for Tables

WordPress themes can be customized, and one of the common practice is to add CSS…

A Broken PS-Session Issue

Symptoms: PS C:\Windows\system32> get-pssession Id Name ComputerName ComputerType State ConfigurationName Availability -- ---- ------------ ------------…