Windows Vista Forums

Running a command from inside a script, command line is corrupted

  1. #1


    Raymond Vought Guest

    Running a command from inside a script, command line is corrupted

    Hello,

    I am trying to give Powershell a chance, since it seems like it may have
    some promise.

    I have this powershell script that needs to run an executable occasionally,
    using logic from the script. The executable has a command line that is
    sensitive to spacing so it has to be just right. In particular, my script
    needs to run the wlogevent.exe like this:

    c:\temp\wlogevent.exe -c:41 -l:3 -ss:"String status message here."

    In cmd.exe, wlogevent works when called as above. But if you try to run
    wlogevent with spaces after the colons it it's command line like this

    c:\temp\wlogevent.exe -c: 41 -l: 3 -ss: "IIS7 is in place."

    it will not work.

    So in my script I create a string command like this:

    $command = "c:\temp\wlogevent.exe -c:41 -l" + [char]58 + $Args[1] + " -ss" +
    [char]58 + [char]34 + $Args[0] + [char]34
    Where $Args[0] and Args[1] are string parameters of a function. I'm using
    [char]58 for the ":" character because I sense there's domething weird about
    colons in powershell.

    Anyway, when I write-host $command, it looks correct in the console output;
    spacing is correct throughout.

    But when I invoke-expression $command, spaces somehow insinuate themselves
    after the colons in the command line, and it doesn't work. I know this by
    viewing the command line of the spawned process, and that command line is

    c:\temp\wlogevent.exe -c: 41 -l: 3 -ss: "IIS7 is in place."

    There is a space after every colon except the one in the path. What is
    going on here?

    Thanks,

    RFV








      My System SpecsSystem Spec

  2. #2


    DennisD Guest

    Re: Running a command from inside a script, command line is corrupted

    try it like this:

    param (
    $arg1,
    $arg2
    )

    $command = "c:\temp\wlogevent.exe -c:41 -l:$arg1 -ss:`"$arg2`""

    write-host $command


    B.t.w.: "I am trying to give Powershell a chance, since it seems like
    it may have
    some promise." L0L

      My System SpecsSystem Spec

  3. #3


    Raymond Vought Guest

    Re: Running a command from inside a script, command line is corrupted

    Thank you for responding. I implemented your suggestion so that my function
    looks like this:

    Function InformMe {

    param (

    $arg1,

    $arg2

    )

    $command = "c:\temp\wlogevent.exe -c:41 -l:$arg2 -ss:`"$arg1`""

    write-host $command

    invoke-expression $command

    }

    This works in exactly the same way as my other code; the write-host output
    looks good, but wlogevent.exe breaks because there are spaces in the command
    line after the colons.

    In particular, the write host output in my case looks like this:

    c:\temp\wlogevent.exe -c:41 -l:3 -ss:"IIS7 is in place."

    which is perfect. But the spawned process has command line

    "C:\temp\wlogevent.exe" -c: 41 -l: 3 -ss: "IIS7 is in place."

    (as captured using sysinternals process explorer)

    What is it about colons in powershell?

    RFV






    "DennisD" <dennis.damen@xxxxxx> wrote in message
    news:c502bce8-404b-47ff-9c3f-06cb4f643dbd@xxxxxx

    > try it like this:
    >
    > param (
    > $arg1,
    > $arg2
    > )
    >
    > $command = "c:\temp\wlogevent.exe -c:41 -l:$arg1 -ss:`"$arg2`""
    >
    > write-host $command
    >
    >
    > B.t.w.: "I am trying to give Powershell a chance, since it seems like
    > it may have
    > some promise." L0L


      My System SpecsSystem Spec

  4. #4


    DennisD Guest

    Re: Running a command from inside a script, command line is corrupted

    I don't think it has anything to do with the colons. Could you try the
    following:

    param (
    $arg1,
    $arg2
    )

    $command = "echo -c:41 -l:$($arg1.trim()) -ss:`"$($arg2.Trim())`""

      My System SpecsSystem Spec

  5. #5


    Raymond Vought Guest

    Re: Running a command from inside a script, command line is corrupted

    One of the args I am passing is a "3", so can't do .trim() since the "3" has
    been secretly cast as a [System.Int32].

    Using your suggestion, we get error output like this:

    Method invocation failed because [System.Int32] doesn't contain a method
    named
    'trim'.

    Thanks for the suggestion all the same. Tried like this next:

    $command = "c:\temp\wlogevent.exe -c:41 -l:$($arg2) -ss:`"$($arg1.Trim())`""

    This results in the same behavior as before. When we write-host the string
    it looks fine, but when powershell shells this command string, it changes
    the comand string so that where-ever there was "-x:", there's a "-x: ".



    "DennisD" <dennis.damen@xxxxxx> wrote in message
    news:4349673c-731c-49ac-9392-07c2778d5671@xxxxxx

    >I don't think it has anything to do with the colons. Could you try the
    > following:
    >
    > param (
    > $arg1,
    > $arg2
    > )
    >
    > $command = "echo -c:41 -l:$($arg1.trim()) -ss:`"$($arg2.Trim())`""


      My System SpecsSystem Spec


Running a command from inside a script, command line is corrupted
Similar Threads
Thread Forum
How to pass the name of a PowerShell script as a command line para PowerShell
noob: PS script fails, but runs from command line PowerShell
Running a Command line script from within powershell. PowerShell
Script from command-line PowerShell
Running PS1 file from command line PowerShell