This topic contains 5 replies, has 4 voices, and was last updated by
December 29, 2014 at 6:24 am #21450
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
$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?
December 29, 2014 at 7:01 am #21451ParticipantPoints: 2Rank: 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.
December 29, 2014 at 7:12 am #21453KeymasterPoints: 1,624Rank: 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.
December 29, 2014 at 11:23 am #21461
Thanks for the suggestion.
December 29, 2014 at 11:29 am #21462
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?
December 29, 2014 at 12:56 pm #21463ModeratorPoints: 24Rank: Member
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.
The topic ‘Design Question’ is closed to new replies.