我有一个工作脚本,该脚本可以在远程服务器上获取本地管理员,但是有时列表中的其中一台服务器存在问题(即使Test-Connection
成功了),该服务器将挂起并停止脚本继续运行。因此,我一直在考虑将作业用作服务器超时所需的超时功能。
在我的测试脚本中,我得到了System.__ComObject
个对象的列表,这似乎很有希望,但是我无法像往常一样解析它们并得到错误:
Exception calling "InvokeMember" with "5" argument(s): "Method 'System.Management.Automation.PSCustomObject.ADSPath' not found." At c:\timeout.ps1:48 char:3 + $member.GetType().Invokemember("ADSPath","GetProperty",$null, ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : MissingMethodException
注意:对于上述错误,将对Name,Class和ADSPath重复此操作(基本上是我尝试的所有调用。
这是脚本代码:
# Test Timeout functionality
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][string]$Computer
)
$timeout = 90 ## seconds
$retryInterval = 5 ## seconds
$LocalGroupName = "Administrators"
$group = $null
$members = $null
Write-Host "Working on $($computer)..."
Write-Verbose "Start Job..."
$job1 = {
Param($computer,$localgroupname)
$group =[ADSI]"WinNT://$computer/$LocalGroupName"
@($group.psbase.Invoke("Members"))
}
Start-Job -Name "Members" $job1 -ArgumentList $computer,$localgroupname
$Check_Job = Get-Job -Name "Members"
$timer = [Diagnostics.Stopwatch]::StartNew()
while ($timer.Elapsed.TotalSeconds -lt $Timeout) {
if ($Check_Job.State -eq 'Completed') {
$members = Receive-Job -Name "Members"
break
}
Start-Sleep -Seconds $RetryInterval
Write-Verbose -Message "Waiting [$totalSecs]sec until retry..."
}
$timer.Stop()
if (!$members) {
Write-Host "No members found in the group"
} else {
foreach($member in $members) {
Write-Host "-------------------------------------"
$member.GetType().InvokeMember("Name","GetProperty",$null,$member,$null)
$member.GetType().InvokeMember("Class","GetProperty",$null,$member,$null)
$member.GetType().InvokeMember("ADSPath","GetProperty",$null,$member,$null)
}
}
我在做什么错了?