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 Automated Disk Cleanup

##################################################################################     <#      This script is created to automate the cleanup activity. Doing so will benefit to reduce the size of disk.     This script will perform the following   1. Clear windows temp and user temp folder   2. Empty recycle bin   3. Disk Cleanup   4. Clear CBS cabinet log files   5. Clear downloaded patches   6. Clear downloaded driver   7. Clean download folder      Note:  …

Use PowerShell to Get GeoLocation of a Computer’s Public IP

Method 1: Invoke-RestMethod -Uri ('http://ipinfo.io/'+(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content) Method 2: function getIPGeolocation($ipAddress) {$request = Invoke-RestMethod -Method…

Disable and Enable Trace Logging for Dynamics CRM

# Set common variables $serverTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRM' # Enable CRM Tracing Add-PSSnapin Microsoft.Crm.PowerShell $setting = Get-CrmSetting TraceSettings…