问题描述
我需要使用“调用命令”从客户端获取一些信息,并开始编写脚本以从AD查询计算机,将其放入变量,使用该变量检查哪些计算机处于联机或脱机状态,并设置状态进入哈希表,然后仅在联机计算机上运行invoke-command。在处理此问题时,我认为不使用哈希表可能会更容易,而是在执行invoke-command之前进行foreach + test-connection。因此,我克隆并重新编写。这两个脚本现在都已完成,我想知道为什么在执行“ ForEach + Test-connection”循环后运行“ invoke-command”这么慢,而在我的其他脚本中却只有一半的时间。此外-ErrorVariable也不起作用。这就是我的意思:
Measure-Command {
#########################################################################################
# Check Computerstatus and put into hashtable
#########################################################################################
$computerstatus = @{}
ForEach ($computer in $adcomputers) {
$result = Test-Connection $computer -Count 1 -quiet
If ($result) {$computerstatus.add( $computer,'online')}
else {$computerstatus.add( $computer,'offline')}
}
# Read hashtable and set Online/Offline variable:
$computersonline = Foreach ($Key in ($computerstatus.GetEnumerator() | Where-Object {$_.Value -eq "online"})) {$Key.name}
$computersoffline = Foreach ($Key in ($computerstatus.GetEnumerator() | Where-Object {$_.Value -eq "offline"})) {$Key.name}
#########################################################################################
# Now run the invoke-command on online computers
#########################################################################################
$remotequery = Invoke-command -ComputerName $computersonline -ErrorAction SilentlyContinue -ErrorVariable MyErrorVar4InvCmd -scriptblock {remote-command} | select Property | where Property -match 'True'
$cantaccess = $MyErrorVar4InvCmd.Targetobject
} # end measure-command
TotalSeconds:34,9779497和“ $ cantaccess = $ MyErrorVar4InvCmd.Targetobject”正确显示
另一种缓慢的方法:
Measure-Command {
#########################################################################################
# Now run the invoke-command
#########################################################################################
$queryresult = @() # create blank array,it will get filled with +=
$CompOFFLINE = @()
ForEach ($computer in $adcomputers) {
if (Test-Connection $computer -Count 1 -quiet) {
$remotequery = Invoke-command -ComputerName $computer -ErrorAction SilentlyContinue -ErrorVariable MyErrorVar4InvCmd -scriptblock {remote-command} | select Property | where Property -match 'True'
if ($remotequery.Property -eq 'true') {
$queryresult += $computer
}
else {
}
}
else {
$CompOFFLINE += $computer
}
}
$cantaccess = $MyErrorVar4InvCmd.Targetobject
} # end measure-command
TotalSeconds:107,4585155和“ $ cantaccess = $ MyErrorVar4InvCmd.Targetobject”为空白...
谢谢, 罗伯特
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)