2015-09-28 54 views
1

我想從xml通過PowerShell獲取我將使用的變量。請參閱下面的示例代碼。如何從使用PowerShell的xml元素獲取變量名

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> 

這僅僅是一個示例代碼。我在我的PowerShell中有3個變量,我想從我的XML文件中獲取要使用的變量。你能給我一個關於我將如何做到這一點的暗示。正如你可以看到我的上述代碼的結果是$Hostname | $IPAddress文本,而不是$ Hostname和$ IPAddress的值。謝謝。

我想使用-split並將其放在數組上。

回答

2

使用ExpandString功能:

$ExecutionContext.InvokeCommand.ExpandString($configfile.Management.PrintMe) 
+0

由於它工作^ _ ^ –