Windows Vista Forums

Possible to pass the empty parameter to a child from Powershell?
  1. #1


    Jason R. Coombs Guest

    Possible to pass the empty parameter to a child from Powershell?

    I'm using a program that takes as one of its parameters a string that will be
    appended to the output. One possible option for this parameter is the empty
    string. I can pass the empty string from a cmd.exe shell with "".

    For example, consider the Visual Basic script, named print-args.vbs:
    WScript.echo WScript.Arguments.Item(0)
    WScript.echo WScript.Arguments.Item(1)
    WScript.echo Wscript.Arguments.Item(2)

    If this script is executed using the following syntax:

    PS C:\> cmd /c 'cscript /Nologo print-args.vbs foo "" bar'

    foo

    bar

    But if the same script is run from the Powershell, the parameter is missing.

    PS C:\> cscript /Nologo print-args.vbs foo "" bar
    foo
    bar
    C:\print-args.vbs(3, 1) Microsoft VBScript runtime error: Subscript out of
    range

    After some muddling around with the syntax, I found that I could in fact
    force Powershell to pass the empty string using the following syntax.

    PS C:\> cscript /Nologo print-args.vbs foo `"`" bar
    foo

    bar

    So for my question.

    Is the `"`" the recommended syntax for passing an empty-string parameter?

    I find the syntax confusing... and in the case of my application, when I use
    the "" syntax that works in cmd.exe and other common shells (i.e. bash), it's
    silently ignored by Powershell, so the subsequent parameter gets consumed
    where I intended to have an empty string. This causes very bad unintended
    behavior.

    I acknowledge that Powershell isn't cmd.exe or bash, but I have another
    question. Is there a reason that "" or '' couldn't be honored as the empty
    string when passed to a child command? If the answer is no, I'll follow up
    with a ticket on MS Connect.



    Regards,
    Jason

      My System SpecsSystem Spec

  2. #2


    Shay Levy [MVP] Guest

    Re: Possible to pass the empty parameter to a child from Powershell?

    Hi Jason,

    Can you try this:


    cscript /Nologo print-args.vbs foo '""' bar

    - or -

    cscript /Nologo print-args.vbs foo ([string]::empty) bar






    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar: http://tinyurl.com/PSToolbar



    JC> I'm using a program that takes as one of its parameters a string
    JC> that will be appended to the output. One possible option for this
    JC> parameter is the empty string. I can pass the empty string from a
    JC> cmd.exe shell with "".
    JC>
    JC> For example, consider the Visual Basic script, named print-args.vbs:
    JC> WScript.echo WScript.Arguments.Item(0)
    JC> WScript.echo WScript.Arguments.Item(1)
    JC> WScript.echo Wscript.Arguments.Item(2)
    JC> If this script is executed using the following syntax:
    JC>
    JC> PS C:\> cmd /c 'cscript /Nologo print-args.vbs foo "" bar'
    JC>
    JC> foo
    JC>
    JC> bar
    JC>
    JC> But if the same script is run from the Powershell, the parameter is
    JC> missing.
    JC>
    JC> PS C:\> cscript /Nologo print-args.vbs foo "" bar
    JC> foo
    JC> bar
    JC> C:\print-args.vbs(3, 1) Microsoft VBScript runtime error: Subscript
    JC> out of
    JC> range
    JC> After some muddling around with the syntax, I found that I could in
    JC> fact force Powershell to pass the empty string using the following
    JC> syntax.
    JC>
    JC> PS C:\> cscript /Nologo print-args.vbs foo `"`" bar foo
    JC>
    JC> bar
    JC>
    JC> So for my question.
    JC>
    JC> Is the `"`" the recommended syntax for passing an empty-string
    JC> parameter?
    JC>
    JC> I find the syntax confusing... and in the case of my application,
    JC> when I use the "" syntax that works in cmd.exe and other common
    JC> shells (i.e. bash), it's silently ignored by Powershell, so the
    JC> subsequent parameter gets consumed where I intended to have an empty
    JC> string. This causes very bad unintended behavior.
    JC>
    JC> I acknowledge that Powershell isn't cmd.exe or bash, but I have
    JC> another question. Is there a reason that "" or '' couldn't be
    JC> honored as the empty string when passed to a child command? If the
    JC> answer is no, I'll follow up with a ticket on MS Connect.
    JC>
    JC> Regards,
    JC> Jason



      My System SpecsSystem Spec

  3. #3


    Joel Bennett Guest

    Re: Possible to pass the empty parameter to a child from Powershell?

    The problem is that you don't need an empty string, you SPECIFICALLY
    need a pair of quotes, which cscript will PARSE as an empty string.

    PowerShell converts the "" to an empty string, and as far as cscript is
    concerned, that just means that foo "" bar = foo bar

    So, you'll get the same results if you do this:

    cscript /nologo print-args.vbs foo '""' bar

    or this ...

    cscript /nologo print-args.vbs foo """""" bar

    or this

    cscript /nologo print-args.vbs foo "`"`"" bar

    or even this ...

    $empty = '""'
    cscript /nologo print-args.vbs foo $empty bar


    .... basically, anything that generates a STRING which consists of a pair
    of quotes ...


    --
    Joel

    Jason R. Coombs wrote:

    > I'm using a program that takes as one of its parameters a string that will be
    > appended to the output. One possible option for this parameter is the empty
    > string. I can pass the empty string from a cmd.exe shell with "".
    >
    > For example, consider the Visual Basic script, named print-args.vbs:
    > WScript.echo WScript.Arguments.Item(0)
    > WScript.echo WScript.Arguments.Item(1)
    > WScript.echo Wscript.Arguments.Item(2)
    >
    > If this script is executed using the following syntax:
    >
    > PS C:\> cmd /c 'cscript /Nologo print-args.vbs foo "" bar'
    >
    > foo
    >
    > bar
    >
    > But if the same script is run from the Powershell, the parameter is missing.
    >
    > PS C:\> cscript /Nologo print-args.vbs foo "" bar
    > foo
    > bar
    > C:\print-args.vbs(3, 1) Microsoft VBScript runtime error: Subscript out of
    > range
    >
    > After some muddling around with the syntax, I found that I could in fact
    > force Powershell to pass the empty string using the following syntax.
    >
    > PS C:\> cscript /Nologo print-args.vbs foo `"`" bar
    > foo
    >
    > bar
    >
    > So for my question.
    >
    > Is the `"`" the recommended syntax for passing an empty-string parameter?
    >
    > I find the syntax confusing... and in the case of my application, when I use
    > the "" syntax that works in cmd.exe and other common shells (i.e. bash), it's
    > silently ignored by Powershell, so the subsequent parameter gets consumed
    > where I intended to have an empty string. This causes very bad unintended
    > behavior.
    >
    > I acknowledge that Powershell isn't cmd.exe or bash, but I have another
    > question. Is there a reason that "" or '' couldn't be honored as the empty
    > string when passed to a child command? If the answer is no, I'll follow up
    > with a ticket on MS Connect.
    >
    > Regards,
    > Jason

      My System SpecsSystem Spec

Possible to pass the empty parameter to a child from Powershell? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass a parameter to XMLHttp AdityaKir PowerShell 1 22 Sep 2008
Pass a parameter in sql statement with VB 2005... bill .NET General 14 21 Jul 2008
Can I pass objects to powershell from my C# program ssg31415926 PowerShell 4 04 Jun 2008
powershell to execute store procedure with an parameter Frank PowerShell 9 05 Mar 2007
How to pass an empty string as an argument to an executable? Joe Blow PowerShell 2 31 Dec 2006