Where i am going wrong?

Welcome Forums General PowerShell Q&A Where i am going wrong?

Viewing 6 reply threads
  • Author
    Posts
    • #216132
      Participant
      Topics: 1
      Replies: 2
      Points: 23
      Rank: Member

      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.

    • #216150
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      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!

    • #216321
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      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?

      • This reply was modified 1 month, 3 weeks ago by Rob Simmers.
    • #216606
      Participant
      Topics: 1
      Replies: 2
      Points: 23
      Rank: Member

      Thanks, @Rob, and @Doug

      here is a link for the Kaz_Inventory file(https://dropmefiles.com.ua/LDs8P), and regarding AssetIT.csv it’s just a blank file with headers.

      Thanks in Advance

       

    • #216690
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      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!

    • #216969
      Participant
      Topics: 1
      Replies: 2
      Points: 23
      Rank: Member

      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

    • #217032
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      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)

Viewing 6 reply threads
  • You must be logged in to reply to this topic.