Advertisement
Neptune443

The ? C,C++,C# operator as a function in PS

Jan 28th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Test-Ternary  {
  2.     param(        
  3.         [parameter(Mandatory,ValueFromPipeline)]
  4.         $Condition,
  5.         [parameter(Mandatory,Position=1)]
  6.         $IsTrue,
  7.         [parameter(Mandatory,Position=2)]
  8.         $IsFalse
  9.     )
  10.     $test = $Condition
  11.     if($Condition -is [scriptblock]){
  12.         $test = &$Condition
  13.     }
  14.     if($test){
  15.         if($IsTrue -is [scriptblock]){
  16.             &$IsTrue
  17.         } else {
  18.             $IsTrue
  19.         }
  20.     } else {
  21.         if($IsFalse -is [scriptblock]){
  22.             &$IsFalse
  23.         } else {
  24.             $IsFalse
  25.         }
  26.     }
  27. }
  28. #Get-Process -Name notepad -ErrorAction SilentlyContinue | ?? "Notepad started" "Notepad not started"
  29. Set-Alias ?? Test-Ternary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement