Using a script with a switch parameter to use a parameter in a cmdlet

Welcome Forums General PowerShell Q&A Using a script with a switch parameter to use a parameter in a cmdlet

This topic contains 0 replies, has 1 voice, and was last updated by  KLaage 3 months, 2 weeks ago.

  • Author
    Posts
  • #144731

    Participant
    Topics: 6
    Replies: 10
    Points: 111
    Rank: Participant

    I'm testing some things and I'm stumped on this.

    I've got a script where I wish to toggle whether or not to show table headers in the final output. I've set up a switch parameter $HideHeaders, and it should simply toggle headers by calling Format-Table with the -HideTableHeaders parameter.

    But instead of expanding to a parameter, Powershell treats it as a positional parameter and gives the following error: "Format-Table : A positional parameter cannot be found that accepts argument '-HideTableHeaders'."
    Also by doing the '$noHeaders = $null' I thougth that should just put nothing instead $noHeaders but that gives me: "Format-Table : A positional parameter cannot be found that accepts argument '$null'.", and '$noHeaders = ""' gives: "Format-Table : A positional parameter cannot be found that accepts argument "."

    function Get-DHCPScopeInfo{
    param(
    $scope = @(10,11,12,13,16,50,100),
    $ComputerName = "*",
    $sort = "hostname",
    $Property = "IPAddress,hostname,clientid",
    [switch]$HideHeaders
    )
    
    if($HideHeaders.IsPresent) {
    $noHeaders = "-HideTableHeaders"
    #$noHeaders = "-HideTableHeaders:$true"
    }
    else {
    $noHeaders = $null
    #$noHeaders = ""
    #$noHeaders = "-HideTableHeaders:$false"
    }
    Get-DhcpServerv4Lease -ComputerName [SERVERNAME] -scopeid [IP SCOPE] `
    | Sort-Object -Property $sort | Where-Object hostname -like "*$ComputerName*" | Format-Table -Property $property.Split(",") $noHeaders
    }
    
    Get-DHCPScopeInfo -ComputerName 8700 -Property hostname
    Get-DHCPScopeInfo -ComputerName 8700 -Property hostname -HideHeaders
    

    If i work around the issue by rewriting the switch test to include the full command in both IF (including the -HideTableHeaders) and ELSE (excluding the -HideTableHeaders) statements it works. Like this:

    function Get-DHCPScopeInfoVerbose {
    param(
    $scope = @(10,11,12,13,16,50,100),
    $ComputerName = "*",
    $sort = "hostname",
    $Property = "IPAddress,hostname,clientid",
    [switch]$HideHeaders
    )
    
    if($HideHeaders.IsPresent) {
    Get-DhcpServerv4Lease -ComputerName [SERVERNAME] -scopeid [IP SCOPE] `
    | Sort-Object -Property $sort | Where-Object hostname -like "*$ComputerName*" | Format-Table -Property $property.Split(",") -HideTableHeaders
    }
    else {
    Get-DhcpServerv4Lease -ComputerName [SERVERNAME] -scopeid [IP SCOPE] `
    | Sort-Object -Property $sort | Where-Object hostname -like "*$ComputerName*" | Format-Table -Property $property.Split(",")
    }
    }
    
    Get-DHCPScopeInfoVerbose -ComputerName 8700 -Property hostname
    Get-DHCPScopeInfoVerbose -ComputerName 8700 -Property hostname -HideHeaders
    

    It seems clumsy, however, and if I need to change things about in the command being called I need to remember to fix everything twice.

The topic ‘Using a script with a switch parameter to use a parameter in a cmdlet’ is closed to new replies.