Where i am going wrong?

Hello Everyone,

I have a CSV list of PC inventory, I tried to create a new one with specific headers all works well except Computername, it’s always empty.

Here is my script:

$f1 = import-csv c:\scan\Kaz_Inventory.csv | Select-Object Сomputername, PCLaptopmodel, Processor, RAM, Location, USERNAME, Project, Serianumber

$f1 | Foreach {
[pscustomobject] @{

Brand = $.Сomputername
Firstname = $
.Username
location = $.location
Serialnumber = $
.Serianumber
Model = $.PCLaptopmodel
notes = $
.Processor += @(‘,’,$_.RAM )

}

} | Export-csv c:\scan\assetsIT.csv -UseCulture -NoTypeInformation

And also one question, how can I know when one property is empty to not put a comma, cause In my script it always exists.

We’d need to see the contents of Kaz_Inventory.csv to really be able to tell you why computername comes up blank. It stands to reason the CSV column header is a different name or formatting.

As far as the comma, you absolutely want that comma. If it’s not there, that column for that row (where the property is empty) would be populated by the value from the subsequent column. Each column thereafter would also be skewed for those rows. If you have an 8 column CSV and only the 3rd has a value, it should be ,value, so that it can be imported/consumed properly.

I hope this helps!

Take a look at calculated properties. Select-Object is created a new object, so you are getting the CSV creating a new object and then again creating a new object with the [pscustomobject] accelerator:

$csv = Import-Csv -Path  c:\scan\Kaz_Inventory.csv | 
       Select-Object -Property @{Name='Brand';Expression={$_.Сomputername}}, 
                               @{Name='Model';Expression={$_.PCLaptopmodel}}, 
                               Processor, 
                               RAM,
                               Location, 
                               @{Name='Firstname';Expression={$_.UserName}}, 
                               Project, 
                               Serianumber,
                               @{Name='Notes';Expression={$_.Processor,$_.RAM -join ',')}}

As @Doug eluded, we can’t assist you without getting a sample of what is being imported. Does the CSV have a header named Computername? Is it possibly Computer_Name, PCName or something else?

Thanks, @Rob, and @Doug

here is a link for the Kaz_Inventory file(DropMeFiles - безкоштовний і швидкий файлообмінник), and regarding AssetIT.csv it’s just a blank file with headers.

Thanks in Advance

 

That is quite strange. My guess is it has something to do with language/culture/etc. I also got blank computername property until i copied the column header from the kas_inventory file and pasted it on top of the existing $.computername - then it worked. Here is what I used. Also, you have win10 under the processor column, nothing in the RAM column, and I have no idea what notes = $.Processor += @(‘,’,$_.RAM ) is supposed to accomplish.

 

import-csv c:\users\doug\downloads\Kaz_Inventory.csv | Foreach {
[pscustomobject] @{
Brand = $($_.Computername)
Firstname = $_.Username
location = $_.location
Serialnumber = $_.Serianumber
Model = $_.PCLaptopmodel
notes = $_.Processor += @(‘,’,$_.RAM )

}
} | Export-csv c:\temp\assetsIT.csv -UseCulture -NoTypeInformation

 

I hope this helps!

Thanks @Doug

I forgot to put some data in the RAM column, there was information about the RAM capacity (8GB and 16GB).

To the column of Notes, I need to list the processor and RAM through the comma, but sometimes this information is missing and therefore only a comma is put, I would like if there is no information about the RAM there would be no comma either

If that column has a header (which it should) you’d call it just like the rest. $_.thatfield and if it has data it will output, and if it doesn’t it wouldn’t output anything. Can you confirm by copying the computername header from the kas_inventory to this command, it now populates the computername data? (there is one row without a computername)