为工作流创建功能 - Powershell
Make function for workflow - Powershell
我正在尝试加快检查磁盘 SMART 故障前状态的脚本,因为我需要检查两千台计算机。
但是,脚本仍在写出同一磁盘的状态 - 计算机 "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
谁能至少给我一个修复方法的提示?
提前致谢!
尝试更改
$WMI = Get-WMIObject -Class Win32_DiskDrive
到
$WMI = Get-WMIObject -Class Win32_DiskDrive -ComputerName $computername
看起来它可能正在从您所在的机器上检索信息,因为您还没有将计算机传递给 Get-WMIObject
cmdlet。
我正在尝试加快检查磁盘 SMART 故障前状态的脚本,因为我需要检查两千台计算机。 但是,脚本仍在写出同一磁盘的状态 - 计算机 "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
谁能至少给我一个修复方法的提示? 提前致谢!
尝试更改
$WMI = Get-WMIObject -Class Win32_DiskDrive
到
$WMI = Get-WMIObject -Class Win32_DiskDrive -ComputerName $computername
看起来它可能正在从您所在的机器上检索信息,因为您还没有将计算机传递给 Get-WMIObject
cmdlet。