One of the things that has bugged me for a long time is how to create a PowerShell-based activity using QIK CLI, but also enable the use of switch parameters.It wasn?t until today that I really turned my attention to the problem because it was something I desperately needed to solve for some current activities I?m building. The good news is that it?s not as difficult as it might seem.
Just to give you some background in case you haven?t personally run into this problem, there are two parts to this. First, many PowerShell Cmdlets allow you to specify optional ?switch? parameters when calling the Cmdlet. Here?s an example ? the Connect-WSMan cmdlet has a switch parameter named ?UseSSL?:
| Connect-WSMan [-ApplicationName <string>] [-ComputerName <string>] [-Port <int>] [-UseSSL] [-Authentication <Authentication>] [-ComputerName <string>] [-Credential <PSCredential>] [-Port <int>] [-SessionOption <hashtable>] |
This parameter is optional, and it?s boolean, which means it?s ?True? if you use the switch, ?False? if you don?t use the switch. Technically, it?s just undefined if you don?t use the switch, but I?m talking at a high level here.
Note: You can find out more about switch parameters from TechNet here: http://technet.microsoft.com/en-us/library/dd347712.aspx (scroll to ?Switch Parameters?)
So part two of the problem is that when you use QIK CLI to create PowerShell command activities, there?s really no obvious way to handle switch parameters. Here?s the above PowerShell Cmdlet put into the QIK CLI command wizard:
You can see that I have the ?UseSSL switch parameter in the Command Line field, and I have a ?UseSSL? parameter entered below, but the switch parameter is either included or not included. If I treated it like any other parameter in the command line and insert the Opalis parameter after ?-UseSSL?, it would be an invalid command line. There?s also no way for me to tell Opalis to conditionally add the ?-UseSSL? parameter based on the True/False value of the user selection. This puts me into a situation where I might not be able to use the functionality of a Cmdlet because I can?t correctly specify the optional switch parameters.
There are two ways to solve this problem: the first is through scripting, and the second is just by modifying the input parameters. I?ll take the second one now since it?s actually the easiest.
Modifying Input Parameters to Accommodate Switches
When you specify parameters in the command line using the $(Parameter) notation, it inserts string values into the command line containing the value the user has chosen in the Designer. What most people might not think of is that you can actually leave the parameter name off in the command line and simply use the parameter value as the actual switch text. Here?s what that looks like:
What I have done is taken the ?UseSSL? parameter and changed it from a Boolean type to ?Text with Selection?, and I have defined two values for the selection list:
- A space
- ?-UseSSL?
I then changed the command line string and removed the ?-UseSSL? switch and instead just inserted the parameter:
When I finish the wizard and the assembly is created, I can look at it using the Invoke .NET activity (from the QIK IP), and I am able to either leave the ?UseSSL? parameter empty or I can select the ?-UseSSL? option:
Now when the activity runs, it will simply append the ?-UseSSL? switch string to the end of the command line. That?s actually a very simple solution to the problem. The only issue I have with it is that it?s kind of clunky from a usability perspective to have a selection that?s either a space or ?-UseSSL?, instead of ?True?/?False?.
If you?re ok with the above solution, you can just stop reading here. The other way to solve this is a bit more involve and I am actually having some issues with QIK?s quote-escaping right now, trying to get the right combination of single and double quotes that both satisfies QIK?s requirements and will actually run in PowerShell and give the right output. More on that process in another article?
Tara Reed Avril Lavigne Bridget Moynahan Noureen DeWulf Nicollette Sheridan
No comments:
Post a Comment