Combining elements for Output

I have a script that outputs an array to the monitor. My coworkers are asking for the option to route the output to either gridview or csv. Currently I have written messy if statement blocks to handle the output.

If ($listonlya){
$svrs = Get-Process a
}
If($OutCSV){
$svrs|Export-CSV “Servers.csv”}
If ($gridview){
$svrs|out-Gridview}
If($OutFile){
$svrs|out-file “servers.txt”

$testone = get-content “testfile.txt”

If($OutCSV){
	$testone |Export-CSV “Servers.csv”}
If ($gridview){
	$testone |out-Gridview}
If($OutFile){
	$testone |out-file “servers.txt” 

My idea is to use my output type as a variable as in my example below.

If ($gridview){
$output = ‘Out-Gridview’}
IF ($OutCSV){
$output = ‘Export-CSV servers.csv’}
If ($OutFile){
$output = ‘Out-File Servers.txt’}
If (normal){
$output = ‘Ft -Auto’}

$svrs |$output
$testone | $output

I have not been able to find a method for using a variable to control the output of the script and am wondering if it is possible.

Thanks

Check out advanced function parameters like ValidateSet.

function Get-This {
    Param( 
            [ValidateSet("List","Grid","CSV")] 
            [String] 
            $As = "List"
    )


    $result = Get-Process

    switch ($As) {
        "List"{$result | Format-List}
        "Grid"{$result | Out-GridView}
        "CSV"{$result | Out-CSV "C:\result.csv"}
    }
}

Get-This -As Grid

If you want to have the path of the CSV as a parameter, you will need to look at dynamic parameters (DynamicParam), because if the $As is CSV, then the path would be mandatory.

I am not a big fan of dynamic parameters simply because they do not appear on Get-Help. If you expect anyone to ever potentially look at what the usage of your function is, dynamic parameters are not your friend.

Another solution (and easier to implement than dynamic parameters) is to user parameter sets, this would correctly show up on Get-Help.

Here’s an example, using the same code from Rob:

function Get-This
{
    [CmdletBinding(DefaultParameterSetName = 'List')]
    Param
    (
        [parameter(ParameterSetName = 'List', Mandatory = $true)]
        [switch]$AsList,
        
        [parameter(ParameterSetName = 'Grid', Mandatory = $true)]
        [switch]$AsGrid,
        
        [parameter(ParameterSetName = 'CSV', Mandatory = $true)]
        [switch]$AsCSV,
        
        [parameter(ParameterSetName = 'CSV', Mandatory = $true)]
        [string]$Path
    )
        
    $result = Get-Process
    
    switch ($PsCmdlet.ParameterSetName)
    {
        "List"{ $result | Format-List }
        "Grid"{ $result | Out-GridView }
        "CSV"{ $result | Out-CSV $Path }
    }
}

Get-This

This way, Get-Help will also behave properly:

Get-Help Get-This

NAME
    Get-This
    
SYNTAX
    Get-This -AsList  [CommonParameters]
    
    Get-This -AsGrid  [CommonParameters]
    
    Get-This -AsCSV -Path   [CommonParameters]
    

ALIASES
    None
    

REMARKS
    None

I was going to recommend ParameterSet too, but I couldn’t use the variable the way I wanted, so that is an interesting implementation. I played with the code and the only think I didn’t like is that running Get-This required the AsList parameter since it was set to Mandatory. I removed the mandatory stipulation and changed the switch statement to default to the list if Grid or CSV is not used:

function Get-This
{
    [CmdletBinding(DefaultParameterSetName = 'List')]
    Param
    (
        [parameter(ParameterSetName = 'List')]
        [switch]$AsList,
 
        [parameter(ParameterSetName = 'Grid', Mandatory = $true)]
        [switch]$AsGrid,
 
        [parameter(ParameterSetName = 'CSV', Mandatory = $true)]
        [switch]$AsCSV,
 
        [parameter(ParameterSetName = 'CSV', Mandatory = $true)]
        [string]$Path
    )
 
    $result = Get-Process
 
    switch ($PsCmdlet.ParameterSetName)
    {
        
        "Grid"{ $result | Out-GridView }
        "CSV"{ $result | Out-CSV $Path }
        default{ $result | Format-List }
    }
}

Good suggestion Fausto. I’ve not played much with ParameterSet or DynamicParam, but I know someone asked the other day when the right time to implement DynamicParam is. Could be a good blog or Scripting Games addition, eh?

I think it was last year I created a function to help with creating dynamic parameters, seeing as they have so many things that require setting to properly work. I never published it though and I can’t seem to find it now :frowning: