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

How To Hide Featured Image in Blog Posts in Single View

In WordPress, single posts can have a featured image to help with displaying in List…

PowerShell: Find Process ID (PID) Locking a Certain File

Here's a function to kill common processes (Antivirus executable excluded) locking a particular file. This…

Basic CSS: Use an id Attribute to Style an Element

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css"><style>.red-text {color: red;}h2 {font-family: Lobster, monospace;}p {font-size: 16px;font-family: monospace;}.thick-green-border {border-color: green;border-width: 10px;border-style:…