Design Question

Welcome Forums General PowerShell Q&A Design Question

This topic contains 5 replies, has 4 voices, and was last updated by

3 years, 10 months ago.

  • Author
  • #21450

    Points: 22
    Rank: Member

    I am creating a module for PowerShell that works with the McAfee ePO API. Currently all of these commands will need to be passed the same set of parameters (-ComputerName, -Credential, -Port, etc) in addition to any parameters needed by the particular API call. Some of these command lines are very "busy" looking.

    I ask you, oh learned gurus of PowerShell, for advice on which way to go here. I still consider myself a newbie in PowerShell so I am wondering is it more acceptable to ask the user to supply these repeating parameters on each call or should I create a "New-Server" type call that returns an object that can be passed to subsequent functions that encapsulates all the repeating values?

    Get-ePOWhatever -ComputerName "blah" -Port 5443 -Credential $myCredential -APIPath "remote" -SystemName "Mikes-PC" -Parm1 $value1 -Parm2 $value2

    New Option
    $ePOServer = New-ePOServer -ComputerName "blah" -Port 5443 -Credential $myCredential -APIPath "remote"
    Get-ePOWhatever -Server $ePOServer -SystemName "Mikes-PC" -Parm1 $value1 -Parm2 $value2

    Also, I would like your opinion on the naming convention. Currently, when specifying the name of the server in the API call I name that parameter -ComputerName. When passing a parameter to specify which computer to perform the API call on, I would name that parameter -SystemName as that is what McAfee refers to it as. (See example above). Would it be more correct when referring to the ePO server to name that parameter -Server and each individual system as -ComputerName?

  • #21451

    Points: 2
    Rank: Member

    I like your example where you create an ePO server object and then use that in the subsequent commands – this is very reminiscent of how the CIM cmdlets work with CIM sessions. However, there are a few other ways you could approach this as well.

    If the parameters other than -ComputerName typically always have the same value (for instance, is the port number generally always 5443?) then you can provide default values in the parameter declaration so that the user will only need to specify these parameters if they are using something other than the default:

    param (
        $Port = '5443',
        $APIPath = 'Remote'

    Alternatively, if your users are relatively savvy, you can have them configure their $PSDefaultParameterValues variable to include the values they need for these parameters.

  • #21453

    Points: 1,624
    Helping HandTeam Member
    Rank: Community Hero

    I think PowerShell's model is to do both.

    Look at Invoke-Command – it can accept a -ComputerName (and -Port and whatnot), or it can accept an existing -PSSession object. In cases where you just need a quick one-off, you use the former parameter set; in cases where you'll be using it a lot, perhaps the latter.

  • #21461

    Points: 22
    Rank: Member

    Thanks for the suggestion.

  • #21462

    Points: 22
    Rank: Member

    I was going to ask about that.

    In VB (where I am much more comfortable), if I want to have a function that can take different sets of parameters, I can just create two different prototype statements for the same function and the compiler figures it out.
    Private Function DoThis (byval item as string, byval i as integer, byval username as string) as String
    Private Function DoThis(byval item as string, byval price as double, byval credentials as objUser) as string

    How does one do it in PowerShell? Can I declare the function twice or do I look to see which parameters are null and try to figure out what is available for me to use?

  • #21463

    Points: 24
    Team Member
    Rank: Member


    Search the PowerShell help "about_Functions_Advanced_Parameters" ( for "ParameterSetName" to get started and "about_Functions_CmdletBindingAttribute" ( for "DefaultParameterSetName" if you want to define a default parameter set. If you search for "ParameterSetName" in your favourite search engine you will find more examples.

The topic ‘Design Question’ is closed to new replies.