# Issue reading a file into an inventory script

**URL:** <https://forums.powershell.org/t/issue-reading-a-file-into-an-inventory-script/2806>\
**Category:** PowerShell Help\
**Created:** [July 10, 2014, 5:18am UTC](https://forums.powershell.org/t/issue-reading-a-file-into-an-inventory-script/2806 "2014-07-10T05:18:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike-hylek](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mike-hylek](https://forums.powershell.org/u/mike-hylek)\
**Post date:** [July 10, 2014, 5:18am UTC](https://forums.powershell.org/t/issue-reading-a-file-into-an-inventory-script/2806/1 "2014-07-10T05:18:26Z")

</div>

Im having an issue reading a .CSV file of computer names into a powershell script that i wrote. I want the script to read in the PC names and then export a .CSV file with all of the inventory fields populated. Here is the working code that i have so far:

```
function Get-CompInfo{
    [cmdletBinding()]
        param(
        [string[]]$ComputerName,
        [Switch]$errorLog,
        [String]$logFile = 'C:\errorlog.txt'
        )

        Begin{
            If($errorLog){
                Write-Verbose 'Error logging turned on'
            } Else {
                write-verbose 'Error logging turned off'
            }
            Foreach($c in $ComputerName){
            $os=Get-Wmiobject -ComputerName $c -Class win32_computersystem
            $disk=Get-WmiObject -ComputerName $c -Class Win32_logicalDisk -Filter "DeviceID='C:'"
            $vid=gwmi -ComputerName $c -class win32_VideoController  
            $bios=gwmi -ComputerName $c -Class win32_bios
            $hdd=gwmi -ComputerName $c -class Win32_LogicalDisk
            $mem=gwmi -ComputerName $c -class win32_physicalmemory |Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 1GB),2)}

                $prop=[ordered]@{
                'computerName'=$c;
                'Service tag'=$bios.serialnumber;
                'PC Model'=$os.model;
                'Hard Disk Size'=$disk.size / 1GB -as [int];
                'Video Card'=$vid.description
                'Memory (GB)'= $mem }
            New-Object -TypeName psobject -Property $prop 
            Write-Output $obj
            
            }
           
        }
}
```

I run it using

```
Get-CompInfo -ComputerName receptionist
```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [July 10, 2014, 7:07am UTC](https://forums.powershell.org/t/issue-reading-a-file-into-an-inventory-script/2806/2 "2014-07-10T07:07:00Z")

</div>

So, perhaps you’re actually having a “problem” rather than an “issue,” but you don’t indicate what that problem is.

Assuming you have a CSV file with a computername column…

```
ComputerName,Whatever,Something
SERVER1,Blah,Bleh
SERVER2,Foo,Bar
```

If you define your parameter accept pipeline input by property name…

```
[Parameter(ValueFromPipelineByPropertyName=$True)][string[]]$ComputerName
```

Then you can…

```
Import-CSV myfile.csv | Get-CompInfo | Export-CSV myoutput.csv
```

However, your function’s code needs to be inside a PROCESS block, not a BEGIN block as you have it. Functions that accept pipeline input must process that input in PROCESS; the BEGIN block executed prior to pipeline parameter binding occurring.

You have some other problems. First, Win32\_LogicalDisk, and possibly Win32\_VideoController, can return multiple objects. You aren’t properly dealing with that, so you’re open to unexpected output. For example, you can’t do math on the Size property when $disk has more than one object in it.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:47pm UTC](https://forums.powershell.org/t/issue-reading-a-file-into-an-inventory-script/2806/3 "2024-05-16T20:47:51Z")

</div>

Closed on May 16, 2024, 8:47pm UTC


