Loop through csv query get-wmiobject for multiple machines

hI,

i am able to query a machine and return the info required but i cannot get it to loop through the csv and query all machines in the file.

here is what i have so far.

$path = "C:\Users\***\Desktop\Serverasset\computers.csv"
Import-csv -path $path |
foreach-object `
{

$bios = Get-WmiObject -class Win32_BIOS -ComputerName $_.computername
$Proc = Get-WmiObject -class Win32_processor -ComputerName $_.computername | Select-Object -First 1
$memory = Get-WmiObject -class Win32_physicalmemory -ComputerName $_.computername
$system= Get-WmiObject -class Win32_ComputerSystem -ComputerName $_.computername
$lastpatch = Get-WMIObject -Class Win32_QuickFixEngineering -ComputerName $_.computername -Filter "HotFixID != 'File 1'"| ? {$_.InstalledON} |sort InstalledOn | select -last 1

}

$Object = New-Object PSObject -Property @{
'ComputerName'           = $proc.SystemName
'Manufacturer'           = $bios.Manufacturer
'Model'                  = $system.Model
'BIOS Version'         = $bios.Version
'Serial Number'        = $bios.SerialNumber
'Processor Number'     = $system.NumberOfProcessors
'Processor Name'       = $proc.name
'Logical Processeur'   = $system.NumberOfLogicalProcessors
'Speed (MHZ)'          = $proc.CurrentClockSpeed
'RAM (GB)'             = $system.TotalPhysicalMemory / 1GB -as [int]
'Used RAM slot'        = $memory.count
'WinUpdates up to date'= $lastpatch.InstalledOn

}

$Object | Export-Csv -Path C:\Users\aflook\Desktop\Serverasset\asset.csv -NoTypeInformation


an help would be appreciated.

many thanks
Adam FlookSenior Technical Services EngineerAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
You're not creating the output object inside the loop.
This one has error handling thrown in for good measure:
$PathIn = "C:\Users\***\Desktop\Serverasset\computers.csv"
$PathOut = "C:\Users\aflook\Desktop\Serverasset\asset.csv"
Import-csv -Path $PathIn | ForEach-Object {
	Try {
		Write-Host "Processing $($_.ComputerName)"
		$Failed = $Bios = $Proc = $Memory = $System = $LastPatch = $Null
		$Bios =			Get-WmiObject -Class Win32_BIOS -ComputerName $_.computername -ErrorAction Stop
		$Proc =			Get-WmiObject -Class Win32_Processor -ComputerName $_.computername -ErrorAction Stop | Select-Object -First 1
		$Memory =		Get-WmiObject -Class Win32_PhysicalMemory -ComputerName $_.computername -ErrorAction Stop
		$System =		Get-WmiObject -Class Win32_ComputerSystem -ComputerName $_.computername -ErrorAction Stop
		$LastPatch =	Get-WMIObject -Class Win32_QuickFixEngineering -ComputerName $_.computername -Filter "HotFixID != 'File 1'" -ErrorAction Stop |
			Where-Object {$_.InstalledON} | Sort-Object -Property InstalledOn -Descending | Select-Object -First 1
	} Catch {
		$Failed = $_.Exception.Message
	}
	New-Object PSObject -Property @{
		'ComputerName' =			$Proc.SystemName
		'Manufacturer'=				$Bios.Manufacturer
		'Model' =					$System.Model
		'BIOS Version' =			$Bios.Version
		'Serial Number' =			$Bios.SerialNumber
		'Processor Number' =		$System.NumberOfProcessors
		'Processor Name' =			$Proc.name
		'Logical Processeur' =		$System.NumberOfLogicalProcessors
		'Speed (MHZ)' =				$Proc.CurrentClockSpeed
		'RAM (GB)'	=				$System.TotalPhysicalMemory / 1GB -as [int]
		'Used RAM slot' =			$Memory.count
		'WinUpdates up to date' =	$LastPatch.InstalledOn
		'Failed' =					$Failed
	}
} | Export-Csv -Path $PathOut -NoTypeInformation

Open in new window

0
 
Adam FlookSenior Technical Services EngineerAuthor Commented:
brilliant that works a treat!! many thanks
0
 
Adam FlookSenior Technical Services EngineerAuthor Commented:
works perfectly
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.