Arrêter en vbscript

```vbscript
Dim objShell
Dim strShutdown

strShutdown = "shutdown -s -t 0 -f"

Set objShell = CreateObject("WScript.Shell")
objShell.Run strShutdown

Wscript.Quit
```

13 answers

  1. Hello,

    I am also a beginner in VBS, but the answer was in the script...
    It asks for the computer name, you just need to cancel the prompt by using "." :
    Dim objShell, strComputer, strInput Dim strShutdown Do strComputer = "." If strComputer <> "" Then strInput = True End if Loop until strInput = True strShutdown = "shutdown -s -t 0 -f -m \\" & strComputer set objShell = CreateObject("WScript.Shell") objShell.Run strShutdown Wscript.Quit
    1. I found this
      If your application needs a privilege for certain actions (for example, requesting the shutdown of the workstation), you can use the function below. Note that it is recommended to always remove a privilege once the action is completed.

      The following function grants or revokes (depending on the value of the parameter grant) the privilege specified by the parameter name:

      bool __fastcall SetPrivilege(AnsiString name, bool grant)
      {
      TOKEN_PRIVILEGES wTokenIn, wTokenOut;
      DWORD wLength;
      HANDLE wCurrentProcess, wToken;
      _LUID wLuid;
      bool ret;

      wCurrentProcess = GetCurrentProcess();
      OpenProcessToken(wCurrentProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &wToken);
      LookupPrivilegeValue(NULL, name.c_str(), &wLuid);
      wTokenIn.PrivilegeCount = 1;
      wTokenIn.Privileges[0].Luid = wLuid;
      wTokenIn.Privileges[0].Attributes = (grant?SE_PRIVILEGE_ENABLED:0);
      ret = AdjustTokenPrivileges(wToken, FALSE, &wTokenIn, sizeof(TOKEN_PRIVILEGES), &wTokenOut,&wLength);
      CloseHandle(wToken);

      return ret;
      }

      For example, to add the SE_SHUTDOWN_NAME parameter, perform an action, and then remove the privilege, based on the information from the MSDN listing the authorization constants (see below), we will use:

      SetPrivilege("SeShutdownPrivilege",true);
      // actions
      SetPrivilege("SeShutdownPrivilege",false);
      1. Your code doesn't work. Obviously, because it's C... so with VBSscript....

        I found out how to change the privilege level. It is done when connecting to the WMI.
        So you need to modify the code I gave you as follows:

        On Error Resume Next

        strComputer = "."
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & strComputer & "\root\cimv2")
        if Err.Number <> 0 Then
        Wscript.Echo "Error connecting to " & strComputer & " : " & Err.Description
        Err.Clear
        wscript.quit
        end if
        Set oWindows = objWMIService.ExecQuery("Select Name From Win32_OperatingSystem")
        if Err.Number <> 0 Then
        Wscript.Echo "Error querying " & strComputer & " : " & Err.Description
        Err.Clear
        wscript.quit
        end if

        For Each oSys In oWindows
        oSys.Win32ShutDown(5)
        if Err.Number <> 0 Then
        Wscript.Echo "Error shutting down the system " & strComputer & " : " & Err.Description
        Err.Clear
        wscript.quit
        end if
        Next
        wscript.quit

        Thus, it works perfectly.

        Good luck.
        1. I found a code to activate .bat

          but I'm struggling to create it, I have the code but I can't create it with shutdown -s in it

          CreateObject(servername.typename [, location])

          if someone could help me thanks
          1. I propose the following code, using WMI.
            However, I created it from 2 existing codes (which worked separately) and I'm not sure that the final combination works...

            On Error Resume Next
            strComputer = "."
            'connect to the local WMI
            Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
            ' query the objects
            Set oWindows = objWMIService.ExecQuery("Select Name From Win32_OperatingSystem")
            For Each oSys In oWindows
            oSys.Win32ShutDown(5)
            Wscript.Echo "System shutdown in progress: " & pComputer
            Next

            wscript.quit
            1. Thank you for the code; a window saying "system shutdown in progress" does appear, but it doesn't turn off. However, when a shutdown is initiated without a specified time, it shuts down after 30 seconds, but not this time. Can you test it?
              Thank you.
              1. No, that's not it.
                If we replace
                oSys.Win32ShutDown(5) with oSys.Win32ShutDown(4) which allows to log off, it works.
                When I display the error, I get a message "privilege not maintained"
                But I am on Vista....

                Can you modify the script as follows:

                On Error Resume Next
                strComputer = "."
                'connect to the local WMI
                Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
                ' query on the objects
                Set oWindows = objWMIService.ExecQuery("Select Name From Win32_OperatingSystem")
                For Each oSys In oWindows
                oSys.Win32ShutDown(5)
                if Err.Number <> 0 Then
                Wscript.Echo "Error shutting down: " & strComputer & " : " & Err.Description
                Err.Clear
                wscript.quit
                end if

                Next

                wscript.quit

                And see the message it shows you???
                1. I have the same "error at stop, privilege not maintained"
                  I'm going to see what we can do.
                  1. OK, good to know.
                    Actually, at first, my code worked to remotely shut down a computer.
                    I connected to its WMI with administrator privileges, so I never had this elevation of privileges issue: it always worked.

                    So is your problem solved???