Wednesday, March 22, 2017

Put a function in your Microsoft.PowerShell_profile.ps1 file at the WindowsPowerShell folder!

In this locale you may roll something like this:

#This is a function in a PowerShell .ps1 file!
function Do-Something {
[CmdletBinding()]
param(
   [switch]$foo,
   [switch]$bar,
   [switch]$baz,
   [String]$qux=""
)
PROCESS {
      Write-Host "doing the something..." -foreground yellow
      if ($baz) {
         $foo = $true;
         $bar = $true;
      }
      if ($foo) {
         Write-Host "either foo or baz was flagged" -foreground yellow
      }
      if ($bar) {
         Write-Host "either bar or baz was flagged" -foreground yellow
      }
      if ($qux.length -gt 0) {
         Write-Host $qux "was handed in for qux" -foreground yellow
      }
   }
}

 
 

From the PowerShell command line the function could be called in the following legitimate ways (amongst others) and you can probably guess what each does.

  • do-something
  • do-something -foo
  • do-something -bar
  • do-something -baz
  • do-something -qux "whatever"
  • do-something -foo -qux "whatever"

 
 

Every time you change the script you must close and reopen the PowerShell console to see the newness.

No comments:

Post a Comment