locked
Running a CMD.exe from PowerShell with arguments RRS feed

  • Question

  • Hello guys, I am working with a driver backup program that I need to automate. I am using Power shell to run a few other tasks, like check the OS version and create a folder based on the system type for drivers. We deploy many different devices and needed a way to automate. That is neither here nor there. Here is what I am trying to run, but the CMD, will not seem to pick up the arguments. If I run from the CMD prompt, it will run as expected, but when I lick it off with Power shell, I get a few errors. 

    Here is what I have:

    $command = @'
    cmd.exe /C C:\DriverBU\DrvBK.exe MODE="BACKUP" BKPATH="C:\TempDrivers" BKDESC="Drivers" BKFILE="Backup %NOW%  %COMPUTERNAME%.bki" BKPATHFTM="%COMPUTERNAME%" BKDEVFMT="%DEVNAME%" BKDATEFMT="" OPT="HW"
    '@

    Invoke-Expression -Command:$command


    This will open the program, however, will not run the arguments.

    Thoughts?

    Thank you!

    Paul


    Duramaxster

    Monday, June 16, 2014 2:35 PM

Answers

  • Sorry, in PowerShell we write $ENV:COMPUTERNAME, not %COMPUTERNAME%


    $now = "{0:yyyy-MM-dd HH:mm}" -f (get-date)
    $devName = "whatever"
    C:\DriverBU\DrvBK.exe MODE=BACKUP BKPATH=C:\TempDrivers BKDESC=Drivers BKFILE="Backup $now $ENV:COMPUTERNAME.bki" BKPATHFTM=$ENV:COMPUTERNAME BKDEVFMT=$devName 'BKDATEFMT=""' OPT=HW
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Monday, June 30, 2014 7:08 PM
    • Marked as answer by Bill_Stewart Tuesday, July 15, 2014 9:58 PM
    Monday, June 16, 2014 3:59 PM

All replies

  • Wrong bits.  Just run the command:

    $command='cmd.exe /C C:\DriverBU\DrvBK.exe MODE=BACKUP"BKPATH=C:\TempDrivers BKDESC=Drivers BKFILE="Backup %NOW%  %COMPUTERNAME%.bki" BKPATHFTM="%COMPUTERNAME%" BKDEVFMT=%DEVNAME% BKDATEFMT="" OPT=HW'

    & $command

    You only need quotes when items have spaves or other terminating characters.

    Also use Start-Process.


    ¯\_(ツ)_/¯

    Monday, June 16, 2014 2:48 PM
  • Here is the start process method which is more flexible:

    $cmdArgs=@(
         '/C',
         'C:\DriverBU\DrvBK.exe', 
         'MODE=BACKUP',
         'BKPATH=C:\TempDrivers',
         'BKDESC=Drivers,
         ' BKFILE="Backup %NOW%  %COMPUTERNAME%.bki"',
         ' BKPATHFTM="%COMPUTERNAME%"',
         ' BKDEVFMT=%DEVNAME%',
         ' BKDATEFMT=""',
         'OPT=HW'
    )
    
    Start-Process -FIlePath cmd -Arguments $cmdArgs
    

    You can also place all of the command in a batch file and just call that as a command

    mybatch.bat

    The file would contain:

    C:\DriverBU\DrvBK.exe MODE=BACKUP BKPATH=C:\TempDrivers BKDESC=Drivers BKFILE="Backup %NOW%  %COMPUTERNAME%.bki"  BKPATHFTM="%COMPUTERNAME%" BKDEVFMT=%DEVNAME%  BKDATEFMT=""' OPT=HW
    


    ¯\_(ツ)_/¯

    Monday, June 16, 2014 2:55 PM
  • Hello guys, I am working with a driver backup program that I need to automate. I am using Power shell to run a few other tasks, like check the OS version and create a folder based on the system type for drivers. We deploy many different devices and needed a way to automate. That is neither here nor there. Here is what I am trying to run, but the CMD, will not seem to pick up the arguments. If I run from the CMD prompt, it will run as expected, but when I lick it off with Power shell, I get a few errors. 

    Here is what I have:

    $command = @'
    cmd.exe /C C:\DriverBU\DrvBK.exe MODE="BACKUP" BKPATH="C:\TempDrivers" BKDESC="Drivers" BKFILE="Backup %NOW%  %COMPUTERNAME%.bki" BKPATHFTM="%COMPUTERNAME%" BKDEVFMT="%DEVNAME%" BKDATEFMT="" OPT="HW"
    '@

    Invoke-Expression -Command:$command


    This will open the program, however, will not run the arguments.

    Thoughts?

    Thank you!

    Paul


    Duramaxster

    Part of your problem is that you cannot use @''@  (a here string) to specify a command because it retains the line breaks.


    ¯\_(ツ)_/¯

    Monday, June 16, 2014 2:56 PM
  • Just run directly in PowerShell. Example:


    $now = "{0:yyyy-MM-dd HH:mm}" -f (get-date)
    $devName = "whatever"
    C:\DriverBU\DrvBK.exe MODE=BACKUP BKPATH=C:\TempDrivers BKDESC=Drivers BKFILE="Backup $now %COMPUTERNAME%.bki" BKPATHFTM=$ENV:COMPUTERNAME BKDEVFMT=$devName 'BKDATEFMT=""' OPT=HW
    


    -- Bill Stewart [Bill_Stewart]

    Monday, June 16, 2014 3:51 PM
  • Bill - You forgot %computername%

    BKFILE="Backup $now %COMPUTERNAME%.bki"

    One reason I like to use Start-Process as it is easier to see the args.

    If this is not using environment variables then using CMD will be of no help.


    ¯\_(ツ)_/¯

    Monday, June 16, 2014 3:54 PM
  • Sorry, in PowerShell we write $ENV:COMPUTERNAME, not %COMPUTERNAME%


    $now = "{0:yyyy-MM-dd HH:mm}" -f (get-date)
    $devName = "whatever"
    C:\DriverBU\DrvBK.exe MODE=BACKUP BKPATH=C:\TempDrivers BKDESC=Drivers BKFILE="Backup $now $ENV:COMPUTERNAME.bki" BKPATHFTM=$ENV:COMPUTERNAME BKDEVFMT=$devName 'BKDATEFMT=""' OPT=HW
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Monday, June 30, 2014 7:08 PM
    • Marked as answer by Bill_Stewart Tuesday, July 15, 2014 9:58 PM
    Monday, June 16, 2014 3:59 PM