为工作流程创建功能 - Powershell

时间:2017-10-28 01:29:00

标签: function powershell parallel-processing workflow

我正在尝试加快脚本以检查磁盘SMART prefail状态,因为我需要检查两千台计算机。 但是,该脚本仍在写出同一磁盘的状态 - 计算机“hostname1”和“hostname2”具有不同的磁盘。

function disk-status (){
        param(
            [Parameter(Mandatory=$true)][string]$computername
            )
                $WMI = Get-WMIObject -Class Win32_DiskDrive
                ForEach ($Drive in $WMI){
                $disk = $Drive.Caption
                $status = $Drive.Status
                #condition will be changed to "-notmatch"
                if ($status -match "OK"){
                        #I'm using write-output to see if the script works during testing
                        Write-output $computername $disk $status
                        }
                }
}



workflow Get-disk-status {
    param(
    [string[]]$computers
  )
    foreach -parallel ($computer in $computers) {        
        disk-status -computername $computer

  }
}

#in the final version I'm going to use get-adcomputer
$computers = "hostname1", "hostname2"

Get-disk-status $computers

输出我得到:

hostname1
ST500LM0 21-1KJ152 SCSI Disk Device
OK
hostname2
ST500LM0 21-1KJ152 SCSI Disk Device
OK

任何人都可以给我至少一个提示如何修复它吗? 提前谢谢!

1 个答案:

答案 0 :(得分:3)

尝试更改

$WMI = Get-WMIObject -Class Win32_DiskDrive 

$WMI = Get-WMIObject -Class Win32_DiskDrive -ComputerName $computername

看起来它可能正在从您所在的计算机上检索信息,因为您还没有将计算机传递到Get-WMIObject cmdlet。