如何使 Powershell 中的 REGQUERY 命令超时?

How do I time out a REGQUERY command in Powershell?

我正在尝试找到一种方法(也许是工作?)使系统作业超时。如果计算机不在线,我的注册表查询似乎通常会在 42 秒后超时,并且代码无法访问注册表。我希望将查询限制在 5 秒左右,因为我的环境中有大量计算机。我尝试过创造工作、秒表等,但没有成功 :( 请帮忙!

$File = Import-Csv 'c:\temp\regcomplist.txt'
$Results=""
$text="Machine Name,Regkey Value, Runtime"
$fileout = "C:\Temp\regquery.csv"
Write-host $text
Out-File -FilePath $fileout -InputObject $text -Force
$timeout = new-timespan -Seconds 5
$swtotal = [Diagnostics.Stopwatch]::StartNew()
foreach ($line in $file)
{
TRY{
$regkey = ""
$keyValue = ""
$machinename = $line.machinename
#trap [Exception] {continue}    
$key = "SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3\Lexmark Universal v2 XL"
$sw = [Diagnostics.Stopwatch]::StartNew()   
#while ($sw.elapsed -lt $timeout){
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$MachineName)
    $regkey = $reg.opensubkey($key)
    $keyValue = $regKey.GetValue('Help File')

#   return
#}
#start-sleep -seconds 5
#if ($ok -ne "OK"){$keyValue = "TIMED OUT: "+$sw.elapsed}
}
Catch{
$keyValue = "Error Opening Registry"
}
$text = $machinename+","+$keyValue+","+$sw.elapsed
$Results += $text
#Output Below Here:
Write-host $text
Out-File -InputObject $text -FilePath $fileout -Append 

}
Write-host "Total time run:"$swtotal.elapsed

谢谢!这正是我所需要的。我将计数设置为 1 ping,它比 42 秒超时快得多。这是可能对任何人有帮助的代码...

$File = Import-Csv 'c:\temp\powershell\regcomplist.txt'
$Results=""
$text="Machine Name,Regkey Value, Runtime"
$fileout = "C:\Temp\powershell\regquery.csv"
Write-host $text
Out-File -FilePath $fileout -InputObject $text -Force
$timeout = new-timespan -Seconds 5
$swtotal = [Diagnostics.Stopwatch]::StartNew()
foreach ($line in $file){
$regkey = ""
$keyValue = ""
$machinename = $line.machinename
#trap [Exception] {continue} 
$key = "SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3\Lexmark Universal v2 XL"
$sw = [Diagnostics.Stopwatch]::StartNew()

$pingtest=Test-Connection -ComputerName $machinename -Quiet -Count 1
if ($pingtest -eq $true ){
    TRY{    
    #while ($sw.elapsed -lt $timeout){
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$MachineName)
    $regkey = $reg.opensubkey($key)
    $keyValue = $regKey.GetValue('Help File')

    #   return
    #}
    #start-sleep -seconds 5
    #if ($ok -ne "OK"){$keyValue = "TIMED OUT: "+$sw.elapsed}
    }
    Catch{
    $keyValue = "Error Opening Registry"
    }
    $text = $machinename+","+$keyValue+","+$sw.elapsed
    $Results += $text
    #Output Below Here:
    Write-host $text
    Out-File -InputObject $text -FilePath $fileout -Append 

}
else {write-host $machinename",doesn't ping!"}
}
Write-host "Total time run:"$swtotal.elapsed

我在查询注册表之前使用了 Tcp 测试:

if((Test-ComputerPort $ComputerName 445)) {
    [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName).OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment").GetValue('TEMP')
}

使用 Test-ComputerPort() :

function Test-ComputerPort($ComputerName,$port){
    try {
        $ip = [System.Net.Dns]::GetHostAddresses($ComputerName).IPAddressToString # confirm DNS resolving
        if([system.Net.Sockets.TcpClient]::new().BeginConnect($ComputerName, $port, $null, $null).AsyncWaitHandle.WaitOne(40, $false) -or [system.Net.Sockets.TcpClient]::new().BeginConnect($ComputerName, $port, $null, $null).AsyncWaitHandle.WaitOne(80, $false)){ # with retry if down
            return $ComputerName
        }
        return $false # tcp-port is down !
    } catch {
        return $null # conputername not resolved !
    }
}