As long as the Secondary Logon service (seclogon
) is running, the following code blocks permit a combination of Batch and VBScript files to automate the task. the batch file uses relative path references to allow the files to be places into any path that allows at least read permission by the current and selected user accounts. Both files should be located within the same path. The use of ShellExecute
with a verb of runasuser
causes Windows to bring up a prompt to allow the user to select from any logon method allowed by the host computer.
This process can be added to a users startup processes so that it occurs once logged into a computer system.
Batch file: {RunAsUser}{CMD}.cmd
@Echo Off
If "%~1" NEQ "/CALLBACK" Goto :label_Process_Run_As_User
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM Start the process once running as designated user
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
cd C:\
start "" %~dp0cmd.lnk
Goto :EOF
:label_Process_Run_As_User
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM Section below verifies if Secondary Login is available
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM Query [Secondary Logon]
sc query seclogon 1> nul 2> nul || (
Goto :label_Missing_Secondary_Login
)
REM Check to see if [Secondary Logon] service is not disabled
sc qc seclogon | Find /i "START_TYPE" | Find /i "DISABLED" 1> nul 2> nul && (
Set flg.SecLog.Enabled=F
) || (
Set flg.SecLog.Enabled=T
)
REM Check to see if [Secondary Logon] service is Running
sc queryex seclogon | Find /i "STATE" | Find /i "RUNNING" 1> nul 2> nul && (
Set flg.SecLog.Running=T
) || (
Set flg.SecLog.Running=F
)
REM Determine if action should work
If /i "%flg.SecLog.Enabled%:%flg.SecLog.Running%" EQU "F:F" Goto :label_Secondary_Login_Unavailable
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM Section below starts the RunAsUser process
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM System configuration was validateed and RunAsUser will commence
Set "str.SELF=%~0"
WSCRIPT /E:VBSCRIPT "%~dp0RunAsUser.txt"
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM Section below provides written notices to user for error conditions
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:label_Secondary_Login_Unavailable
Echo.
Echo Unable to utilize the Secondary Logon system service because it is disabled.
Echo.
pause
Goto :EOF
:label_Missing_Secondary_Login
Echo.
Echo Unable to find the Secondary Logon system service
Echo.
pause
Goto :EOF
VBScript file: RunAsUser.txt
'-------------------------------------------
'
' Launch Process RunAsUser
CreateObject("Shell.Application").ShellExecute CreateObject("WScript.Shell").Environment("PROCESS")("str.SELF"), "/CALLBACK", "", "runasuser", 1
'
' Display a message box to pause script
msgbox "Enter username or select Certificate for account" & vbCrLf & "On the windows dialog that will popup." & vbCrLf & vbCrLf & "Click OK once process opens", vbokonly
'
' Quit the script
On Error Resume Next
Window.Close ' HTA Must be Closed Through the Window Object
Err.Clear
Wscript.Quit ' VBS Must be Closed Through the Wscript Object
Err.Clear
On Error Goto 0
'
' ----------------------------------------------------------------------