将参数传递给函数

时间:2017-03-01 19:41:16

标签: sql powershell sccm

我试图通过函数传递数组中的每个值。这就是我所拥有的:

#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)
{

} 

我似乎无法弄清楚如何从$ sccmComputers获取每个项目来传递函数,如果返回true则将值输出到控制台。

以下代码对我有用:

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

1 个答案:

答案 0 :(得分:0)

foreach ($pc in $sccmComputers)
{
    CheckDisposal $pc
}