Issue reading a file into an inventory script

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 3 years, 1 month ago.

  • Author
  • #16985

    Mike Hylek

    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{
            [String]$logFile = 'C:\errorlog.txt'
                    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)}
                    '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 
  • #16996

    Don Jones

    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...


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


    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.

You must be logged in to reply to this topic.