2

i'm trying to save information gathered about the local computer to an XML-file. Everything seems to be running fine, but I get this error about Strings only beeing able to be set as XmlNode properties, but for example:

Any ideas on how to solve it?

PS C:\Users\Hkon> $newdrive.name = $_.name

results in this error:

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

Here is my code:

$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
2
  • If you do $_.Name | Get-Member, what is it's type? Commented Mar 30, 2012 at 13:58
  • I guess that you are probably right. Might potentially be String[] or some other collection as well. Commented Mar 30, 2012 at 14:18

1 Answer 1

4

I have tested this code ( excluding last 2 lines ) and I have to cast

$_volumename, $_.size, and $_.frespace 

as [string]

but all my volumes got a name (a: c: d: e: f: g: and so on... )

Can be that some unmounted volume is present in system are you testing?

Try to cast also

$newdrive.name = [string]$_.name 

Here the result after executions of your code adding cast:

Powershell Console

Where gwmi return no value I had error without the cast to [string]

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.