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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Adam FlookSenior Technical Services EngineerAuthor Commented:
brilliant that works a treat!! many thanks
0
Adam FlookSenior Technical Services EngineerAuthor Commented:
works perfectly
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.