[xml] question

Welcome Forums General PowerShell Q&A [xml] question

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
2 years, 10 months ago.

  • Author
    Posts
  • #32383

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

  • #32388

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Dave! That works like a dream, thanks!

The topic ‘[xml] question’ is closed to new replies.