Tom_P Posted July 1, 2021 Posted July 1, 2021 Hello! Powershell beginner here.. I have set the below PS script to run on login and outputs computer information to a CSV file. This is working well however it grows out of control quite quickly. What can i add to this so that rather than adding a new line at every login, it searches for the computer name and updates the rest of the information. This way there will be one entry for each PC with the most up to date information. $obj = New-Object PSObject $obj | Add-Member NoteProperty "Computer" $env:computername $obj | Add-Member NoteProperty "LastReboot"((Get-WmiObject win32_operatingsystem -ComputerName $env:computername | select @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}})).LastBootUpTime $obj | Add-Member NoteProperty "WindowsVersion"(Get-WmiObject -class Win32_OperatingSystem -ComputerName $env:computername ).Caption $obj | Add-Member NoteProperty "WindowsBuild"(get-wmiobject -class win32_OperatingSystem -ComputerName $env:computername ).BuildNumber $obj | Add-Member NoteProperty "Make" (Get-WmiObject Win32_ComputerSystem -ComputerName $env:computername | Select-Object -Property Manufacturer).Manufacturer $obj | Add-Member NoteProperty "Model" (Get-WmiObject Win32_ComputerSystem -ComputerName $env:computername | Select-Object -Property Model).Model $obj | Add-Member NoteProperty "SerialNumber" (Get-WmiObject Win32_BIOS -ComputerName $env:computername| Select-Object -Property SerialNumber).SerialNumber $obj | Add-Member NoteProperty "OU" (Get-ADComputer -Identity $env:computername | Select-Object DistinguishedName).DistinguishedName $obj | Export-CSV -Force -NoTypeInformation -Append "\\SERVER\Logs\Computer Info\ComputerInfo.csv" Any help on this is greatly appreciated!
HPlum78 Posted July 1, 2021 Posted July 1, 2021 In my mind you are going to have to import the csv and then foreach loop it, at logon mind its going to be resource hungry...
dmj Posted July 1, 2021 Posted July 1, 2021 comparison operators: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.1&viewFallbackFrom=powershell-3.0 match, replace 1
HPlum78 Posted July 1, 2021 Posted July 1, 2021 $csv = import-csv C:\localdata\serverinfo.csv $out = New-Object System.Collections.ArrayList foreach ($row in $CSV){ if ($obj.Computer -eq $roW.computer){ Write-Host -for yellow "updating details for $($obj.Computer)" $row.WindowsVersion = "Server 2012 my very own version build" } $out.Add($row) } $out | Export-CSV -Force -NoTypeInformation "C:\localdata\serverinfo.csv" thats what I have in my mind, needs altering to do the correct matches but is a starter for 10 (I hope) 1
Tom_P Posted July 2, 2021 Author Posted July 2, 2021 Thank you @dmj & @HPlum78! Its something i will only be running periodically so not too worried about resources etc.
chaplic Posted July 2, 2021 Posted July 2, 2021 Don't do this - you'll end up with a corrupt CSV when two computers write the file at the same time Better to output unique files and don't append. Then write a collector program. Export-CSV -Force -NoTypeInformation "\\SERVER\Logs\Computer Info\$($ENV:COMPUTERNAME + ".csv") 1
Tom_P Posted July 5, 2021 Author Posted July 5, 2021 Don't do this - you'll end up with a corrupt CSV when two computers write the file at the same time Better to output unique files and don't append. Then write a collector program. Export-CSV -Force -NoTypeInformation "\\SERVER\Logs\Computer Info\$($ENV:COMPUTERNAME + ".csv") This would explain the issues ive been having. Thank you very much!
DaveTheTech Posted July 5, 2021 Posted July 5, 2021 I would also pull the duplicated wmi requests - othewise you are waiting for it each time. $pcWMI = gwmi win32_computersystem Then add it to the CSV using $pcWMI.Caption Create a table variable to hold all of your csv data get the computers then for each computer test its up grab the WMI info create a computer object to hold the row data add the row data to the table variable. 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now