Design Question

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 1 year, 11 months ago.

  • Author
    Posts
  • #21450
    Profile photo of Brian Cyr
    Brian Cyr
    Participant

    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?

    Example:
    Currently
    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 [b]-Server $ePOServer [/b]-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
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of Brian Cyr
    Brian Cyr
    Participant

    Matt,
    Thanks for the suggestion.

  • #21462
    Profile photo of Brian Cyr
    Brian Cyr
    Participant

    Don,
    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.
    Example:
    Private Function DoThis (byval item as string, byval i as integer, byval username as string) as String
    or
    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
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Brian,

    Search the PowerShell help "about_Functions_Advanced_Parameters" (http://technet.microsoft.com/en-us/library/hh847743.aspx) for "ParameterSetName" to get started and "about_Functions_CmdletBindingAttribute" (http://technet.microsoft.com/en-us/library/hh847872.aspx) 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.

You must be logged in to reply to this topic.