Author Posts

July 10, 2014 at 10:48 am

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 

July 10, 2014 at 12:37 pm

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.