S
S
StenMarsh13372020-12-16 15:41:51
PowerShell
StenMarsh1337, 2020-12-16 15:41:51

Write PC info to text file via PowerShell?

Hello everyone)
It is necessary for the script to collect information about the PC and write it to a text file, but you need to make sure that the script does not write information if such a computer is already in the file
5fd9fef0c03e4313001349.png

$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

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Korotenko, 2020-12-16
@firedragon

As always, there is an easy way and a hard way. The first is to write a lock file with the name of the computer, if there is no name in the directory, then add . The second is a variation, only the write goes to the file line by line, and is read. The third option is a local database or xml, you read what is there and do not write if it matches

M
MaxKozlov, 2020-12-16
@MaxKozlov

read the file before writing something there and do not write if you find a line about "pcname: xxx" in it

A
alhaos, 2020-12-18
@alhaos

@'
<?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")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question