Tech Off Thread

4 posts

PowerShell: Running an executable with a long filename and using a variable as a command line parame

Back to Forum: Tech Off
  • User profile image
    gdesroches

    I started a little project today, and I figured I could bang it out in PowerShell. I want to take all .img (DVD image) files and mount them as virtual drives with a program called "DAEMON Tools". I thought this would be easy to figure out.

    Here was my first step. I came up with the following code within seconds. I can now iterate through all .img files in a directory.

    [code]
    $files = get-childitem -filter *.img
    foreach ($file in $files)
    {
      echo $file.FullName
    }
    [/code]

    Now, to call the DAEMON Tools executable and pass the file name as a command line argument. The following line works in cmd.exe. I have to use quotes, because the path has a space.

    "C:\Program Files\DAEMON Tools\daemon.exe" -mount 0,myimage.img

    However, this does not work in PowerShell. It took me a quite a while to figure out how to run an exe with a long filename, but I finally found it on some random blog post. So, this is what I have now.

    [code]
    $files = get-childitem -filter *.img
    foreach ($file in $files)
    {
       &'C:\Program Files\DAEMON Tools\daemon.exe' -mount 0`,`"myimage.img`"
    }
    [/code]

    Now I need to know how to use the $file variable as a command line argument for the executable. I couldn't find anything related to this stuff in the documentation, in the wiki, or through the other various links google gave me.

    Why is this information so difficult to find? All the web sites I've seen contain the same basic MONAD tutorials. There is no mention of how to run a plain executable other than stating "Just type it in."

    Sorry about the rant. I'm just frustrated. This stuff should have taken a few minutes to figure out.

    Can anyone help me out here?

  • User profile image
    Toolsmith

    It appears to me that your quotes are a bit mismatched, but other than that, this should work for you:

    [code]
    $files = get-childitem -filter *.img
    foreach ($file in $files)
    {
       &'C:\Program Files\DAEMON Tools\daemon.exe' -mount 0,$file.FullName
    }
    [/code]

    Hope that helps.

  • User profile image
    Marc Magnin

    Toolsmith said:
    It appears to me that your quotes are a bit mismatched, but other than that, this should work for you:

    [code]
    $files = get-childitem -filter *.img
    foreach ($file in $files)
    {
       &'C:\Program Files\DAEMON Tools\daemon.exe' -mount 0,$file.FullName
    }
    [/code]

    Hope that helps.

    Hello,

    I'm replying to this old post because it helped me to find a way to dynamically call / run program requiring arguments like :

    C:\Program files\Program\program.exe /verysilent /norestart /LoadInf=".\conf.ini"

    This work if I write directly the full command in powershell like :

    [code]
    & C:\Program files\Program\program.exe /verysilent /norestart /LoadInf=".\conf.ini"
    [/code]

    Great!
    But I have no idea of the command in advance... Heck!
    So I tried several things like :

    [code]
    & $fullCommandString
    & $commandString $argsString
    . $commandString $argsString
    . $quotedCommandString $quotedArgsString
    powershell -command "& {" $quotedCommandString $quotedArgsString "}"
    invoke-item $commandString $argsString
    [/code]
    etc........

    but I still had errors caused by space in "Program files" or slash of arguments etc...

    The way I found is to pass a string for the command and an array of string for arguments :
    (here I directly wrote the command in the code by in reality, $command is feeded from and XML file)

    [code]
    $command = "C:\Program files\Program\program.exe /verysilent /norestart /LoadInf='.\conf.ini'"
    if($command -match "(?<appPath>.*\.[A-Za-z]+\s)(?<appArgs>.*)"){
       $args = [regex]::Split($Matches.appArgs.trim(), "\s" )
       & $Matches.appPath.trim() $args
    }
    [/code]

    Hope thats helps.

    Marc Magnin,
    Pourquoi faire simple quand on peut faire compliqué ?!

  • User profile image
    Marc Magnin

    Marc Magnin said:
    Toolsmith said:
    *snip*

    Hello,

    I'm replying to this old post because it helped me to find a way to dynamically call / run program requiring arguments like :

    C:\Program files\Program\program.exe /verysilent /norestart /LoadInf=".\conf.ini"

    This work if I write directly the full command in powershell like :

    [code]
    & C:\Program files\Program\program.exe /verysilent /norestart /LoadInf=".\conf.ini"
    [/code]

    Great!
    But I have no idea of the command in advance... Heck!
    So I tried several things like :

    [code]
    & $fullCommandString
    & $commandString $argsString
    . $commandString $argsString
    . $quotedCommandString $quotedArgsString
    powershell -command "& {" $quotedCommandString $quotedArgsString "}"
    invoke-item $commandString $argsString
    [/code]
    etc........

    but I still had errors caused by space in "Program files" or slash of arguments etc...

    The way I found is to pass a string for the command and an array of string for arguments :
    (here I directly wrote the command in the code by in reality, $command is feeded from and XML file)

    [code]
    $command = "C:\Program files\Program\program.exe /verysilent /norestart /LoadInf='.\conf.ini'"
    if($command -match "(?<appPath>.*\.[A-Za-z]+\s)(?<appArgs>.*)"){
       $args = [regex]::Split($Matches.appArgs.trim(), "\s" )
       & $Matches.appPath.trim() $args
    }
    [/code]

    Hope thats helps.

    Marc Magnin,
    Pourquoi faire simple quand on peut faire compliqué ?!

    I'have just found a better way by using the [diagnostics.process] class cause I need to wait the end of execution :

    [code]
     if($command -match "(?<appPath>.*\.[A-Za-z]+\s)(?<appArgs>.*)"){
         $com =$Matches.appPath.trim()
         $args =  $Matches.appArgs.trim()
         [diagnostics.process]::start($com, $args).WaitForExit()
    }
    [/code]


    Marc Magnin,
    Pourquoi faire simple quand on peut faire compliqué ?!

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.