1

I want to get the variable I will use from xml thru powershell. Please see my sample code below.

Powershell :

$Hostname = (Get-WmiObject Win32_Computersystem).name
$IPAddress = ((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
$Date=Get-Date -format "yyyy-MM-dd HH:mm:ss"

[xml]$ConfigFile = Get-Content "C:\Settings.xml"

Write-Host $ConfigFile.Management.Printme

XML :

<?xml version="1.0"?>
<Management>
    <Printme>
        $Hostname | $IPAddress
    </Printme>
</Management>

This is a sample code only. I have 3 variable on my powershell and I want to get what variable to use from my xml file. Can you give me a hint on how I will do this. As you can see the result of my above code is $Hostname | $IPAddress text instead of the value of $Hostname and $IPAddress. Thank you.

I am thinking to use -split and put it on array.

1 Answer 1

2

Use the ExpandString function:

$ExecutionContext.InvokeCommand.ExpandString($configfile.Management.PrintMe)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.