我正在尝试将收集的有关本地计算机的信息保存到XML文件中。一切似乎运行正常,但我得到关于Strings的错误,只能将其设置为XmlNode属性,但例如:
关于如何解决它的任何想法?
PS C:\Users\Hkon> $newdrive.name = $_.name
导致此错误:
Cannot set "name" because only strings can be used as values to set XmlNode properties.
At line:1 char:11
+ $newdrive. <<<< name = $_.name
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
这是我的代码:
$template = "<computer version='1.0'>
<hardware>
<serialnr>$serialnr</serialnr>
<systeminfo>
<name></name>
<domain></domain>
<manufacturer></manufacturer>
<model></model>
<bitversion></bitversion>
</systeminfo>
<usb>
<model></model>
</usb>
<drive>
<name></name>
<volumename></volumename>
<size></size>
<freespace></freespace>
</drive>
<memory>
<positioninrow></positioninrow>
<capacity></capacity>
<datawidth></datawidth>
<devicelocator></devicelocator>
</memory>
<gpu>
<name>$gpu</name>
<status>$status</status>
</gpu>
<cpu>
<name></name>
<manufacturer></manufacturer>
<id></id>
<numberofcores></numberofcores>
<addresswidth></addresswidth>
</cpu>
</hardware>
<software>
<printer>
<id></id>
<drivername></drivername>
<portname></portname>
</printer>
<allusers>
<user>
<name></name>
</user>
</allusers>
<osinfo>
<caption></caption>
<serialnr></serialnr>
<bitversion></bitversion>
<installdate>$NormalDateTime</installdate>
<manufacturer></manufacturer>
</osinfo>
</software>
</computer>"
$template | out-File C:\Scripts\XML\info.xml
$xml = New-Object xml
$xml.Load("C:\Scripts\XML\info.xml")
#Drive, hardware
$newdrive = (@($xml.computer.hardware.drive)[0])
Get-WmiObject -Class Win32_logicaldisk |
ForEach-Object {
$newdrive = $newdrive.clone()
$newdrive.name = $_.name
$newdrive.volumename = $_.volumename
$newdrive.size = $_.size
$newdrive.freespace = $_.freespace.toString()
$xml.computer.hardware.AppendChild($newdrive) > $null
}
$xml.computer.hardware.drive | where-object {$_.name -eq ""} | foreach-object {$xml.computer.hardware.RemoveChild($_)}
$xml.computer.hardware.drive.Count
答案 0 :(得分:4)
我已经测试了这段代码(不包括最后两行),我必须投出
$_volumename, $_.size, and $_.frespace
为[string]
但我的所有卷都有一个名称(a: c: d: e: f: g: and so on... )
您可以测试系统中是否存在某些未安装的卷?
尝试施放
$newdrive.name = [string]$_.name
执行代码添加强制转换后的结果:
如果gwmi
没有返回任何值,我会在没有投射到[string]