如何在其中使用powershell显示XML标签?

时间:2011-12-22 11:24:49

标签: xml powershell powershell-v2.0

我有以下powershell脚本

$MyVM= [XML] @"
<VM>
  <IPAddress>X.Y.Z.T</IPAddress>
  <VMName>R2-VM</VMName>
  <Vsphere-host>X.X.X.X</Vsphere-host>
  <OS>W2K8-R2</OS>        
  <AssignedTo>Tester</AssignedTo>
</VM>
"@

Write-host " Your VM details"
Write-host "------------------"
Write-host "IPaddress: " $MyVM.VM.IPAddress
Write-host "Host Machine IP:" $MyVM.VM.Vsphere-host

它不是显示Vsphere主机信息,而是显示如下

 Host Machine IP: System.Xml.XmlDocument.VM.Vsphere-host

如何让它发挥作用?

1 个答案:

答案 0 :(得分:4)

Vsphere-host放在引号之间,如下所示:"Vsphere-host"

$MyVM= [XML] @"
<VM>
  <IPAddress>X.Y.Z.T</IPAddress>
  <VMName>R2-VM</VMName>
  <Vsphere-host>X.X.X.X</Vsphere-host>
  <OS>W2K8-R2</OS>        
  <AssignedTo>Tester</AssignedTo>
</VM>
"@

Write-host " Your VM details"
Write-host "------------------"
Write-host "IPaddress: " $MyVM.VM.IPAddress
Write-host "Host Machine IP:" $MyVM.VM."Vsphere-host"