[xml] question

This topic contains 5 replies, has 3 voices, and was last updated by  ertuu85 1 year, 10 months ago.

  • Author
    Posts
  • #32383

    ertuu85
    Participant

    I've created an xml file that contains file paths, some of them being environmental variables.

    I'm trying to use the XML to grab and use them but when i take them from the xml it becomes a string, not a cmdlet...is there a way to use them as a command and not a string?

    [xml]$spec = get-content .\FILE.xml
    
    $dv = ($spec.techspec.section | ? {$_.systemvalueparameter -eq '$env:SystemRoot'}).systemvalueparameter
    

    so now $dv will equal "$env:systemroot", but if i test-path $dv it fails..

  • #32384

    Dave Wyatt
    Moderator

    So $spec.techspec.section contains the name of a cmdlet? If so, you'd just use PowerShell's Call operator (&), which allows you to execute strings or ScriptBlock objects. For example:

    $command = 'Get-Command'
    
    & $command -Name Get-Help -Syntax
    
  • #32385

    ertuu85
    Participant

    No, I'm sorry...I'm explaining this horribly...

    $dv = ($spec.techspec.section | ? {$_.systemvalueparameter -eq '$env:SystemRoot'}).systemvalueparameter
    

    This would assign $dv the value of $env:systemroot (which is the value coded into the xml doc on the systemvalueparameter element).

    So if I did like a

    test-path $dv
    

    It would retrun "False" because it's really doing a test-path '$env:systemroot' instead of a test-path $env:systeroot or test-path "$env:systermroot"

    I would like it to return the value of $env:systemroot, not the literal string '$env:systemroot'

  • #32386

    Curtis Smith
    Participant

    It would help if you have an example of your XML document

  • #32388

    Dave Wyatt
    Moderator

    Oh, I see. There's a method available in the PowerShell engine to expand variables in strings like that:

    $myString = '$env:systemRoot'
    
    $ExecutionContext.InvokeCommand.ExpandString($myString)
    
  • #32390

    ertuu85
    Participant

    Dave! That works like a dream, thanks!

You must be logged in to reply to this topic.