none
Powershell - AddParameter() with spaces RRS feed

  • Question

  • Hi,

    Am trying to use AddCommand + AddParameter to run powershell command. One of the parameters- vmName contains spaces. So while executing the command am getting below error.

    msg=A positional parameter cannot be found that accepts argument 'server3_recoverytest'., VMName=

    I have already tried escaping using single, double quotes, backslashes etc.. Any suggestion on this ?

    powerShellInstance.Commands.AddCommand("Restore-Vm")

                                                  .AddParameter("Key1",value1)
                                                  .AddParameter("VmName", vmName)

    Thanks,

    Arvind S

    Wednesday, May 16, 2018 5:06 AM

All replies

  • You shouldn't have to escape it at all. Can you post the exact code you're using and the results you get?

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Wednesday, May 16, 2018 7:30 AM
  • Hello ArvindSesh,

    The Space has different meaning in the command, you need to convert it as below format.

    Question: How do I run a script with a space in the path?

    Answer 1: PS > c:my` foldermyscript.ps1

    Answer 2: PS> &(“c:my  foldermyscript.ps1”)

    PowerTip: Run a PowerShell Script with Space in the Path

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 17, 2018 9:52 AM
    Moderator
  • That's true if you're typing commands at the PowerShell prompt.  It should not be required when you're creating commands with the PowerShell APIs.  The parsing has already been done.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Thursday, May 17, 2018 7:04 PM