我无法使用以下代码获取格式正确的XML文件:
# Get Computer System info
$CompInfo = Get-WmiObject Win32_ComputerSystem
$CompName = $CompInfo.Name
# Get IP Address
$IPAddressTemp = Test-Connection $CompName -count 1 | select Address,Ipv4Address
$IPAddress = $IPAddressTemp.IPV4Address
# Begin Writing values to XML Document
$filePath = "C:\Temp\$CompName.$IPAddress.xml" # Set the File Name
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$Null) # Create The Document
# Set The XML Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Start the XML Document
$xmlWriter.WriteStartDocument()
# Set the XSL
$XSLPropText = "type='text/xsl' href='style.xsl'"
$xmlWriter.WriteProcessingInstruction("xml-stylesheet", $XSLPropText)
$xmlWriter.WriteStartElement("Server") # Write Root Element
# Write the Basic System Elements to the XML Document
$xmlWriter.WriteStartElement("SystemInfo")
$xmlWriter.WriteElementString("Hostname","$CompName")
$xmlWriter.WriteElementString("IPAddress","$IPAddress")
$xmlWriter.WriteStartElement("Drives") # Add drive information as individual elements
# Get drive information and add each to XML
$logicalDisk = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" ComputerName localhost
$count=0
foreach($disk in $logicalDisk)
{
$diskObj = "" | Select-Object Disk,Size,FreeSpace
$diskObj.Disk = $disk.DeviceID
$diskObj.Size = "{0:n0} GB" -f (($disk | Measure-Object -Property Size -Sum).sum/1gb)
$diskObj.FreeSpace = "{0:n0} GB" -f (($disk | Measure-Object -Property FreeSpace -Sum).sum/1gb)
$DriveLetter = $diskObj.Disk
$DriveSize = $diskObj.Size
$DriveFreeSpace = $diskObj.FreeSpace
$xmlWriter.WriteStartElement("Disk$count") # Add tag for each drive
$xmlWriter.WriteElementString("DriveLetter","$DriveLetter") # Write Drive Letter to XML
$xmlWriter.WriteElementString("DriveSize","$DriveSize") # Write Drive Size to XML
$xmlWriter.WriteElementString("DriveFreeSpace","$DriveFreeSpace") # Write Drive Free Space to XML
$xmlWriter.WriteEndElement # <-- Closing Drive Tag
$count++
}
# Write Close Tag for Drives and SysInfo elements
$xmlWriter.WriteEndElement # <-- Closing Drives Tag
$xmlWriter.WriteEndElement # <-- Closing SystemInfo
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # <-- Closing Server
# End the XML Document
$xmlWriter.WriteEndDocument()
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
XML输出如下所示:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<Server>
<SystemInfo>
<Hostname>TEST-SERVER</Hostname>
<IPAddress>10.0.0.10</IPAddress>
<Drives>
<Disk0>
<DriveLetter>C:</DriveLetter>
<DriveSize>223 GB</DriveSize>
<DriveFreeSpace>34 GB</DriveFreeSpace>
<Disk1>
<DriveLetter>D:</DriveLetter>
<DriveSize>931 GB</DriveSize>
<DriveFreeSpace>547 GB</DriveFreeSpace>
<Disk2>
<DriveLetter>E:</DriveLetter>
<DriveSize>466 GB</DriveSize>
<DriveFreeSpace>466 GB</DriveFreeSpace>
<Disk3>
<DriveLetter>G:</DriveLetter>
<DriveSize>931 GB</DriveSize>
<DriveFreeSpace>159 GB</DriveFreeSpace>
</Disk3>
</Disk2>
</Disk1>
</Disk0>
</Drives>
</SystemInfo>
</Server>
这就是我需要出现的方式:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<Server>
<SystemInfo>
<Hostname>TEST-SERVER</Hostname>
<IPAddress>10.0.0.10</IPAddress>
<Drives>
<Disk0>
<DriveLetter>C:</DriveLetter>
<DriveSize>223 GB</DriveSize>
<DriveFreeSpace>34 GB</DriveFreeSpace>
</Disk0>
<Disk1>
<DriveLetter>D:</DriveLetter>
<DriveSize>931 GB</DriveSize>
<DriveFreeSpace>547 GB</DriveFreeSpace>
</Disk1>
<Disk2>
<DriveLetter>E:</DriveLetter>
<DriveSize>466 GB</DriveSize>
<DriveFreeSpace>466 GB</DriveFreeSpace>
</Disk2>
<Disk3>
<DriveLetter>G:</DriveLetter>
<DriveSize>931 GB</DriveSize>
<DriveFreeSpace>159 GB</DriveFreeSpace>
</Disk3>
</Drives>
</SystemInfo>
</Server>
有人能告诉我我做错了吗?
由于
答案 0 :(得分:2)
您在()
末尾错过了一组括号($XmlWriter.WriteEndElement
):
$xmlWriter.WriteStartElement("Disk$count") # Add tag for each drive
$xmlWriter.WriteElementString("DriveLetter","$DriveLetter") # Write Drive Letter to XML
$xmlWriter.WriteElementString("DriveSize","$DriveSize") # Write Drive Size to XML
$xmlWriter.WriteElementString("DriveFreeSpace","$DriveFreeSpace") # Write Drive Free Space to XML
$xmlWriter.WriteEndElement() # <-- Closing Drive Tag - don't forget the the ()
如果没有()
,它只返回带有方法签名的PSMethod
对象,但它永远不会被执行,这就是为什么你的{{1} }子元素保持嵌套