powershellwaitthread-sleeppause

Alternative to Thread. Sleep with different hardware


I am writing a autologin script in Powershell. With main purpose of doing autologon with keystrokes on remote clients in our environment after installation, with the desired AD and password entered.

Works fine on my i9. But most people using Tablets and Elitebooks so using

Thread Sleep

Works bad since i would need to have custom timing on Every hardware, or very high default numbers for lower end clients using my script

Is there any way adding an "wait for row above to completed" Before continuation to next.


Solution

  • I got it all working perfect by calling Windows Timer Resolution using ntdll and adjusting it to be 0.500 (Also working using; WinMM library DLL) Then i switched to using [System.Threading.Thread]::Sleep(1 * 1000) instead of normal Start-Sleep. So now i could get a more precise execution wait timing and this does wonders when heavily relying on accurate timing in milliseconds.

        ´$NtSetTimerResolution = @'
    [DllImport("ntdll.dll", SetLastError=true)]
    public static extern int NtSetTimerResolution(uint DesiredResolution, bool SetResolution, out uint CurrentResolution );
    '@
    
    $NtdllSet = Add-Type -MemberDefinition $NtSetTimerResolution -Name 'NtdllSet' -Namespace 'Win32' -PassThru
    
    $TRCurrent=0
    $null = $NtdllSet::NtSetTimerResolution(5000, $true, [ref]$TRCurrent)´
    

    Reference: PowerShell Sleep Duration Accuracy and Windows Timers