$fname = "C:\pcinfo.txt"
$CPU = Get-WmiObject -Class win32_processor
$MB = Get-WmiObject -Class win32_baseboard
$MEM = Get-WmiObject Win32_PhysicalMemory
$DD = Get-PhysicalDisk
$pcn = Get-WmiObject -Class Win32_ComputerSystem
#имя компьютера
"pcname: "+$pcn.Name | Out-File -FilePath $fname -Append -Encoding Default
#IP-адрес компьютера по его имени
Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionStatus=2' |
ForEach-Object {
$pcip = 1 | Select-Object IP
$config = $_.GetRelated('Win32_NetworkAdapterConfiguration')
$pcip.IP = $config | Select-Object -expand IPAddress
$pcip
}
foreach($aip in $pcip) {
"IP: "+$aip.IP | Out-File -FilePath $fname -Append -Encoding Default
}
#имя активного пользователя
"username: "+$env:UserName | Out-File -FilePath $fname -Append -Encoding Default
@'
<?xml version="1.0" encoding="UTF-8"?>
<root>
<pc>
<ip>1.1.1.1</ip>
<name>2112</name>
<userName>admin</userName>
</pc>
<pc>
<ip>1.1.1.2</ip>
<name>2113</name>
<userName>admin</userName>
</pc>
</root>
'@ | out-file d:\tmp\pcExampleFile.xml
class ComputerInfo {
[String]$name
[String]$IPAddress
[String]$userName
ComputerInfo() {
$this.name = $env:COMPUTERNAME
$this.IPAddress = ((Get-NetIPAddress).where{ $_.AddressFamily -eq 'IPv4' }).IPAddress -join ", "
$this.userName = $env:USERNAME
}
}
[xml]$pcXML = Get-Content d:\tmp\pcExampleFile.xml
$ComputerInfoInstance = [ComputerInfo]::New()
if (($thisPCNode = $pcXML.SelectSingleNode("//pc[name=`"$($ComputerInfoInstance.name)`"]"))){
$thisPCNode.ip = $ComputerInfoInstance.IPAddress
$thisPCNode.userName = $ComputerInfoInstance.userName
}
else {
$nodePC = $pcXML.root.AppendChild($pcXML.CreateElement("pc"))
$nodeIPAddress = $pcXML.CreateElement("IPAddress")
$nodeIPAddress.InnerText = $ComputerInfoInstance.IPAddress
[void]$nodePC.AppendChild($nodeIPAddress)
$nodeName = $pcXML.CreateElement("name")
$nodeName.InnerText = $ComputerInfoInstance.name
[void]$nodePC.AppendChild($nodeName)
$nodeUserName = $pcXML.CreateElement("UserName")
$nodeUserName.InnerText = $ComputerInfoInstance.userName
[void]$nodePC.AppendChild($nodeUserName)
}
$pcXML.Save("d:\tmp\pcExampleFile.xml")