locked
Resolve 'tags' to variable value in Powershell RRS feed

  • Question

  • I have a Powershell script with code similar to the following (variable values are actually being read in from a params file, but for brevity, I am hard-coding them here):

    $ROOT = C:\Path1\Path2\Path3

    $CMD_LINE = /F "%ROOT%\MyFile.xxx" /L "%ROOT%/log.txt" ...

    I want to execute a program and have $CMD_LINE be a parameter, but before doing so, I want to 'resolve' $CMD_LINE such that the final value is with %ROOT% is replaced with the value of $ROOT. Thus $CMD_LINE's value would be:

    /F "C:\Path1\Path2\Path3\MyFile.xxx" /L "C:\Path1\Path2\Path3/log.txt" ...

    There are multiple occurances of the root value not only in this but other variables. This script will be deployed to clients and I want them to be able to change the value of ROOT in one place (in the param file) and it will be implemented in any variable that has %ROOT% tag in it (via code I've written of course).


    Doug Bishop

    Tuesday, July 3, 2012 5:23 PM

Answers

  • hi,

    if using Powershell you have to use $Root instead of %Root% in order to reference to a variable.


    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com

    • Proposed as answer by Peddy1st Tuesday, July 3, 2012 7:29 PM
    • Marked as answer by Yan Li_ Monday, July 9, 2012 1:38 AM
    Tuesday, July 3, 2012 5:26 PM
  • Rather then using %ROOT%, you can change it to $ROOT

    $ROOT = C:\Path1\Path2\Path3
     
    $CMD_LINE = "/F $ROOT\MyFile.xxx /L $ROOT/log.txt"
    As long as the variable $ROOT is contained within double quotes, powershell will automatically expand the variable so, it will contain the value of that variable


    • Edited by clayman2 Tuesday, July 3, 2012 5:29 PM
    • Proposed as answer by Peddy1st Tuesday, July 3, 2012 7:29 PM
    • Marked as answer by Yan Li_ Monday, July 9, 2012 1:38 AM
    Tuesday, July 3, 2012 5:27 PM

All replies

  • hi,

    if using Powershell you have to use $Root instead of %Root% in order to reference to a variable.


    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com

    • Proposed as answer by Peddy1st Tuesday, July 3, 2012 7:29 PM
    • Marked as answer by Yan Li_ Monday, July 9, 2012 1:38 AM
    Tuesday, July 3, 2012 5:26 PM
  • Rather then using %ROOT%, you can change it to $ROOT

    $ROOT = C:\Path1\Path2\Path3
     
    $CMD_LINE = "/F $ROOT\MyFile.xxx /L $ROOT/log.txt"
    As long as the variable $ROOT is contained within double quotes, powershell will automatically expand the variable so, it will contain the value of that variable


    • Edited by clayman2 Tuesday, July 3, 2012 5:29 PM
    • Proposed as answer by Peddy1st Tuesday, July 3, 2012 7:29 PM
    • Marked as answer by Yan Li_ Monday, July 9, 2012 1:38 AM
    Tuesday, July 3, 2012 5:27 PM
  • Hi,

    any further questions? Is it working?

    If your question is answered it would be nice if you mark it accordingy because this may help otheres who have got the same or a similar question.

    Thanks a lot in advance

    Kind regards

      Thomas


    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com

    Tuesday, July 3, 2012 7:30 PM
  • I thought I posted a reply here on Monday. For some reason it seems like it did not post. I resolved the problem using the following code:

    $CMD_LINE = $CMD_LINE -replace "%ROOT%", $ROOT

    Usint $ROOT in the value of $CMD_LINE would have worked if all of the occurances of %ROOT% were quoted, but that was not the case. The above method did work.


    Doug Bishop

    Wednesday, July 11, 2012 1:58 PM
  • I have a Powershell script with code similar to the following (variable values are actually being read in from a params file, but for brevity, I am hard-coding them here):

    $ROOT = C:\Path1\Path2\Path3

    $CMD_LINE = /F "%ROOT%\MyFile.xxx" /L "%ROOT%/log.txt" ...

    I want to execute a program and have $CMD_LINE be a parameter, but before doing so, I want to 'resolve' $CMD_LINE such that the final value is with %ROOT% is replaced with the value of $ROOT. Thus $CMD_LINE's value would be:

    /F "C:\Path1\Path2\Path3\MyFile.xxx" /L "C:\Path1\Path2\Path3/log.txt" ...

    There are multiple occurances of the root value not only in this but other variables. This script will be deployed to clients and I want them to be able to change the value of ROOT in one place (in the param file) and it will be implemented in any variable that has %ROOT% tag in it (via code I've written of course).


    Doug Bishop

    shouldn't the path string be quoted, as in:

        $ROOT = "C:\Path1\Path2\Path3"


    Al Dunbar

    Wednesday, July 11, 2012 4:32 PM