0

我一直在编写一些 powershell 代码,它应该以 XML 格式收集和存储有关计算机的信息。但我似乎无法获得网卡的 IP、MAC 和名称。

这是我编写的一些代码的示例(工作代码):

#OSInfo
$newosinfo = (@($xml.computer.software.osinfo)[0])
Get-WmiObject -computer $compname Win32_OperatingSystem | 
ForEach-Object {
        $newosinfo = $newosinfo.clone() 
        $newosinfo.caption = $_.caption
        $newosinfo.serialnumber = [string]$_.serialnumber
        $newosinfo.OSArchitecture = $_.OSArchitecture
        $newosinfo.manufacturer = $_.manufacturer
        #Time $newosinfo.installdate;
        $xml.computer.software.AppendChild($newosinfo) > $null
}
$xml.computer.software.osinfo | where-object {$_.caption -eq ""} | foreach-object {$xml.computer.software.RemoveChild($_)}

这是我尝试编写的代码(不工作的代码):

$newnetwork = (@($xml.computer.software.network)[0])
Get-WmiObject -computer $compname Win32_NetworkAdapter -Filter 'NetConnectionStatus=2' | 
ForEach-Object {
        $newnetwork = $newnetwork.clone()
        $newnetwork = 1 | Select-Object Name, MAC, IP 
        $newnetwork.Name = $_.Name
        $newnetwork.MAC = $_.MacAddress
        $IP = $_.GetRelated('Win32_NetworkAdapterConfiguration')
        $newnetwork.IP = $IP | Select-Object -expand IPAddress
        $xml.computer.software.AppendChild($newnetwork) > $null
}
$xml.computer.software.network | where-object {$_.Name -eq ""} | foreach-object {$xml.computer.software.RemoveChild($_)}

这段代码给出了这个错误:

Cannot convert argument "0", with value: "@{Name=Intel(R) 82579V Gigabit Network Connect
ion; MAC=F4:6D:04:40:DB:6F; IP=10.0.0.12}", for "AppendChild" to type "System.Xml.XmlNod
e": "Cannot convert the "@{Name=Intel(R) 82579V Gigabit Network Connection; MAC=F4:6D:04
:40:DB:6F; IP=10.0.0.12}" value of type "Selected.System.Int32" to type "System.Xml.XmlN
ode"."
At A:\Dropbox\Bachelorprosjekt\Kode\Fungerende Script+kode\infoGatherer.ps1:177 char:43
+         $xml.computer.software.AppendChild <<<< ($newnetwork) > $null
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

所以我的问题是,有没有人知道或可以编写像上面这样的代码,行得通?顺便说一句:仅收集有关“激活”卡的信息是否更聪明?

4

1 回答 1

1

首先,我不确定您为什么要将 XmlNode 包装在一个数组中,然后再展开它 - 据我所知,以下两行是等效的:

$a = (@($xml.computer.software.network)[0])
$b = $xml.computer.software.network

我发现的第二件事,你有这个奇怪的东西1 | Select Name, MAC, IP,我猜你正在用它来尝试在你的 XmlNode 上填充这些属性?您实际上正在做的是将先前从 XmlNode 克隆的 $newnetwork 对象更改为 PSCustomObject 类 - 当您尝试将其作为参数传递给 appendChild() 方法时,该类稍后会中断。

下面是一些工作的 PowerShell 代码,它们可能会满足您的需求。我正在对您的 XML 结构做出一些假设,因此您可能需要对其进行调整以满足您的确切需求:

# setup sample XML
$xml = [xml]"
  <computer><software><network>
    <Name />
    <MAC />
    <IP />
  </network></software></computer>"

# duplicate network XmlNode and grab necessary info through WMI
# note: there may be more than one IP Address bound
#       on a given adapter. Use just the first one.
$info = $xml.computer.software.network.clone()
$nic = Get-WmiObject Win32_NetworkAdapter | select -first 1
$ip = $nic.GetRelated("Win32_NetworkAdapterConfiguration") | 
    select -expand IPAddress -first 1

# populate children with correct values
$info.Name = $nic.Name
$info.MAC = $nic.MACAddress
$info.IP = [string]($ip)

# finally append new XmlNode to software node
$xml.computer.software.appendChild($info)
于 2012-04-10T23:33:39.333 回答