Jump to content

Force reboot (or not)


Recommended Posts

Hello everybody,

I am currently programming some maintenance scripts to be run at night. And because some scripts don't work unless there is an admin session open and active, I've created a script that checks whether that's the case.

It's called by the task manager like "C:\Program Files\Scripts\Login.exe" "C:\Program Files\Scripts\The Script I Really Want To Run.exe" If there is an admin session open and active, it just runs the second script. If not, it sets the registry for an autologin into admin and, in theory, reboots the computer. That's the problem, it reaches that point successfully, but it never reboots the computer. Why? I'm really puzzle. Any ideas?

Regards,

Carlos

;Establezco las opciones
AutoItSetOption("MustDeclareVars", 1)

;Declaro las variables
Dim $nombreFicheroLog, $param1, $param2

;Principio
$nombreFicheroLog = FileOpen("C:\Program Files\Scripts\Script Log.txt", 1);Write mode (append to end of file)
FileWriteLine($nombreFicheroLog, @MDAY & "-" & @MON & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & @ScriptName & "   Principio")

;Obtengo los parámetros
If $CmdLine[0] = 1 Then
    $param1 = $CmdLine[1]
    $param2 = "paso1"
ElseIf $CmdLine[0] = 2 Then
    $param1 = $CmdLine[1]
    $param2 = $CmdLine[2]
Else
    FileWriteLine($nombreFicheroLog, @MDAY & "-" & @MON & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & @ScriptName & "   Final   (Número de parámetros incorrecto)")
    FileClose($nombreFicheroLog)
    Exit
EndIf

;Averiguo si es la primera o la segunda vez que lo ejecuto
Select
;Es la primera
    Case $param2 = "paso1"
        FileWriteLine($nombreFicheroLog, @MDAY & "-" & @MON & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & @ScriptName & "   (Es la primera vez que lo ejecuto)")
;Averiguo si hay una sesión de "Admin" abierta y activa
        Run('"C:\Program Files\Scripts\Login - Probando.exe"');Watch out the quotes
        Sleep(5000)
;Si la hay, ejecuto el script y final
        If WinExists("Probando, probando.", "Un, dos, tres.") Then
            WinClose("Probando, probando.", "Un, dos, tres.")
            Run('"' & $param1 & '"')
            FileWriteLine($nombreFicheroLog, @MDAY & "-" & @MON & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & @ScriptName & '   Final   (Sesión de "Admin" abierta y activa)');I have to use single quotes because I've used double quotes in Admin
            FileClose($nombreFicheroLog)
            Exit
        EndIf
;Si no la hay, cierro la ventana de prueba, programo Windows para que al reiniciarlo se abra una sesión de "Admin" y se ejecute "Login" por segunda vez, final y lo reinicio
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RUN", "Login", "REG_SZ", '"' & @ScriptFullPath & '" "' & $param1 & '" paso2')
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "AutoAdminLogon", "REG_SZ", "1")
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "DefaultDomainName", "REG_SZ", EnvGet("LOGONSERVER"))
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "DefaultUserName", "REG_SZ", "Admin")
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "DefaultPassword", "REG_SZ", "password")
        FileWriteLine($nombreFicheroLog, @MDAY & "-" & @MON & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & @ScriptName & "   Final   (Reinicio del sistema)")
        FileClose($nombreFicheroLog)
        Shutdown(6);Force reboot
;Es la segunda
    Case $param2 = "paso2"
        FileWriteLine($nombreFicheroLog, @MDAY & "-" & @MON & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & @ScriptName & "   (Es la segunda vez que lo ejecuto)")
;Espero a que se termine de iniciar Windows, lo programo para que al reiniciarlo no se abra una sesión de "Admin" y no se ejecute "Login", ejecuto el script y final
        Sleep(60000)
        RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RUN", "Login")
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "AutoAdminLogon", "REG_SZ", " ")
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "DefaultDomainName", "REG_SZ", " ")
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "DefaultUserName", "REG_SZ", " ")
        RegWrite("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\", "DefaultPassword", "REG_SZ", " ")
        Run('"' & $param1 & '"')
        FileWriteLine($nombreFicheroLog, @MDAY & "-" & @MON & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & @ScriptName & "   Final")
        FileClose($nombreFicheroLog)
EndSelect

;Establezco las opciones
AutoItSetOption("MustDeclareVars", 1)
#NoTrayIcon

;Abro una ventana de diálogo
MsgBox(0, "Probando, probando.", "Un, dos, tres.")
Edited by Carlos Villar
Link to comment
Share on other sites

I use

Shutdown (18)

This forces a shutdown even if some process hangs while shutting down. However, this has the (obvious) proviso that your script in fact reaches the Shutdown command - if it does not, that is a good old-fashioned logic error.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...