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