我如何在PowerShell中暂停regquery命令



我正在尝试找到一种暂停系统作业的方法(也许有工作?)。如果计算机不在线,我的登录通常会在42秒后止步,并且代码无法达到注册表。我希望将查询限制在5秒钟左右,因为我的环境中有大量的计算机。我试图在创造工作,秒表等方面玩耍。但是没有运气:(请帮忙!

$File = Import-Csv 'c:tempregcomplist.txt'
$Results=""
$text="Machine Name,Regkey Value, Runtime"
$fileout = "C:Tempregquery.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:temppowershellregcomplist.txt'
$Results=""
$text="Machine Name,Regkey Value, Runtime"
$fileout = "C:Temppowershellregquery.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("SYSTEMCurrentControlSetControlSession ManagerEnvironment").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 !
    }
}