none
Powershell advanced function - boolean parameters

    Question

  • With cmdlets, there are often parameters that don't take a value.  They simply indicate an option, for example, -Verbose, or -Recurse.  My question is, how do we implement this in an advanced function?

    I have the following code.  I would like to just specify the 'Printout' parameter, without having to say $true or $false.  Here is my code so far: (it doesn't work as I want, I have to specifiy Print-Something -Printout $true)

     

    function Print-Something {
      
      [CmdletBinding()]
      param
      (
        [Parameter(Mandatory=$True)]
         [bool]$Printout
    		
        )
    
      begin {
      if ($printout) { Write-Host "Got parameter" }
      else {Write-Host "No parameter"}
      }
    }
      
    Print-Something -Printout
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Tuesday, October 04, 2011 12:35 PM
    Tuesday, October 04, 2011 12:34 PM

Answers

All replies