Jump to content

PS - How do I search and update an entry rather than adding a new line to csv


Recommended Posts

Posted

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! :)

Posted

$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)

  • Thanks 1
Posted

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")

  • Thanks 1
Posted
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!

Posted

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.

  • Thanks 1

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...