locked
diskpart script arguments built in powershell are rejected as invalid RRS feed

  • Question

  • I want to use powershell to automat diskpart, but the script arguments
    that powershell builds are seen as invalid. For example, the trivial
    diskpart script below works as expected:

    $diskDetailsDPCom=@(
        "select disk 0",
        “detail disk”,
        “exit”
    )
    $diskDetailsDPCom | diskpart

    But when I try to build the “select disk” command by concatenating a
    variable or literal “0”, then diskpart fails with “The arguments
    specified for this command are not valid.”:

    $diskDetailsDPCom=@(
        "select disk " + “0”,
        “detail disk”,
        “exit”
    )
    $diskDetailsDPCom | diskpart

    What is going on here, and how might I proceed?
    Thanks!

    Wednesday, October 12, 2016 5:12 PM

Answers

  • It looks like my problem is that in PowerShell the comma (,) operator has higher precedence than the concatenation (+) operator. So I must build command lines within parentheses to get the correct order of operation:
    @(
        ("select disk " + “0”),
        “detail disk”,
        “exit”
    )
    Gives me the results I expected. Thanks to Ryan Bemrose on stackoverflow!

    • Marked as answer by Charlweed Wednesday, October 12, 2016 5:25 PM
    • Edited by Charlweed Wednesday, October 12, 2016 5:26 PM
    Wednesday, October 12, 2016 5:25 PM