Add Interactive as Admin for x Days

So every once in a while we get into the situation where a user has to get Administrator access on the company computers, be it for traveling where they have to install something or have some other issues at hand.

 

If that is the case you can add a timed access the following way.

 

What you need:

 

The Script:


$CompName = Read-Host('Computer Name to add as admin x day.');
$days = Read-Host('how many days');
$Date=(get-date).AddDays($days)
$DateYear=$Date.ToString("yyyy")
$DateMonth=$Date.ToString("MM")
$DateDay=$Date.ToString("dd")
$DateHour=$date.ToString("HH")
$DateMinute=$date.ToString("mm")
$DateString=$DateYear + '/' + $DateMonth + '/' + $DateDay 
$TimeString=$DateHour + ':' + $DateMinute
$PSString1='psexec.exe \\' + $CompName + ' -s cmd.exe "/c net localgroup administrators /add interactive"'
$PSString2='psexec.exe \\' + $CompName + ' -s cmd.exe "/c schtasks /create /tn RemoveInteractiveAsAdmin /SC once /ru system /tr "net localgroup administrators /del interactive" /sd ' + $DateString + ' /st ' + $TimeString + ' /f"'

if (Test-Connection -computername $CompName -count 1 -quiet)
{
    Invoke-Expression $PSString1
    Invoke-Expression $PSString2
}
else {$Popup.Popup("Error, can not ping $CompName",0,"Error",16)}

Posted on: Sunday 11 January 2015