Remote Logoff, restart, and shutdown PCs

Author Message
Wakawaka

  • Total Posts : 456
  • Scores: 23
  • Reward points : 0
  • Joined: 8/27/2009
  • Status: offline
Remote Logoff, restart, and shutdown PCs Wednesday, October 28, 2009 4:12 AM (permalink)
0
Don't know HOW useful this is to anyone, but this HTA allows you to Shutdown, restart or log off users.

All have a "Force" option that will automatically End all running applications  For the Restart and Shutdown you can specify a shutdown delay and comment to be shown to the user.

This has only been tested with IE7 on a WIN XP system SP3

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
 <html> 
      <!-- 
      '******************************************************************** 
      '*  File:          PC Power Manager.hta 
      '*  Author:        Justin Thomas 
      '*  Created:       Oct 2009 
      '*  Modified:      Oct 2009 
      '*                    Nov 2009 
      '*                    Dec 2009 
      '*  Version:       2.8 
      '******************************************************************** 
      --> 
      <head> 
      <title>PC Power Manager</title> 
      <HTA:APPLICATION 
          applicationName = "PC Power Manager" 
          singleInstance = "yes" 
          showInTaskbar = "yes" 
          border = "thin" 
          scroll = "no" 
          maximizeButton = "no" 
          contextMenu = "no" 
          version = "2.8" 
      > 
 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
 
      <style> 
          body{ 
              background-color: buttonface; 
          } 
          p{ 
              text-align: right; 
          } 
          button{ 
              clear: both; 
          } 
          span{ 
              font-size: 18pt; 
          } 
          #left{ 
              float: left; 
              width: 200px; 
          } 
          #right{ 
              float: right; 
              width: 225px; 
          } 
      </style> 
       
      <SCRIPT LANGUAGE="vbScript"> 
          OPTION EXPLICIT 
          Const READ_ONLY = 1 
          Const LOG_OFF = 0 
          Const FORCE_LOGOFF = 4 
           
          Sub Window_OnLoad() 
              Call CenterWindow(500, 475) 
              Call CreateDelay(30) 
              Call ChangeState 
              pcList.Focus() 
          End Sub 'End Window_OnLoad() 
           
          Sub CenterWindow(intWidth, intHeight) 
              self.ResizeTo intWidth, intHeight 
              self.MoveTo (screen.Width - intWidth)/2, (screen.Height - intHeight)/2 
          End Sub 'End CenterWindow(intWidth, intHeight) 
           
          Sub CreateDelay(intMaxTime) 
              Dim i 
              For i=5 to intMaxTime 
                  Dim objOption : Set objOption = Document.CreateElement("option") 
                  objOption.value = "-t " & i 
                  objOption.Text = i 
                  sel_Delay.Add(objOption) 
              Next 
          End Sub 'End GetDelay() 
           
          Function CreateCmd() 
              If Document.GetElementByID("logoff").Checked = True Then 
                  strNewCmd = "" 
              Else 
                  Dim objTask 
                  Dim strNewCmd : strNewCmd = "shutdown.exe " 
                  Dim arrTasks : Set arrTasks = Document.GetElementsByName("rad_shutdownOpt") 
               
                  For each objTask In arrTasks 
                      If objTask.Checked Then 
                          strNewCmd = strNewCmd & objTask.Value 
                      End If 
                  Next 
               
                  If Document.GetElementByID("chk_Force").Checked Then 
                      strNewCmd = strNewCmd & Document.GetElementByID("chk_Force").Value 
                  End If 
               
                  strNewCmd = strNewCmd & Document.GetElementByID("sel_Delay").Value 
                  If Document.GetElementByID("txt_Message").Value = "" Then 
                      strNewCmd = strNewCmd & " -c " & chr(34) & "This is a planned event." & chr(34) 
                  Else 
                      strNewCmd = strNewCmd & " -c " & chr(34) & Document.GetElementByID("txt_Message").Value & chr(34) 
                  End If 
                  Set arrTasks = Nothing 
              End If 
              CreateCmd = strNewCmd 
          End Function 'End CreateCmd() 
           
          Sub Execute_OnClick() 
              Dim strComputer, intProcessID 
              Dim objNetwork : Set objNetwork = CreateObject("WScript.Network") 
              Dim strCommand : strCommand = CreateCmd() 
              Dim arrComputers : arrComputers = Split(pcList.Value, vbCrLf) 
              txt_Results.Value = "" 
              If UBound(arrComputers) < 0 Then 
                  Msgbox "At least one remote PC required!", vbApplicationModal & vbExclamation, "Remote PC's - Error" 
                  pcList.Focus() 
              Else 
                  Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") 
                  For Each strComputer In arrComputers 
                      If UCase(strComputer) = UCase(objNetwork.ComputerName) Then 
                          txt_Results.Value = UCase(strComputer) & " is the localhost, command aborted" & vbCrLf & txt_Results.Value 
                      Else 
                          Dim blnExecuteNext : blnExecuteNext = ValidatePC(strComputer) 
                          If blnExecuteNext = True Then 
                              Dim objWMI : Set objWMI = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2") 
                               
                              On Error Resume Next 
                              If Not(objFSO.FolderExists("\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName)) Then 
                                  objFSO.CreateFolder "\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName 
                              End If 
                               
                              If strCommand <> "" Then 
                                  Dim objProcess : Set objProcess = objWMI.Get("Win32_Process") 
                                  objProcess.Create strCommand, Null, , intProcessID 
                                  Select Case Err.Number 
                                  Case "0" 
                                      txt_Results.Value = UCase(strComputer) & " successfully created shutdown process" & vbCrLf & txt_Results.Value 
                                  Case "2" 
                                      txt_Results.Value = UCase(strComputer) & " denied access to create proccess" & vbCrLf & txt_Results.Value 
                                  Case "3" 
                                      txt_Results.Value = "You do not have sufficient privileges to create processes on " & Ucase(strComputer) & vbCrLf & txt_Results.Value 
                                  Case "9" 
                                      txt_Results.Value = UCase(strComputer) & " could not find the specificed execution path" & vbCrLf & txt_Results.Value 
                                  Case "21" 
                                      txt_Results.Value = UCase(strComputer) & " was passed and invalid parameter" & vbCrLf & txt_Results.Value 
                                  Case Else 
                                      txt_Results.Value = "An unknown error has occured on " & UCase(strComputer) & vbCrLf & txt_Results.Value 
                                  End Select 
                                  Set objProcess = Nothing 
                              Else 
                                  Dim objOSItem 
                                  Dim colOS : Set colOS = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem") 
                                  For Each objOSItem In colOS 
                                      Select Case Document.GetElementByID("chk_Force").Checked 
                                          Case "True" 
                                              objOSItem.Win32Shutdown FORCE_LOGOFF 
                                          Case Else 
                                              objOSItem.Win32Shutdown LOG_OFF 
                                      End Select 
 
                                      Select Case Err.Number 
                                          Case "0" 
                                              txt_Results.Value = UCase(strComputer) & " successfully created shutdown process" & vbCrLf & txt_Results.Value 
                                          Case Else 
                                              txt_Results.Value = "An unknown error has occured on " & UCase(strComputer) & vbCrLf & txt_Results.Value 
                                      End Select 
                                  Next 
                                  Set objOSItem = Nothing 
                                  Set colOS = Nothing 
                              End If 'End Shutdown Opt Check 
                          End If 'End ValidatePC Check 
                      End If 'End localhost Check 
                      On Error Resume Next 
                      objFSO.DeleteFolder "\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName, True 
                  Next 
                  Set objFSO = Nothing 
                  Set objWMI = Nothing 
                  Set objNetwork = Nothing 
                  Err.Clear 
              End If 
          End Sub 'End Execute_OnClick() 
           
          Function ValidatePC(strComputer) 
               If strComputer = "" Then 
                  ValidatePC = False 
                  Exit Function 
              End If 
               
              ON ERROR RESUME NEXT 
              Dim colPing : Set colPing = GetObject("winmgmts:\\").ExecQuery("Select * from Win32_PingStatus where Address='" & strComputer & "'") 
              Dim objPingStatus 
 
              For Each objPingStatus in colPing 
                  If objPingStatus.StatusCode = 0 Then 
                      If IsNull(GetObject("winmgmts:\\" & strComputer & "\root\cimv2")) Then 
                          txt_Results.Value = UCase(strComputer) & " denied access to WMI." & vbCrLf & txt_Results.Value 
                           ValidatePC = False 
                      Else 
                          ValidatePC = True 
                      End If 
                  Else 
                      txt_Results.Value = UCase(strComputer) & " did not respond to ping requests" & vbCrLf & txt_Results.Value 
                      ValidatePC = False 
                  End If 
              Next 
              Set objPingStatus = Nothing 
              Set colPing = Nothing 
          End Function 'End ValidatePC(strComputer) 
           
          Sub LoadFile_OnClick() 
              Dim objDialog : Set objDialog = CreateObject("UserAccounts.CommonDialog") 
   
              With objDialog 
                  .Filter = "Text Files (*.txt)|*.txt" 
                  .FilterIndex = 1 
                  .InitialDir = "C:\" 
              End With 
              objDialog.ShowOpen 
 
              If objDialog.FileName <> "" Then 
                  Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") 
                  If objFSO.FileExists(objDialog.FileName) Then 
                      Dim objFile : Set objFile = objFSO.OpenTextFile(objDialog.FileName, READ_ONLY) 
                      pcList.Value = "" 
                      Do Until objFile.AtEndOfStream 
                          Dim strLine : strLine = TRIM(objFile.ReadLine) 
                          pcList.Value = pcList.Value & strLine & vbCrLf 
                      Loop 
                  Else 
                      Msgbox objDialog.FileName & " not found!", vbApplicationModal & vbCritical,"Open File - Error" 
                  End If 
              End If 
              Set objFile = Nothing 
              Set objFSO = Nothing 
              Set objDialog = Nothing 
          End Sub 'End GetFile_OnClick() 
           
          Sub ChangeState() 
              If Document.GetElementByID("logoff").Checked = True Then 
                  Document.GetElementByID("sel_Delay").Disabled = True 
                  Document.GetElementByID("txt_Message").Disabled = True 
              Else 
                  Document.GetElementByID("sel_Delay").Disabled = False 
                  Document.GetElementByID("txt_Message").Disabled = False 
              End If 
          End Sub 'End ChangeState() 
 
          Sub Exit_OnClick() 
              self.Close() 
          End Sub 'End CloseApplication() 
      </SCRIPT> 
      </head> 
       
      <body> 
      <div id="left"> 
          Remote PC(s): 
          <br> 
          <textarea id="pcList" rows="17" cols="25"></textarea> 
          <br> 
      </div> 
      <div id="right"> 
          <fieldset> 
              <legend>Task</legend> 
                  <input type="radio" name="rad_shutdownOpt" id="logoff" checked onClick="ChangeState"/>Logoff<br> 
                  <input type="radio" name="rad_shutdownOpt" value="-r " onClick="ChangeState"/>Restart<br> 
                  <input type="radio" name="rad_shutdownOpt" value="-s " onClick="ChangeState"/>Shutdown<br> 
                  <br> 
                  <input type="checkbox" id="chk_force" value="-f " />Force 
          </fieldset> 
          <br> 
          <fieldset> 
              <legend>Delay</legend> 
                  <select id="sel_Delay"></select>Second(s) 
          </fieldset> 
          <br> 
          <fieldset> 
              <legend>Display Message</legend> 
                  <textarea id="txt_Message" rows="4" cols="24" ></textarea> 
          </fieldset> 
      </div> 
      <p> 
          <button name="LoadFile" accesskey="l"><u>L</u>oad File...</button>&nbsp 
          <button name="Execute" accesskey="e"><u>E</u>xecute</button>&nbsp 
          <button name="Exit" accesskey="x">E<u>x</u>it</button>&nbsp 
      </p> 
          <textarea id="txt_Results" cols="56" READONLY></textarea> 
      </body> 
 </html>


Any suggestions to make it better or more efficient are welcome!

EDIT://Edited as requested below - DM
EDIT: Edited as requested below - Mbouchard
<message edited by mbouchard on Friday, December 18, 2009 1:55 AM>
 
#1
    djredmar

    • Total Posts : 64
    • Scores: 0
    • Reward points : 0
    • Joined: 12/11/2006
    • Location: Europe, The Netherlands
    • Status: offline
    Re:Remote Logoff, restart, and shutdown PCs Thursday, October 29, 2009 2:09 AM (permalink)
    0
    here is mine,
    i just created it today and then saw your script... Funny :D
         <html>
         <head>
          <title>Shutdown tool</title>
          <HTA:APPLICATION
           APPLICATIONNAME = 'Shutdown tool'
           ID      = 'objShutdown'
           BORDER    = 'Thick'
           BORDERSTYLE   = 'Normal'
           CAPTION    = 'Yes'
           CONTEXTMENU   = 'No'
           ICON    = 'img/shutdown.ico'
           INNERBORDER   = 'Yes'
           MAXIMIZEBUTTON  = 'Yes'
           MINIMIZEBUTTON  = 'Yes'
           NAVIGABLE   = 'No'
           SCROLL    = 'Auto'
           SCROLLFLAT   = 'No'
           SELECTION   = 'Yes'
           SHOWINTASKBAR  = 'Yes'
           SINGLEINSTANCE  = 'Yes'
           SYSMENU    = 'Yes'
           VERSION    = '0.1'
           WINDOWSTATE   = 'Normal'>
          <STYLE>
           BODY  {background-color: buttonface; font-family: tahoma; font-size: 10pt; margin-top: 1px; margin-left: 1px; margin-right: 1px; margin-bottom: 1px;}
           button {font-family: tahoma; font-size: 8pt;}
           textarea {font-family: tahoma; font-size: 10pt;}
           select {font-family: tahoma; font-size: 10pt;}
           td   {font-family: tahoma; font-size: 10pt;}
           input  {font-family: tahoma; font-size: 10pt;}
          </STYLE>
         </head>
         <script language='vbscript'>     Option Explicit
         Dim objShell : Set objShell = CreateObject("Wscript.Shell")     Sub MultiFunctions(strType)
          Select Case strType
           Case "Initialize" WindowSize 240, 250      End Select
         End Sub     Sub WindowSize(iWidth, iHeight)
          On Error Resume Next
          Dim posWidth, posHeight
          posWidth   = (window.screen.width - iWidth) / 2
          posHeight  = (window.screen.height - iHeight) / 2
          If posWidth  < 0 Then posWidth = 0
          If posHeight < 0 Then posHeight = 0
          window.resizeTo iWidth, iHeight
          StatusUpdate "Window Size set to " & iWidth & "x" & iHeight
          window.moveTo posWidth, posHeight
          StatusUpdate "Window Postion set to " & posWidth & "x" & posHeight
          On Error GoTo 0
          txtPC.focus()
         End Sub     Dim arrCommands, strPC     </script>     <body onload="MultiFunctions('Initialize')">
         <table>
         <tr>
          <td><b>Pc</b></td>
          <td>:</td>
          <td><input type="text" name="txtPC"  size="10"></td>
         </tr>
         <tr>
          <td><b>Shutdown</b></td>
          <td>:</td>
          <td>
           <select name="cmbShutdown">
            <option value="r">Reboot</option> 
            <option value="s">Shut Down</option> 
          </td>
         </tr>
         <tr>
          <td><b>Timeout</b></td>
          <td>:</td>
          <td><input type="text" name="txtTimeOut" size="2" value="10">sec.</td>
         </tr>
         <tr>
          <td><b>Geforceerd?</b></td>
          <td>:</td>
          <td>
           <input type="radio" name="radForceer" value="Ja" checked>Ja
           <input type="radio" name="radForceer" value="Nee">Nee
          </td>
         </tr>
         <tr>
          <td><b>Commentaar</b></td><td colspan="2">:</td>
         </tr>
         <tr>
          <td colspan="3">
           <textarea name="txtCommentaar" style="width:100%;" rows="3" ></textarea>
          </td>
         </tr>
         <tr>
          <td colspan="3">
           <input type="submit" name="cmdShutdown" value="Shutdown!" OnClick="ShootDown()">
           <input type="button" name="cmdAbort" value="ABORT!!!" OnClick="abortShootDown()">
          </td>
         </tr>
         </table>
         <script>
         cmdAbort.disabled = true     Sub abortShootDown()      
          If Len(Trim(txtPC.value)) = 0 Then
           Msgbox "  - Geen pcnaam opgegegeven!" & vbCrLf, vbCritical, objShutdown.APPLICATIONNAME
          Else
           objShell.Run "%COMSPEC% /C shutdown.exe -a -m \\" & txtPC.Value, 1, False
           cmdAbort.disabled = true      End If
          
         End Sub     Sub ShootDown()      Dim sCommand, sError
          
          
          sCommand = "%COMSPEC% /C shutdown.exe"      If radForceer(0).Checked Then
           sCommand = sCommand & " -f "
          End If      sCommand = sCommand & " -"  & cmbShutdown.value
          If Len(Trim(txtPC.value)) = 0 Then
           sError = sError & "  - Geen pcnaam opgegegeven!" & vbCrLf
          Else
           sCommand = sCommand & " -m \\" & txtPC.Value
          End If
          
          If Len(Trim(txtTimeOut.value)) = 0 Then
           sError = sError & "  - Geen timeout opgegegeven!" & vbCrLf
          Else
           If Not IsNumeric(txtTimeOut.value) Then
            sError = sError & " - Timeout is niet nummeriek!" & vbCrLf
           Else
            sCommand = sCommand & " -t " & txtTimeOut.Value
           End If
          End If
          
          If Len(Trim(txtCommentaar.value)) > 0 Then
           sCommand = sCommand & " -c """ & txtCommentaar.value & """"
          End If      If Len(sError) > 0 Then
           Msgbox sError, vbCritical, objShutdown.APPLICATIONNAME
          Else
           cmdAbort.disabled = false
          
           objShell.Run sCommand, 1, False
          End If
         End Sub     arrCommands = Split(objShutdown.CommandLine, Chr(34) + " ")
         If UBound(arrCommands) = 1 Then 'Parameter passed
          strPC = Replace(arrCommands(1), """", "")
          txtPC.value = Trim(strPC)
         End If     </script>     </body>     </html>
         <!-- Created with HTA Generator v7.1 Tool by J. Libenson : htatools@yahoo.com -->
         
         

     
    #2
      Wakawaka

      • Total Posts : 456
      • Scores: 23
      • Reward points : 0
      • Joined: 8/27/2009
      • Status: offline
      Re:Remote Logoff, restart, and shutdown PCs Thursday, October 29, 2009 2:40 AM (permalink)
      0
      Hmmm....I should add an abort option lol but I guess if it is under 5 seconds it's pretty fruitless since you wouldn't make it in time....most likely.

      Although it's a pretty interesting format for the for the coding lol.


      EDIT:

      So it won't let me edit my original post so if someone is capable, it would be apprecitated if it could be updated as so:

       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
       <html>
            <head>
            <title>PC Power Manager</title>
            <HTA:APPLICATION
                applicationName = "PC Power Manager"
                singleInstance = "yes"
                showInTaskbar = "yes"
                scroll = "no"
                sysMenu = "no"
                contextMenu = "no"
                version = "2.4"
            >
       
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
       
            <style>
                body{
                    background-color: buttonface;
                }
                p{
                    text-align: right;
                }
                button{
                    clear: both;
                }
                .success{
                    color: green;
                }
                .error{
                    color: red;
                    font-weight: bold;
                }
                span{
                    font-size: 18pt;
                }
                #left{
                    float: left;
                    width: 200px;
                }
                #right{
                    float: right;
                    width: 225px;
                }
            </style>
            
            <SCRIPT LANGUAGE="vbScript">
                OPTION EXPLICIT
                Const READ_ONLY = 1
                Const LOG_OFF = 0
                Const FORCE_LOGOFF = 4
                
                Sub Window_OnLoad()
                    Call CenterWindow(500, 475)
                    Call CreateDelay(30)
                    Call ChangeState
                    pcList.Focus()
                End Sub 'End Window_OnLoad()
                
                Sub CenterWindow(intWidth, intHeight)
                    self.ResizeTo intWidth, intHeight
                    self.MoveTo (screen.Width - intWidth)/2, (screen.Height - intHeight)/2
                End Sub 'End CenterWindow(intWidth, intHeight)
                
                Sub CreateDelay(intMaxTime)
                    Dim i
                    For i=1 to intMaxTime
                        Dim objOption : Set objOption = Document.CreateElement("option")
                        objOption.value = "-t " & i
                        objOption.Text = i
                        sel_Delay.Add(objOption)
                    Next
                End Sub 'End GetDelay()
                
                Function CreateCmd()
                    If Document.GetElementByID("logoff").Checked = True Then
                        strNewCmd = ""
                    Else
                        Dim objTask
                        Dim strNewCmd : strNewCmd = "shutdown.exe "
                        Dim arrTasks : Set arrTasks = Document.GetElementsByName("rad_shutdownOpt")
                    
                        For each objTask In arrTasks
                            If objTask.Checked Then
                                strNewCmd = strNewCmd & objTask.Value
                            End If
                        Next
                    
                        If Document.GetElementByID("chk_Force").Checked Then
                            strNewCmd = strNewCmd & Document.GetElementByID("chk_Force").Value
                        End If
                    
                        strNewCmd = strNewCmd & Document.GetElementByID("sel_Delay").Value
                        If Document.GetElementByID("txt_Message").Value = "" Then
                            strNewCmd = strNewCmd & " -c " & chr(34) & "This is a planned event." & chr(34)
                        Else
                            strNewCmd = strNewCmd & " -c " & chr(34) & Document.GetElementByID("txt_Message").Value & chr(34)
                        End If
                        Set arrTasks = Nothing
                        CreateCmd = strNewCmd
                    End If
                End Function 'End CreateCmd()
                
                Sub Execute_OnClick()
                    Dim strComputer, intProcessID
                    Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
                    Dim strCommand : strCommand = CreateCmd()
                    Dim arrComputers : arrComputers = Split(pcList.Value, vbCrLf)
                    If UBound(arrComputers) < 0 Then
                        Msgbox "At least one remote PC required!", vbExclamation, "Remote PC's - Error"
                        pcList.Focus()
                    Else
                        For Each strComputer In arrComputers
                            If strComputer = objNetwork.ComputerName Then
                                spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & " execution aborted for localhost!</font>"
                            Else
                                Dim blnExecuteNext : blnExecuteNext = ValidatePC(strComputer)
                                If blnExecuteNext = True Then
                                    Dim objWMI : Set objWMI = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
                                
                                    If strCommand <> "" Then
                                        Dim objProcess : Set objProcess = objWMI.Get("Win32_Process")
                                        Dim intReturn : intReturn = objProcess.Create(strCommand, Null, , intProcessID)
                                        If intReturn <> 0 Then
                                            spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & "........Failed!</font>"
                                        Else
                                            spn_Status.InnerHTML = "<font class='success'>" & UCase(strComputer) & "........Success!</font>"
                                        End If
                                        Set objProcess = Nothing
                                    Else
                                        Dim objOSItem
                                        Dim colOS : Set colOS = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem")
                                        For Each objOSItem In colOS
                                            Select Case Document.GetElementByID("chk_Force").Checked
                                                Case "True"
                                                    intReturn = objOSItem.Win32Shutdown(FORCE_LOGOFF)
                                                Case Else
                                                    intReturn = objOSItem.Win32Shutdown(LOG_OFF)
                                            End Select
       
                                            Select Case intReturn
                                                Case "0"
                                                    spn_Status.InnerHTML = "<font class='success'>" & UCase(strComputer) & "........Success!</font>"
                                                Case Else
                                                    spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & "........Failed!</font>"
                                            End Select
                                        Next
                                        Set objOSItem = Nothing
                                        Set colOS = Nothing
                                    End If 'End Shutdown Opt Check
                                End If 'End ValidatePC Check
                            End If 'End localhost Check
                        Next
                        Set objWMI = Nothing
                        Set objNetwork = Nothing
                    End If
                End Sub 'End Execute_OnClick()
                
                Function ValidatePC(strComputer)
                     If strComputer = "" Then
                        ValidatePC = False
                        Exit Function
                    End If
                    
                    ON ERROR RESUME NEXT
                    Dim colPing : Set colPing = GetObject("winmgmts:\\").ExecQuery("Select * from Win32_PingStatus where Address='" & strComputer & "'")
                    Dim objPingStatus
       
                    For Each objPingStatus in colPing
                        If objPingStatus.StatusCode = 0 Then
                            If IsNull(GetObject("winmgmts:\\" & strComputer & "\root\cimv2")) Then
                                spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & "........Failed to access!</font>"
                                 ValidatePC = False
                            Else
                                ValidatePC = True
                            End If
                        Else
                            spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & "........Failed to connect!</font>"
                            ValidatePC = False
                        End If
                    Next
                    Set objPingStatus = Nothing
                    Set colPing = Nothing
                End Function 'End ValidatePC(strComputer)
                
                Sub LoadFile_OnClick()
                    Dim objDialog : Set objDialog = CreateObject("UserAccounts.CommonDialog")
        
                    With objDialog
                        .Filter = "Text Files (*.txt)|*.txt"
                        .FilterIndex = 1
                        .InitialDir = "C:\"
                    End With
                    objDialog.ShowOpen
       
                    If objDialog.FileName <> "" Then
                        Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
                        If objFSO.FileExists(objDialog.FileName) Then
                            Dim objFile : Set objFile = objFSO.OpenTextFile(objDialog.FileName, READ_ONLY)
                            pcList.Value = ""
                            Do Until objFile.AtEndOfStream
                                Dim strLine : strLine = TRIM(objFile.ReadLine)
                                pcList.Value = pcList.Value & strLine & vbCrLf
                            Loop
                        Else
                            Msgbox objDialog.FileName & " not found!", vbExclamation,"Open File - Error"
                        End If
                    End If
                    Set objFile = Nothing
                    Set objFSO = Nothing
                    Set objDialog = Nothing
                End Sub 'End GetFile_OnClick()
                
                Sub ChangeState()
                    If Document.GetElementByID("logoff").Checked = True Then
                        Document.GetElementByID("sel_Delay").Disabled = True
                        Document.GetElementByID("txt_Message").Disabled = True
                    Else
                        Document.GetElementByID("sel_Delay").Disabled = False
                        Document.GetElementByID("txt_Message").Disabled = False
                    End If
                End Sub 'End ChangeState()
       
                Sub Exit_OnClick()
                    self.Close()
                End Sub 'End CloseApplication()
            </SCRIPT>
            </head>
            
            <body>
            <div id="left">
                Remote PC(s):
                <br>
                <textarea id="pcList" rows="17" cols="25"></textarea>
                <br>
            </div>
            <div id="right">
                <fieldset>
                    <legend>Task</legend>
                        <input type="radio" name="rad_shutdownOpt" id="logoff" checked onClick="ChangeState"/>Logoff<br>
                        <input type="radio" name="rad_shutdownOpt" value="-r " onClick="ChangeState"/>Restart<br>
                        <input type="radio" name="rad_shutdownOpt" value="-s " onClick="ChangeState"/>Shutdown<br>
                        <br>
                        <input type="checkbox" id="chk_force" value="-f " />Force
                </fieldset>
                <br>
                <fieldset>
                    <legend>Delay</legend>
                        <select id="sel_Delay"></select>Second(s)
                </fieldset>
                <br>
                <fieldset>
                    <legend>Display Message</legend>
                        <textarea id="txt_Message" rows="4" cols="24" ></textarea>
                </fieldset>
            </div>
            <p>
                <button name="LoadFile" accesskey="l"><u>L</u>oad File...</button>&nbsp
                <button name="Execute" accesskey="e"><u>E</u>xecute</button>&nbsp
                <button name="Exit" accesskey="x">E<u>x</u>it</button>&nbsp
            </p>
            <span id="spn_Status"></span>
            </body>
       </html>
       



      Something I forgot to add was disabling the actually shutdown/restart/logoff process to take place on the localhost.  Obviously it doesn't make much sense to do that now does it and someone could load a file and not realize their PC is one of them.
      <message edited by Wakawaka on Thursday, October 29, 2009 2:55 AM>
       
      #3
        dazlak

        • Total Posts : 10
        • Scores: 0
        • Reward points : 0
        • Joined: 11/9/2009
        • Status: offline
        Re:Remote Logoff, restart, and shutdown PCs Monday, November 09, 2009 4:31 PM (permalink)
        0
        will this work for over IP remote computer?
         
        #4
          Wakawaka

          • Total Posts : 456
          • Scores: 23
          • Reward points : 0
          • Joined: 8/27/2009
          • Status: offline
          Re:Remote Logoff, restart, and shutdown PCs Tuesday, November 10, 2009 12:09 AM (permalink)
          0
          As long as what you are connecting to is in the same group as it.

          Also, something interesting, perhaps it's just at my work..., but it might say it failed but if you create a folder on the remote PC with your LAN ID it will work fine. 

          Still trying to figure that one out....
           
          #5
            Wakawaka

            • Total Posts : 456
            • Scores: 23
            • Reward points : 0
            • Joined: 8/27/2009
            • Status: offline
            Re:Remote Logoff, restart, and shutdown PCs Thursday, November 12, 2009 5:17 AM (permalink)
            0
            So this is an updated version.  It seems to create processes, it requires certain registry files which needs an user Profile.  If you create a empty profile with your userID, Windows will create the required files there to access the registry keys required.

            So This will check if your profile is there, if it is it will continue, if it isn't, it will create it and then it will delete it after the process completes so it will execute the process on all PC's regardless.  Of course, I'm sure there is a better way but to have the functionality I wanted, it ended up like this.

             
             <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
             <html> 
                  <!-- 
                  '******************************************************************** 
                  '*  File:          PC Power Manager.hta 
                  '*  Author:        Justin Thomas 
                  '*  Created:       Oct 2009 
                  '*  Modified:      Oct 2009 
                  '*                    Nov 2009 
                  '*  Version:       2.4 
                  '******************************************************************** 
                  --> 
                  <head> 
                  <title>PC Power Manager</title> 
                  <HTA:APPLICATION 
                      applicationName = "PC Power Manager" 
                      singleInstance = "yes" 
                      showInTaskbar = "yes" 
                      border = "thin" 
                      scroll = "no" 
                      maximizeButton = "no" 
                      contextMenu = "no" 
                      version = "2.6" 
                  > 
             
                 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
             
                  <style> 
                      body{ 
                          background-color: buttonface; 
                      } 
                      p{ 
                          text-align: right; 
                      } 
                      button{ 
                          clear: both; 
                      } 
                      .success{ 
                          color: green; 
                      } 
                      .error{ 
                          color: red; 
                          font-weight: bold; 
                      } 
                      span{ 
                          font-size: 18pt; 
                      } 
                      #left{ 
                          float: left; 
                          width: 200px; 
                      } 
                      #right{ 
                          float: right; 
                          width: 225px; 
                      } 
                  </style> 
                   
                  <SCRIPT LANGUAGE="vbScript"> 
                      OPTION EXPLICIT 
                      Const READ_ONLY = 1 
                      Const LOG_OFF = 0 
                      Const FORCE_LOGOFF = 4 
                       
                      Sub Window_OnLoad() 
                          Call CenterWindow(500, 475) 
                          Call CreateDelay(30) 
                          Call ChangeState 
                          pcList.Focus() 
                      End Sub 'End Window_OnLoad() 
                       
                      Sub CenterWindow(intWidth, intHeight) 
                          self.ResizeTo intWidth, intHeight 
                          self.MoveTo (screen.Width - intWidth)/2, (screen.Height - intHeight)/2 
                      End Sub 'End CenterWindow(intWidth, intHeight) 
                       
                      Sub CreateDelay(intMaxTime) 
                          Dim i 
                          For i=1 to intMaxTime 
                              Dim objOption : Set objOption = Document.CreateElement("option") 
                              objOption.value = "-t " & i 
                              objOption.Text = i 
                              sel_Delay.Add(objOption) 
                          Next 
                      End Sub 'End GetDelay() 
                       
                      Function CreateCmd() 
                          If Document.GetElementByID("logoff").Checked = True Then 
                              strNewCmd = "" 
                          Else 
                              Dim objTask 
                              Dim strNewCmd : strNewCmd = "shutdown.exe " 
                              Dim arrTasks : Set arrTasks = Document.GetElementsByName("rad_shutdownOpt") 
                           
                              For each objTask In arrTasks 
                                  If objTask.Checked Then 
                                      strNewCmd = strNewCmd & objTask.Value 
                                  End If 
                              Next 
                           
                              If Document.GetElementByID("chk_Force").Checked Then 
                                  strNewCmd = strNewCmd & Document.GetElementByID("chk_Force").Value 
                              End If 
                           
                              strNewCmd = strNewCmd & Document.GetElementByID("sel_Delay").Value 
                              If Document.GetElementByID("txt_Message").Value = "" Then 
                                  strNewCmd = strNewCmd & " -c " & chr(34) & "This is a planned event." & chr(34) 
                              Else 
                                  strNewCmd = strNewCmd & " -c " & chr(34) & Document.GetElementByID("txt_Message").Value & chr(34) 
                              End If 
                              Set arrTasks = Nothing 
                          End If 
                          CreateCmd = strNewCmd 
                      End Function 'End CreateCmd() 
                       
                      Sub Execute_OnClick() 
                          Dim strComputer, intProcessID 
                          Dim objNetwork : Set objNetwork = CreateObject("WScript.Network") 
                          Dim strCommand : strCommand = CreateCmd() 
                          Dim arrComputers : arrComputers = Split(pcList.Value, vbCrLf) 
                          If UBound(arrComputers) < 0 Then 
                              Msgbox "At least one remote PC required!", vbExclamation, "Remote PC's - Error" 
                              pcList.Focus() 
                          Else 
                              Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") 
                              For Each strComputer In arrComputers 
                                  If strComputer = objNetwork.ComputerName Then 
                                      spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & " execution aborted for localhost!</font>" 
                                  Else 
                                      Dim blnExecuteNext : blnExecuteNext = ValidatePC(strComputer) 
                                      If blnExecuteNext = True Then 
                                          If Not(objFSO.FolderExists("\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName)) Then 
                                              objFSO.CreateFolder "\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName 
                                          End If 
                                          Dim objWMI : Set objWMI = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2") 
                                       
                                          If strCommand <> "" Then 
                                              Dim objProcess : Set objProcess = objWMI.Get("Win32_Process") 
                                              Dim intReturn : intReturn = objProcess.Create(strCommand, Null, , intProcessID) 
                                              If intReturn <> 0 Then 
                                                  spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & "........Failed!</font>" 
                                              Else 
                                                  spn_Status.InnerHTML = "<font class='success'>" & UCase(strComputer) & "........Success!</font>" 
                                              End If 
                                              Set objProcess = Nothing 
                                          Else 
                                              Dim objOSItem 
                                              Dim colOS : Set colOS = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem") 
                                              For Each objOSItem In colOS 
                                                  Select Case Document.GetElementByID("chk_Force").Checked 
                                                      Case "True" 
                                                          intReturn = objOSItem.Win32Shutdown(FORCE_LOGOFF) 
                                                      Case Else 
                                                          intReturn = objOSItem.Win32Shutdown(LOG_OFF) 
                                                  End Select 
             
                                                  Select Case intReturn 
                                                      Case "0" 
                                                          spn_Status.InnerHTML = "<font class='success'>" & UCase(strComputer) & "........Success!</font>" 
                                                      Case Else 
                                                          spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & "........Failed!</font>" 
                                                  End Select 
                                              Next 
                                              Set objOSItem = Nothing 
                                              Set colOS = Nothing 
                                          End If 'End Shutdown Opt Check 
                                      End If 'End ValidatePC Check 
                                  End If 'End localhost Check 
                                  On Error Resume Next 
                                  objFSO.DeleteFolder "\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName, True 
                              Next 
                              Set objFSO = Nothing 
                              Set objWMI = Nothing 
                              Set objNetwork = Nothing
                              Err.Clear
                          End If 
                      End Sub 'End Execute_OnClick() 
                       
                      Function ValidatePC(strComputer) 
                           If strComputer = "" Then 
                              ValidatePC = False 
                              Exit Function 
                          End If 
                           
                          ON ERROR RESUME NEXT 
                          Dim colPing : Set colPing = GetObject("winmgmts:\\").ExecQuery("Select * from Win32_PingStatus where Address='" & strComputer & "'") 
                          Dim objPingStatus 
             
                          For Each objPingStatus in colPing 
                              If objPingStatus.StatusCode = 0 Then 
                                  If IsNull(GetObject("winmgmts:\\" & strComputer & "\root\cimv2")) Then 
                                      spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & "........Failed to access!</font>" 
                                       ValidatePC = False 
                                  Else 
                                      ValidatePC = True 
                                  End If 
                              Else 
                                  spn_Status.InnerHTML = "<font class='error'>" & UCase(strComputer) & "........Failed to connect!</font>" 
                                  ValidatePC = False 
                              End If 
                          Next 
                          Set objPingStatus = Nothing 
                          Set colPing = Nothing 
                      End Function 'End ValidatePC(strComputer) 
                       
                      Sub LoadFile_OnClick() 
                          Dim objDialog : Set objDialog = CreateObject("UserAccounts.CommonDialog") 
               
                          With objDialog 
                              .Filter = "Text Files (*.txt)|*.txt" 
                              .FilterIndex = 1 
                              .InitialDir = "C:\" 
                          End With 
                          objDialog.ShowOpen 
             
                          If objDialog.FileName <> "" Then 
                              Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") 
                              If objFSO.FileExists(objDialog.FileName) Then 
                                  Dim objFile : Set objFile = objFSO.OpenTextFile(objDialog.FileName, READ_ONLY) 
                                  pcList.Value = "" 
                                  Do Until objFile.AtEndOfStream 
                                      Dim strLine : strLine = TRIM(objFile.ReadLine) 
                                      pcList.Value = pcList.Value & strLine & vbCrLf 
                                  Loop 
                              Else 
                                  Msgbox objDialog.FileName & " not found!", vbExclamation,"Open File - Error" 
                              End If 
                          End If 
                          Set objFile = Nothing 
                          Set objFSO = Nothing 
                          Set objDialog = Nothing 
                      End Sub 'End GetFile_OnClick() 
                       
                      Sub ChangeState() 
                          If Document.GetElementByID("logoff").Checked = True Then 
                              Document.GetElementByID("sel_Delay").Disabled = True 
                              Document.GetElementByID("txt_Message").Disabled = True 
                          Else 
                              Document.GetElementByID("sel_Delay").Disabled = False 
                              Document.GetElementByID("txt_Message").Disabled = False 
                          End If 
                      End Sub 'End ChangeState() 
             
                      Sub Exit_OnClick() 
                          self.Close() 
                      End Sub 'End CloseApplication() 
                  </SCRIPT> 
                  </head> 
                   
                  <body> 
                  <div id="left"> 
                      Remote PC(s): 
                      <br> 
                      <textarea id="pcList" rows="17" cols="25"></textarea> 
                      <br> 
                  </div> 
                  <div id="right"> 
                      <fieldset> 
                          <legend>Task</legend> 
                              <input type="radio" name="rad_shutdownOpt" id="logoff" checked onClick="ChangeState"/>Logoff<br> 
                              <input type="radio" name="rad_shutdownOpt" value="-r " onClick="ChangeState"/>Restart<br> 
                              <input type="radio" name="rad_shutdownOpt" value="-s " onClick="ChangeState"/>Shutdown<br> 
                              <br> 
                              <input type="checkbox" id="chk_force" value="-f " />Force 
                      </fieldset> 
                      <br> 
                      <fieldset> 
                          <legend>Delay</legend> 
                              <select id="sel_Delay"></select>Second(s) 
                      </fieldset> 
                      <br> 
                      <fieldset> 
                          <legend>Display Message</legend> 
                              <textarea id="txt_Message" rows="4" cols="24" ></textarea> 
                      </fieldset> 
                  </div> 
                  <p> 
                      <button name="LoadFile" accesskey="l"><u>L</u>oad File...</button>&nbsp 
                      <button name="Execute" accesskey="e"><u>E</u>xecute</button>&nbsp 
                      <button name="Exit" accesskey="x">E<u>x</u>it</button>&nbsp 
                  </p> 
                  <span id="spn_Status"></span> 
                  </body> 
             </html> 
             

            <message edited by Wakawaka on Thursday, November 12, 2009 5:19 AM>
             
            #6
              mcds99

              • Total Posts : 519
              • Scores: 4
              • Reward points : 0
              • Joined: 2/28/2006
              • Status: offline
              Re:Remote Logoff, restart, and shutdown PCs Tuesday, December 01, 2009 8:36 AM (permalink)
              0
              One thing I noticed was the in-line DIM's - OUCH! -
               
              There is no rule that says don't ( DIM fred : SET fred... )
                                                                ( DIM barny : SET barny... )
              At the start of the Function or Sub DIM the variables used within that Function or Sub, it makes it easier to read the code.
                  DIM fred, barny
                  Set fred...
                  Set barny...
               
              It's just a matter of form really but when trying to read the code it is easier to understand when the variables are defined first.
              Sam

              Keep it Simple Make it Fun KiSMiF
               
              #7
                Wakawaka

                • Total Posts : 456
                • Scores: 23
                • Reward points : 0
                • Joined: 8/27/2009
                • Status: offline
                Re:Remote Logoff, restart, and shutdown PCs Wednesday, December 02, 2009 5:14 AM (permalink)
                0
                I prefer to declare when I use them, unless unavoidable.
                 
                #8
                  ebgreen

                  • Total Posts : 8227
                  • Scores: 98
                  • Reward points : 0
                  • Joined: 7/12/2005
                  • Status: offline
                  Re:Remote Logoff, restart, and shutdown PCs Wednesday, December 02, 2009 5:41 AM (permalink)
                  0
                  Well I will weigh in here. For standard variables that I use in every script, I do it inline:

                  Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
                  Dim oWSH : Set oWSH = CreateObject("WScript.Shell")
                  etc.

                  There are a couple of reasons that I do this. First whenever I do Dim oFSO, there is one and only one assignment that will at some point follow that. So I just put it right there with the Dim. Second, these statements are so ubiquitous that in reality for me anyway, they are just noise. Doing them inline let's me make the block of noise smaller and easier to ignore.

                  For all other variables. I follow a declare at the top methodology. This is sort of old school and I under stand and have no problem with the "Declare at use" philosophy. Any potential problems with the declare at the top methodology are mitigated through other best practices that I try to adhere to like minimize global variables, self documenting variable names, and small (less than a one screen size) procedures.
                  "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
                  Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
                  http://www.visualbasicscript.com/m_47117/tm.htm
                   
                  #9
                    Wakawaka

                    • Total Posts : 456
                    • Scores: 23
                    • Reward points : 0
                    • Joined: 8/27/2009
                    • Status: offline
                    Re:Remote Logoff, restart, and shutdown PCs Thursday, December 17, 2009 3:00 AM (permalink)
                    0
                    So this is yet another update.  This have more in-depth error checking and the span would only show the last results of the PC on the list, so in place of that, there is now a scrolling textarea with the results of the operation.

                    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                     <html>
                          <!--
                          '********************************************************************
                          '*  File:          PC Power Manager.hta
                          '*  Author:        Justin Thomas
                          '*  Created:       Oct 2009
                          '*  Modified:      Oct 2009
                          '*                    Nov 2009
                          '*                    Dec 2009
                          '*  Version:       2.8
                          '********************************************************************
                          -->
                          <head>
                          <title>PC Power Manager</title>
                          <HTA:APPLICATION
                              applicationName = "PC Power Manager"
                              singleInstance = "yes"
                              showInTaskbar = "yes"
                              border = "thin"
                              scroll = "no"
                              maximizeButton = "no"
                              contextMenu = "no"
                              version = "2.8"
                          >
                     
                         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                     
                          <style>
                              body{
                                  background-color: buttonface;
                              }
                              p{
                                  text-align: right;
                              }
                              button{
                                  clear: both;
                              }
                              span{
                                  font-size: 18pt;
                              }
                              #left{
                                  float: left;
                                  width: 200px;
                              }
                              #right{
                                  float: right;
                                  width: 225px;
                              }
                          </style>
                          
                          <SCRIPT LANGUAGE="vbScript">
                              OPTION EXPLICIT
                              Const READ_ONLY = 1
                              Const LOG_OFF = 0
                              Const FORCE_LOGOFF = 4
                              
                              Sub Window_OnLoad()
                                  Call CenterWindow(500, 475)
                                  Call CreateDelay(30)
                                  Call ChangeState
                                  pcList.Focus()
                              End Sub 'End Window_OnLoad()
                              
                              Sub CenterWindow(intWidth, intHeight)
                                  self.ResizeTo intWidth, intHeight
                                  self.MoveTo (screen.Width - intWidth)/2, (screen.Height - intHeight)/2
                              End Sub 'End CenterWindow(intWidth, intHeight)
                              
                              Sub CreateDelay(intMaxTime)
                                  Dim i
                                  For i=5 to intMaxTime
                                      Dim objOption : Set objOption = Document.CreateElement("option")
                                      objOption.value = "-t " & i
                                      objOption.Text = i
                                      sel_Delay.Add(objOption)
                                  Next
                              End Sub 'End GetDelay()
                              
                              Function CreateCmd()
                                  If Document.GetElementByID("logoff").Checked = True Then
                                      strNewCmd = ""
                                  Else
                                      Dim objTask
                                      Dim strNewCmd : strNewCmd = "shutdown.exe "
                                      Dim arrTasks : Set arrTasks = Document.GetElementsByName("rad_shutdownOpt")
                                  
                                      For each objTask In arrTasks
                                          If objTask.Checked Then
                                              strNewCmd = strNewCmd & objTask.Value
                                          End If
                                      Next
                                  
                                      If Document.GetElementByID("chk_Force").Checked Then
                                          strNewCmd = strNewCmd & Document.GetElementByID("chk_Force").Value
                                      End If
                                  
                                      strNewCmd = strNewCmd & Document.GetElementByID("sel_Delay").Value
                                      If Document.GetElementByID("txt_Message").Value = "" Then
                                          strNewCmd = strNewCmd & " -c " & chr(34) & "This is a planned event." & chr(34)
                                      Else
                                          strNewCmd = strNewCmd & " -c " & chr(34) & Document.GetElementByID("txt_Message").Value & chr(34)
                                      End If
                                      Set arrTasks = Nothing
                                  End If
                                  CreateCmd = strNewCmd
                              End Function 'End CreateCmd()
                              
                              Sub Execute_OnClick()
                                  Dim strComputer, intProcessID
                                  Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
                                  Dim strCommand : strCommand = CreateCmd()
                                  Dim arrComputers : arrComputers = Split(pcList.Value, vbCrLf)
                                  txt_Results.Value = ""
                                  If UBound(arrComputers) < 0 Then
                                      Msgbox "At least one remote PC required!", vbApplicationModal & vbExclamation, "Remote PC's - Error"
                                      pcList.Focus()
                                  Else
                                      Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
                                      For Each strComputer In arrComputers
                                          If UCase(strComputer) = UCase(objNetwork.ComputerName) Then
                                              txt_Results.Value = UCase(strComputer) & " is the localhost, command aborted" & vbCrLf & txt_Results.Value
                                          Else
                                              Dim blnExecuteNext : blnExecuteNext = ValidatePC(strComputer)
                                              If blnExecuteNext = True Then
                                                  Dim objWMI : Set objWMI = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
                                                  
                                                  On Error Resume Next
                                                  If Not(objFSO.FolderExists("\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName)) Then
                                                      objFSO.CreateFolder "\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName
                                                  End If
                                                  
                                                  If strCommand <> "" Then
                                                      Dim objProcess : Set objProcess = objWMI.Get("Win32_Process")
                                                      objProcess.Create strCommand, Null, , intProcessID
                                                      Select Case Err.Number
                                                      Case "0"
                                                          txt_Results.Value = UCase(strComputer) & " successfully created shutdown process" & vbCrLf & txt_Results.Value
                                                      Case "2"
                                                          txt_Results.Value = UCase(strComputer) & " denied access to create proccess" & vbCrLf & txt_Results.Value
                                                      Case "3"
                                                          txt_Results.Value = "You do not have sufficient privileges to create processes on " & Ucase(strComputer) & vbCrLf & txt_Results.Value
                                                      Case "9"
                                                          txt_Results.Value = UCase(strComputer) & " could not find the specificed execution path" & vbCrLf & txt_Results.Value
                                                      Case "21"
                                                          txt_Results.Value = UCase(strComputer) & " was passed and invalid parameter" & vbCrLf & txt_Results.Value
                                                      Case Else
                                                          txt_Results.Value = "An unknown error has occured on " & UCase(strComputer) & vbCrLf & txt_Results.Value
                                                      End Select
                                                      Set objProcess = Nothing
                                                  Else
                                                      Dim objOSItem
                                                      Dim colOS : Set colOS = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem")
                                                      For Each objOSItem In colOS
                                                          Select Case Document.GetElementByID("chk_Force").Checked
                                                              Case "True"
                                                                  objOSItem.Win32Shutdown FORCE_LOGOFF
                                                              Case Else
                                                                  objOSItem.Win32Shutdown LOG_OFF
                                                          End Select
                     
                                                          Select Case Err.Number
                                                              Case "0"
                                                                  txt_Results.Value = UCase(strComputer) & " successfully created shutdown process" & vbCrLf & txt_Results.Value
                                                              Case Else
                                                                  txt_Results.Value = "An unknown error has occured on " & UCase(strComputer) & vbCrLf & txt_Results.Value
                                                          End Select
                                                      Next
                                                      Set objOSItem = Nothing
                                                      Set colOS = Nothing
                                                  End If 'End Shutdown Opt Check
                                              End If 'End ValidatePC Check
                                          End If 'End localhost Check
                                          On Error Resume Next
                                          objFSO.DeleteFolder "\\" & strComputer & " \C$\Documents and Settings\" & objNetwork.UserName, True
                                      Next
                                      Set objFSO = Nothing
                                      Set objWMI = Nothing
                                      Set objNetwork = Nothing
                                      Err.Clear
                                  End If
                              End Sub 'End Execute_OnClick()
                              
                              Function ValidatePC(strComputer)
                                   If strComputer = "" Then
                                      ValidatePC = False
                                      Exit Function
                                  End If
                                  
                                  ON ERROR RESUME NEXT
                                  Dim colPing : Set colPing = GetObject("winmgmts:\\").ExecQuery("Select * from Win32_PingStatus where Address='" & strComputer & "'")
                                  Dim objPingStatus
                     
                                  For Each objPingStatus in colPing
                                      If objPingStatus.StatusCode = 0 Then
                                          If IsNull(GetObject("winmgmts:\\" & strComputer & "\root\cimv2")) Then
                                              txt_Results.Value = UCase(strComputer) & " denied access to WMI." & vbCrLf & txt_Results.Value
                                               ValidatePC = False
                                          Else
                                              ValidatePC = True
                                          End If
                                      Else
                                          txt_Results.Value = UCase(strComputer) & " did not respond to ping requests" & vbCrLf & txt_Results.Value
                                          ValidatePC = False
                                      End If
                                  Next
                                  Set objPingStatus = Nothing
                                  Set colPing = Nothing
                              End Function 'End ValidatePC(strComputer)
                              
                              Sub LoadFile_OnClick()
                                  Dim objDialog : Set objDialog = CreateObject("UserAccounts.CommonDialog")
                      
                                  With objDialog
                                      .Filter = "Text Files (*.txt)|*.txt"
                                      .FilterIndex = 1
                                      .InitialDir = "C:\"
                                  End With
                                  objDialog.ShowOpen
                     
                                  If objDialog.FileName <> "" Then
                                      Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
                                      If objFSO.FileExists(objDialog.FileName) Then
                                          Dim objFile : Set objFile = objFSO.OpenTextFile(objDialog.FileName, READ_ONLY)
                                          pcList.Value = ""
                                          Do Until objFile.AtEndOfStream
                                              Dim strLine : strLine = TRIM(objFile.ReadLine)
                                              pcList.Value = pcList.Value & strLine & vbCrLf
                                          Loop
                                      Else
                                          Msgbox objDialog.FileName & " not found!", vbApplicationModal & vbCritical,"Open File - Error"
                                      End If
                                  End If
                                  Set objFile = Nothing
                                  Set objFSO = Nothing
                                  Set objDialog = Nothing
                              End Sub 'End GetFile_OnClick()
                              
                              Sub ChangeState()
                                  If Document.GetElementByID("logoff").Checked = True Then
                                      Document.GetElementByID("sel_Delay").Disabled = True
                                      Document.GetElementByID("txt_Message").Disabled = True
                                  Else
                                      Document.GetElementByID("sel_Delay").Disabled = False
                                      Document.GetElementByID("txt_Message").Disabled = False
                                  End If
                              End Sub 'End ChangeState()
                     
                              Sub Exit_OnClick()
                                  self.Close()
                              End Sub 'End CloseApplication()
                          </SCRIPT>
                          </head>
                          
                          <body>
                          <div id="left">
                              Remote PC(s):
                              <br>
                              <textarea id="pcList" rows="17" cols="25"></textarea>
                              <br>
                          </div>
                          <div id="right">
                              <fieldset>
                                  <legend>Task</legend>
                                      <input type="radio" name="rad_shutdownOpt" id="logoff" checked onClick="ChangeState"/>Logoff<br>
                                      <input type="radio" name="rad_shutdownOpt" value="-r " onClick="ChangeState"/>Restart<br>
                                      <input type="radio" name="rad_shutdownOpt" value="-s " onClick="ChangeState"/>Shutdown<br>
                                      <br>
                                      <input type="checkbox" id="chk_force" value="-f " />Force
                              </fieldset>
                              <br>
                              <fieldset>
                                  <legend>Delay</legend>
                                      <select id="sel_Delay"></select>Second(s)
                              </fieldset>
                              <br>
                              <fieldset>
                                  <legend>Display Message</legend>
                                      <textarea id="txt_Message" rows="4" cols="24" ></textarea>
                              </fieldset>
                          </div>
                          <p>
                              <button name="LoadFile" accesskey="l"><u>L</u>oad File...</button>&nbsp
                              <button name="Execute" accesskey="e"><u>E</u>xecute</button>&nbsp
                              <button name="Exit" accesskey="x">E<u>x</u>it</button>&nbsp
                          </p>
                              <textarea id="txt_Results" cols="56" READONLY></textarea>
                          </body>
                     </html>


                    Admin:  Is it possible to consolidate these posts and only have the most recent version posted so people don't get confused?  Thanks!
                    <message edited by Wakawaka on Thursday, December 17, 2009 3:34 AM>
                     
                    #10

                      Online Bookmarks Sharing: Share/Bookmark

                      Jump to:

                      Current active users

                      There are 0 members and 1 guests.

                      Icon Legend and Permission

                      • New Messages
                      • No New Messages
                      • Hot Topic w/ New Messages
                      • Hot Topic w/o New Messages
                      • Locked w/ New Messages
                      • Locked w/o New Messages
                      • Read Message
                      • Post New Thread
                      • Reply to message
                      • Post New Poll
                      • Submit Vote
                      • Post reward post
                      • Delete my own posts
                      • Delete my own threads
                      • Rate post

                      2000-2012 ASPPlayground.NET Forum Version 3.9