Chapters/adding-cmdletbinding-and-parameterizing/snippets.ps1

# basic function model
function test {
    Param(
    [string]$ComputerName
    )
}
help test



# again:
function test {
    [CmdletBinding()]
    Param(
        [string]$ComputerName
    )
}
test



# starting with...
function Get-MachineInfo {
    Param(
        [string[]]$ComputerName,
        [string]$LogFailuresToPath,
        [string]$Protocol = "Wsman",
        [switch]$ProtocolFallback
    )
 
    foreach ($computer in $computername) {
 
        # Establish session protocol
        if ($protocol -eq 'Dcom') {
            $option = New-CimSessionOption -Protocol Dcom
        } else {
            $option = New-CimSessionOption -Protocol Wsman
        }
 
        # Connect session
        $session = New-CimSession -ComputerName $computer `
                                  -SessionOption $option
  
        # Query data
        $os = Get-CimInstance -ClassName Win32_OperatingSystem `
                              -CimSession $session
  
        # Close session
        $session | Remove-CimSession
  
        # Output data
        $os | Select-Object -Prop @{n='ComputerName';e={$computer}},
                                Version,ServicePackMajorVersion
 
    } #foreach
 
} #function



# moving to...
function Get-MachineInfo {
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline=$True)]
        [string[]]$ComputerName,
        
        [string]$LogFailuresToPath,

        [string]$Protocol = "Wsman",

        [switch]$ProtocolFallback
    )
 
 BEGIN {}

 PROCESS {
    foreach ($computer in $computername) {
 
        # Establish session protocol
        if ($protocol -eq 'Dcom') {
            $option = New-CimSessionOption -Protocol Dcom
        } else {
            $option = New-CimSessionOption -Protocol Wsman
        }
 
        # Connect session
        $session = New-CimSession -ComputerName $computer `
                                  -SessionOption $option
  
        # Query data
        $os = Get-CimInstance -ClassName Win32_OperatingSystem `
                              -CimSession $session
  
        # Close session
        $session | Remove-CimSession
  
        # Output data
        $os | Select-Object -Prop @{n='ComputerName';e={$computer}},
                                Version,ServicePackMajorVersion
 
    } #foreach
} #PROCESS

END {}

} #function



# imagine this run pattern:
Get-MachineInfo -ComputerName ONE,TWO,THREE

# versus:
"ONE","TWO","THREE" | Get-MachineInfo



# consider doing this
End {
 # intentionally empty
}



# parameter decorator
[Parameter(ValueFromPipeline=$True)]


# adding ByPropertyName
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]



# marking mandatory
    Param(
        [Parameter(ValueFromPipeline=$True,
                   Mandatory=$True)]
        [string[]]$ComputerName,
        
        [string]$LogFailuresToPath,

        [string]$Protocol = "Wsman",

        [switch]$ProtocolFallback
    )



# adding validation
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline=$True,
                   Mandatory=$True)]
        [string[]]$ComputerName,
        
        [string]$LogFailuresToPath,

        [ValidateSet('Wsman','Dcom')]
        [string]$Protocol = "Wsman",

        [switch]$ProtocolFallback
    )



# aliases (more on these later)
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline=$True,
                   Mandatory=$True)]
        [Alias('CN','MachineName','Name')]
        [string[]]$ComputerName,
        
        [string]$LogFailuresToPath,

        [ValidateSet('Wsman','Dcom')]
        [string]$Protocol = "Wsman",

        [switch]$ProtocolFallback
    )