I'm trying to pass each value in an array through a function. This is what I have:
#Pulls a list of computers from SCCM
$sccmComputers = Get-ComputersCollection -CollectionName "All Desktops"
#Function to check each item in $sccmComputers against a database.
Function CheckDisposal ($pc){
$check = invoke-sqlcmd2 -ServerInstance shopserv-lr -Database inventory -query "SELECT * FROM Computers WHERE ServiceTag = '$pc' and Status = 'Disposed' "
return $check}
foreach ($pc in $sccmComputers)
{
}
I can't seem to figure out how to get each of the items from $sccmComputers to pass through the function and if returns true output the value to the console.
Below is the code that worked for me:
Function CheckDisposal ($a){
$check = invoke-sqlcmd2 -ServerInstance shopserv-lr -Database inventory -query "SELECT * FROM Computers WHERE Name = '$a' and Status = 'Disposed'"
return $check
}
$sccmComputers = Get-ComputersCollection -CollectionName "All Desktops"
foreach($item in $sccmComputers)
{
$dispose = CheckDisposal $item.Name
if($dispose)
{
Write-Output $dispose.Name
}
}