0

I'm trying to run a command and it's not working, here is the command which works just fine at the PowerShell command prompt:

mcli-add DiskLocator -r diskLocatorName=XAShared-2015-10-13, siteName=mysite, storeName=pvs_prod

Here is the code I'm trying to use to make that command work. I've tried a number of methods (see commented lines), from having it all in one line to having it in separate parameters to not putting the commas and putting the commas. The parameter name is -p, the parts after that are the arguments.

Here's the code as I have it now, attempting to capture the output which seems successful but it's not actually doing anything, I'm getting Executing: Add DiskLocator in the event log but I am not seeing the second result which should be Add Succeeded. followed by a result ID:

using (Runspace runspace = RunspaceFactory.CreateRunspace())
            {
                runspace.Open();
                PowerShell ps = PowerShell.Create();
                ps.Runspace = runspace;
                PSSnapInException psex;
                runspace.RunspaceConfiguration.AddPSSnapIn("McliPSSnapIn", out psex);

                Pipeline pipeline = runspace.CreatePipeline();

                Command mcliAdd = new Command("mcli-add");
                mcliAdd.Parameters.Add(null, "DiskLocator");
                mcliAdd.Parameters.Add("-r", "diskLocatorName=" + diskLocatorName + ", " + "siteName=" + siteName + ", " + "storeName=" + storeName);
                //mcliAdd.Parameters.Add("siteName=" + siteName + ", ");
                //mcliAdd.Parameters.Add("storeName=" + storeName);
                pipeline.Commands.Add(mcliAdd);
                Collection<PSObject> output = pipeline.Invoke();
                foreach (PSObject x in output)
                {
                    log.WriteEntry(x.ToString(), EventLogEntryType.Information);
                }
            }

Errors I have gotten with the commented lines uncommented are like this:

A parameter cannot be found that matches parameter name 'siteName=mysite'

UPDATE:

Even weirder is this, I can put this line here any way I want it after the comma and it won't throw an error:

mcliAdd.Parameters.Add("r", "who cares what I put here nothing happens");

This leads me to believe that the DiskLocator argument isn't being used right. I can't use it as a parameter because it says that's not a parameter name.

However this can't be true because if I remove it or the -r it either says DiskLocator is missing or that -r is missing. If I pass only -r without the other details it throws an error complaining that there's no string[] array values. So I'm at a loss here. It SHOULD throw an error if I don't provide the entire array, but it's not even throwing an error when I pass just the disklocatorname part.

2
  • Instead of embedding powershell, it may actually be easier to program directly against the SOAP API. docs.citrix.com/content/dam/docs/en-us/provisioning-services/… Commented Oct 14, 2015 at 22:29
  • Yeah I don't know much about soap and those client side proxy svcutil thing is a little foreign to me but thanks I'll check it out. Commented Oct 14, 2015 at 22:40

2 Answers 2

1

The argument to -r appears to be an array. You will need to give it an array of strings. Also, when passing parameters, you must omit the - since that is merely a parameter delimiter on the command line. The parameter itself is called "r".

mcliAdd.Parameters.Add("r", new string[]{"diskLocatorName=" + diskLocatorName, "siteName=" + siteName, "storeName=" + storeName});

Or something similar.

Failing this, an alternative to try which assumes a terribly written powershell module, would be:

 mcliAdd.Parameters.Add(null, "-r diskLocatorName=" + diskLocatorName + ", " + "siteName=" + siteName + ", " + "storeName=" + storeName);

Another thought for a possibility:

Command mcliAdd = new Command("mcli-add");
pipeline.Commands.Add(mcliAdd);
ps.AddArgument("DiskLocator");
ps.AddParameter("r", new string[]{"diskLocatorName=" + diskLocatorName, "siteName=" + siteName, "storeName=" + storeName});
// Alternatively add both using AddArgument
Sign up to request clarification or add additional context in comments.

12 Comments

That really seemed like a good solution but alas it didn't work in any form so far. I tried it as is, I tried fixing the commas to form the array, adding more commas to fit after each command, etc.
(I updated my answer to fix the commas in the array) What error do you get when passing an array?
No error, I even tried removing the required parameters siteName and storeName from the array and it didn't give an error (it should if I do that at the command prompt) which means it's not accepting all the additional arguments with -p in fact I think it's ignoring the whole thing and pausing to prompt for more and never executing, assuming I entered only mcli-add DiskLocator and nothing else.
Try using mcliAdd.AddArgument("DiskLocator") instead of the Parameters array.
You already created ps, it's your toplevel Powershell object.
|
0

Powershell doesn't always treat commas as delimiters as you might expect. Try using spaces or parenthesis (or all three: "(Value), (value2), (value3)").

@Eris already suggested an array;

$item = @()
$item += "-r"
$item += ("diskLocatorName=" + diskLocatorName + ", " + "siteName=" + siteName + ", " + "storeName=" + storeName)
function($item)

You could also try an intermediate variable:

$item = ("diskLocatorName=" + diskLocatorName + ", " + "siteName=" + siteName + ", " + "storeName=" + storeName)
function(("-r"), ($item))

It should be noted that string concatenation for an array needs a parenthesis. $test = "a"+"b" fails but $test = ("a"+"b") succeeds. Omitting the parenthesis results in multiple array elements.

2 Comments

I'll have to try to translate that to c#. It's just not accepting anything I give it. It just won't follow through on the command and I can't tell what's really happening since it's not throwing an error.
Updated my post just now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.