Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Script To atomate login time

$
0
0

I have script needs update to extend login time by 15 minutes.

 

Please suggest mofifications to ot.

 

Add-Type @'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace PInvoke.Win32 {

    public static class UserInput {

        [DllImport("user32.dll", SetLastError=false)]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        [StructLayout(LayoutKind.Sequential)]
        private struct LASTINPUTINFO {
            public uint cbSize;
            public int dwTime;
        }

        public static DateTime LastInput {
            get {
                DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
                DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);
                return lastInput;
            }
        }

        public static TimeSpan IdleTime {
            get {
                return DateTime.UtcNow.Subtract(LastInput);
            }
        }

        public static int LastInputTicks {
            get {
                LASTINPUTINFO lii = new LASTINPUTINFO();
                lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
                GetLastInputInfo(ref lii);
                return lii.dwTime;
            }
        }
    }
}
'@


function Get-Values ($title,$Hours, $Minutes, $Seconds) {

    ###################Load Assembly for creating form & button######

    [void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
    [void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)

    #####Define the form size & placement

    $form = New-Object “System.Windows.Forms.Form”;
    $form.Width = 400;
    $form.Height = 250;
    $form.Text = $title;
    $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

    ##############Define text label1
    $textLabel1 = New-Object “System.Windows.Forms.Label”;
    $textLabel1.Left = 25;
    $textLabel1.Top = 15;

    $textLabel1.Text = $Hours;

    ##############Define text label2

    $textLabel2 = New-Object “System.Windows.Forms.Label”;
    $textLabel2.Left = 25;
    $textLabel2.Top = 50;

    $textLabel2.Text = $Minutes;

    ##############Define text label3

    $textLabel3 = New-Object “System.Windows.Forms.Label”;
    $textLabel3.Left = 25;
    $textLabel3.Top = 85;

    $textLabel3.Text = $Seconds;

    ############Define text box1 for input
    $textBox1 = New-Object “System.Windows.Forms.TextBox”;
    $textBox1.Left = 150;
    $textBox1.Top = 10;
    $textBox1.width = 100;

    ############Define text box2 for input

    $textBox2 = New-Object “System.Windows.Forms.TextBox”;
    $textBox2.Left = 150;
    $textBox2.Top = 50;
    $textBox2.width = 100;

    ############Define text box3 for input

    $textBox3 = New-Object “System.Windows.Forms.TextBox”;
    $textBox3.Left = 150;
    $textBox3.Top = 90;
    $textBox3.width = 100;

    #############Define default values for the input boxes
    $defaultHours = 7
    $defaultMinutes = 59
    $defaultSeconds = 59
    $textBox1.Text = $defaultHours;
    $textBox2.Text = $defaultMinutes;
    $textBox3.Text = $defaultSeconds;

    #############define OK button
    $button_ok = New-Object “System.Windows.Forms.Button”;
    $button_ok.Top = 130;
    $button_ok.Left = 260;

    $button_ok.Width = 100;
    $button_ok.Text = “Ok”;

    $button_can = New-Object “System.Windows.Forms.Button”;
    $button_can.Top = 130;
    $button_can.Left = 60;
    $button_can.Width = 100;
    $button_can.Text = “Cancel”;

    ############# This is when you have to close the form after getting values
    $eventHandler = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    $textBox3.Text;
    $form.Close();};
    $button_ok.Add_Click($eventHandler) ;
    $button_can.Add_Click($eventHandler) ;

    #############Add controls to all the above objects defined
    $form.Controls.Add($textLabel1);
    $form.Controls.Add($textLabel2);
    $form.Controls.Add($textLabel3);
    $form.Controls.Add($textBox1);
    $form.Controls.Add($textBox2);
    $form.Controls.Add($textBox3);
    $form.Controls.Add($button_ok);
    $form.Controls.Add($button_can);

    $ret = $form.ShowDialog();

    #################return values

    $hour=$textBox1.Text
    $min=$textBox2.Text
    $sec=$textBox3.Text
    $secs = ([int]$hour)*3600 + ([int]$min)*60 + [int]$sec
    #Write-Host "In Function: $secs"
    return ($secs)
}

function getSeconds($Min1, $Max1) {
    $secs =  Get-Values "Duration of Workday to keep Alive" “Hours” “Minutes” “Seconds”
    #Write-Host "In Function: $secs, $Min1, $Max1"
    $MaxSecs = 3600 * $Max1
    while (-not(($secs -lt $MaxSecs) -and ($secs -gt $Min1))) {
        $Range = "Value is not within: $Min1 Seconds and  $Max1 Hours, Try Again?"
        $result = [Microsoft.VisualBasic.Interaction]::MsgBox($Range, ‘YesNoCancel,Question’, “Respond please”)
        #Write-Host "In Function: $result"
        switch ($result) {
        ‘Yes’ {
            $secs =  Get-Values "Duration of Workday to keep Alive" “Hours” “Minutes” “Seconds”
            }
        ‘No’ {
            $secs = 0
            }
        ‘Cancel’ {
            $defaultHours = 0
            $defaultMinutes = 59
            $defaultSeconds = 59
            $secs = ([int]$defaultHours)*3600 + ([int]$defaultMinutes)*60 + [int]$defaultSeconds
            }
        }
     }
     return $secs
}

function Secs2Time ($Duration1) {
    $Hrs = [math]::floor($Duration1/3600)
    $Min = [math]::floor(($Duration1%3600)/60)
    $Sec = $Duration1%60
    $time = '{0}:{1}:{2}' -f $Hrs.ToString('00'),$Min.ToString('00'),$Sec.ToString('00')
    #Write-Host "In Function Secs2Time End: $Duration1 $time" 
    return $time
   
}

# Duration Between p1=Seconds and p2=Hours
function Main() {
$Duration= getSeconds 60 12
$Msg = "Please be aware of side effects if any."
$proDir = (split-path $profile)
if ( ! (test-path -pathtype container $proDir)) {
   mkdir $proDir
}
$timestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
$File="$proDir/log.$timestamp.txt"
$BackgroundColor = "yellow"
$ForegroundColor = "blue"
Write-Host -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor $Msg;
$Msg | Out-File -encoding "UTF8" -filepath $File
$timestamp=Get-Date -Format o
$interval = 59
$cnt=0
$lag=0

$Last_input=([PInvoke.Win32.UserInput]::LastInput)
Write-host '$Last_input:{0}',$Last_input
$Last_input
$Idle_for=([PInvoke.Win32.UserInput]::IdleTime)
Write-host '$Idle_for:{0}',$Idle_for
$Idle_for

while ($cnt -lt $Duration) {
    $ElapsedTime=Secs2Time $cnt
    $RemainingTime=Secs2Time ($Duration-$cnt)
    $lag = $lag + ([math]::floor($Duration1/3600)%2)
    $cnt = $cnt + $interval+1
    $wsh = New-Object -ComObject WScript.Shell
    $wsh.SendKeys('{NUMLOCK}')
    Start-Sleep -Seconds 1
    $wsh.SendKeys('{NUMLOCK}')
    $time_now=Get-Date -Format o # (Get-Date).ToString() #
    $Last_input=([PInvoke.Win32.UserInput]::LastInput)
    $Idle_for=([PInvoke.Win32.UserInput]::IdleTime)
    $Msg = "$time_now, lag = $lag, Last: $Last_input Idle:$Idle_for Elapased Time: $ElapsedTime Remaining Time: "
    $Msg2 = "$RemainingTime"
    Write-Host -NoNewline $Msg;
    Write-Host -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor $Msg2;
    $Msg + $Msg2 | Out-File -encoding "UTF8" -filepath $File -append
    Start-Sleep -Seconds ($interval)
}
$time_now=Get-Date -Format o
$Msg = 'Start:{0} Finish:{1}' -f $timestamp,$time_now
Write-Host -BackgroundColor $BackgroundColor -ForegroundColor $ForegroundColor $Msg;
$Msg | Out-File -encoding "UTF8" -filepath $File -append
}
Main

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles