我一直在编写一些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:DropboxBachelorprosjektKodeFungerende Script+kodeinfoGatherer.ps1:177 char:43
+ $xml.computer.software.AppendChild <<<< ($newnetwork) > $null
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
所以我的问题是,有没有人知道或可以编写像上面这样的代码,有效?顺便说一句:仅收集有关"激活"卡的信息是否更明智?
首先,我不确定为什么要将 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)