ive written a powershell script that runs at pc startup to grab various inventory details from a pc and save it to a scv file on a server. In testing it works fine but when deployed i keep finding that after a few days he csv file is empty and nothing will add data to it thought the date stamp on the fie updates. the code is below im reasonably convinced its something in the section that adds altered data back to the file rather than the sections that create a new file or add a new line if a line for that pc dosent exist as the main place this happens is on a bunch of pcs thats ive tested and all have existing data(have #'d that section out as a test in one location im trying it but awaiting results).
Im new to powershell having done a bi of vbs scripting in the past but the code looks sensible to me (granted it probably not the best bit of coding ever but i can follow it all easily) i just cant work out why it every few days chucks a wobbler and writes an empty file
# system info ps v1
#locate output file & define default values
$filepath="\\server\share$\outputfile.csv"
$dop="-"
$warantyyears="-"
$warantyend="-"
$location = "-"
$file=""
$csvout=""
$arrayloc=""
$newline=""
#import existing file if it exists
if(test-path -path $filepath)
{
$fileexists="true"
$file = Import-Csv $filepath
}
if(!(test-path -path $filepath))
{
$fileexists="false"
}
# get mac address active cards (tested on multinics)
$mac= Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"' | Select macAddress
if ($mac.count) {$mac3=$mac.macaddress[0] + " : " + $mac.macaddress[1]} else {$mac3=$mac.macaddress}
# get processor and pc name
$procinfo = Get-WmiObject Win32_Processor
$cpuname=$procinfo.Name
$pcname=$procinfo.SystemName
# get memory info/model/make
$pcinfo = Get-WmiObject Win32_ComputerSystem
$maker=$pcinfo.Manufacturer
$model=$pcinfo.Model
$ram=$pcinfo.TotalPhysicalMemory/1024/1024/1024
$ram = "{0:N1}" -f $ram
#get serial no
$serial=Get-WmiObject Win32_BIOS
$serial2=$serial.SerialNumber
#hdd size and free in gb rounded to nearest whole no
$hdddata=Get-WmiObject Win32_LogicalDisk -Filter 'deviceid="c:"'
$freespace=$hdddata.FreeSpace/1024/1024/1024
$freespace = "{0:N0}" -f $freespace
$totalspace=$hdddata.size/1024/1024/1024
$totalspace = "{0:N0}" -f $totalspace
# get windows version
$os=Get-WmiObject Win32_OperatingSystem | select caption
$os2=$os.caption
#date-time
$now= Get-Date
$now2 = $now.date
#create csv data if no existing file
if ($fileexists -eq "false")
{
#echo "new file created"
$csvout = New-Object psobject
$csvout | add-member NoteProperty Macaddress $mac3
$csvout | add-member NoteProperty PCName $pcname
$csvout | add-member NoteProperty Manufacturer $Maker
$csvout | add-member NoteProperty "PC Model" $model
$csvout | add-member NoteProperty Processor $CPUName
$csvout | add-member NoteProperty Memory $ram
$csvout | add-member NoteProperty "HDD Size" $totalspace
$csvout | add-member NoteProperty "HDD Free" $freespace
$csvout | add-member NoteProperty "Serial No" $serial2
$csvout | add-member NoteProperty "Windows Version" $os2
$csvout | add-member NoteProperty "Date Collected" $now2
$csvout | add-member NoteProperty "Date purchased" $dop
$csvout | add-member NoteProperty "Warranty years" $warantyyears
$csvout | add-member NoteProperty "Warranty Expiration" $warantyend
$csvout | add-member NoteProperty "Location" $location
$csvout | export-csv $filepath -notypeinformation #-Append
}
if($fileexists -eq "true")
{
$arrayloc=[array]::indexof($file.pcname, $pcname)
if (!($arrayloc -eq -1))
{
#echo "replacing data"
#$file[$arrayloc] |Format-Table
$file[$arrayloc].macaddress=$mac3
$file[$arrayloc].pcname= $pcname
$file[$arrayloc].Manufacturer=$maker
$file[$arrayloc]."pc model"=$model
$file[$arrayloc].processor=$cpuname
$file[$arrayloc].memory=$ram
$file[$arrayloc]."hdd size" = $totalspace
$file[$arrayloc]."hdd free" = $freespace
$file[$arrayloc]."serial no" = $serial2
$file[$arrayloc]."Windows Version" = $os2
$file[$arrayloc]."Date Collected" = $now2
$file | export-csv $filepath -notypeinformation
}
if ($arrayloc -eq -1)
{
#echo "adding new line"
$csvout = New-Object psobject
$csvout | add-member NoteProperty Macaddress $mac3
$csvout | add-member NoteProperty PCName $pcname
$csvout | add-member NoteProperty Manufacturer $Maker
$csvout | add-member NoteProperty "PC Model" $model
$csvout | add-member NoteProperty Processor $CPUName
$csvout | add-member NoteProperty Memory $ram
$csvout | add-member NoteProperty "HDD Size" $totalspace
$csvout | add-member NoteProperty "HDD Free" $freespace
$csvout | add-member NoteProperty "Serial No" $serial2
$csvout | add-member NoteProperty "Windows Version" $os2
$csvout | add-member NoteProperty "Date Collected" $now2
$csvout | add-member NoteProperty "Date purchased" $dop
$csvout | add-member NoteProperty "Warranty years" $warantyyears
$csvout | add-member NoteProperty "Warranty Expiration" $warantyend
$csvout | add-member NoteProperty "Location" $location
$csvout | export-csv $filepath -notypeinformation -Append
}
}