none
use defined variable in another command RRS feed

  • Question

  • my code is 

    $usb = read-host -p "enter your flash disk leter like g:\backup"

    net share backup="$usb" /GRANT:Everyone,Full /remark:"backup"

    why is wrong please advise me i search a lot but i do not get my answer 

    Saturday, July 26, 2014 5:54 PM

Answers

  • Just recently wrote an article about this:

    Windows IT Pro: Running Executables in PowerShell

    You should be able to write this:


    net share backup=$usb "/grant:Everyone,Full" /remark:backup

    If the $usb variable contains spaces, PowerShell will quote the entire backup parameter and its argument. To preserve the comma in the /grant parameter's argument, enclose the entire parameter and it's argument in quotes.

    You can test with the included showargs.exe sample utility to see what PowerShell is really doing.


    -- Bill Stewart [Bill_Stewart]

    Saturday, July 26, 2014 10:00 PM
    Moderator

All replies

  • No fault of yours here; PowerShell's a little but funny when it comes to executing external console commands like "net.exe". It parses your arguments and places quotation marks around them in ways that are sometimes confusing, and in this case, the problem  is the comma between Everyone and Full.  As far as net.exe is concerned, you passed this, or something very like it:

    net "share" "backup="g:\backup"" "/GRANT:Everyone" "Full" "remark:"backup""

    There are a couple of options, but the simplest is just to put a set of quotation marks around /GRANT:Everyone,Full , telling PowerShell to treat that as a single string token instead of treating the comma operator as the separator of elements of an array.

    net share backup="$usb" '/GRANT:Everyone,Full' /remark:"backup"

    In PowerShell 3.0 or later, you could also use the "stop parsing" operator, but that takes away your ability to do PowerShell variable expansion, so it's sometimes not terribly helpful.  That version would look like this:

    net share backup="$usb" --% /GRANT:Everyone,Full /remark:"backup"
    

    Everything after --% is treated as a literal string to be passed to the net.exe command as-is without any special parsing or quoting by PowerShell.

    • Edited by David Wyatt Saturday, July 26, 2014 6:23 PM
    Saturday, July 26, 2014 6:19 PM
  • thank you sir for your advise is that rule in powershell use " " ' ' for use variable or is different in another script ?

    is there any reference for understand parsing ? i see  that before but i do not now what is exactly

     $drive = read-host -p "which drive you want backup like (C:)"

    wbadmin start backup '-backuptarget:\\127.0.0.1\Backup' -include:"$drive"\  -quiet

    i use " " like but in that script not work !!!

    Saturday, July 26, 2014 6:50 PM
  • There is an about_Parsing help file which talks a little bit about the stop parsing operator, and the things that you might have to do to get a call to an external command line working. There's also an about_Quoting_Rules help file which describes the differences between single and double quotation marks in PowerShell.

    I haven't tested your wbadmin command, but right now the only thing that looks off about it is that you've placed the backslash outside of the quotation marks.  Try this:

    wbadmin start backup '-backuptarget:\\127.0.0.1\Backup' -include:"$drive\"  -quiet

    Saturday, July 26, 2014 8:08 PM
  • Just recently wrote an article about this:

    Windows IT Pro: Running Executables in PowerShell

    You should be able to write this:


    net share backup=$usb "/grant:Everyone,Full" /remark:backup

    If the $usb variable contains spaces, PowerShell will quote the entire backup parameter and its argument. To preserve the comma in the /grant parameter's argument, enclose the entire parameter and it's argument in quotes.

    You can test with the included showargs.exe sample utility to see what PowerShell is really doing.


    -- Bill Stewart [Bill_Stewart]

    Saturday, July 26, 2014 10:00 PM
    Moderator