Announcement

Collapse
No announcement yet.

Shutdown script at 17:00 startup at 08:30

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Shutdown script at 17:00 startup at 08:30

    Hi there,

    Does anyone know how to get a shutdown scripot that swutches off teh computers at 17;00 and boots them (wake on lan) at 08:00?

  • #2
    Re: Shutdown script at 17:00 startup at 08:30

    How many stations?
    You need the solution centralized, like running a script on a computer that will turn all the others off? Or it can reside on each and every computer?
    To shutdown, you can use the shutdown command.
    To wake them up, do you have a tool that came with the NIC's driver? If not, you can use this little app. It's free, I didn't checked it (got it from a friend some time ago and asked myself what to do with it ).
    Now, you only have to find the way to the computers' list. Will that be a limited list, that can be run with a loop? Or should it be something more serious?
    Hope this info helps.
    Good luck.

    Sorin Solomon


    In order to succeed, your desire for success should be greater than your fear of failure.
    -

    Comment


    • #3
      Re: Shutdown script at 17:00 startup at 08:30

      A very simple script would do:

      shutdown -s \\computername

      This would be scheduled at shutdown time to run on one of your servers.

      Then a similar wake up script would need a download of a little program called wol.exe (wake on lan) (google it)

      Then wol.exe works like:

      wol macaddress

      Or something like that.

      Hope this helps
      Server 2000 MCP
      Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

      Comment


      • #4
        Re: Shutdown script at 17:00 startup at 08:30

        Originally posted by tonyyeb View Post
        wol.exe (wake on lan) (google it)
        But only if you need badly to . If not, you can use the link in my reply, it's for wol.exe

        Sorin Solomon


        In order to succeed, your desire for success should be greater than your fear of failure.
        -

        Comment


        • #5
          Re: Shutdown script at 17:00 startup at 08:30

          Hmm just tried your link. Its dead.
          Server 2000 MCP
          Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

          Comment


          • #6
            Re: Shutdown script at 17:00 startup at 08:30


            They took off the file!! Darn !!
            I'll look for it and post. After all, we don't want to disappoint the customer, do we?

            Sorin Solomon


            In order to succeed, your desire for success should be greater than your fear of failure.
            -

            Comment


            • #7
              Re: Shutdown script at 17:00 startup at 08:30

              Hmmmmmm.
              Looks like it was a hickup. Now it is available.
              If there will be more problems, you can send me a personal message and I'll send it to you.
              See ya.

              Sorin Solomon


              In order to succeed, your desire for success should be greater than your fear of failure.
              -

              Comment


              • #8
                Re: Shutdown script at 17:00 startup at 08:30

                Just a couple of notes on the shutdown command.
                Originally posted by tonyyeb View Post
                A very simple script would do:

                shutdown -s \\computername
                You mean shutdown -s -m \\computername, right? And you might want to throw in /f and /t 0 for good measure.

                Also, I discovered that the shutdown command doesn't do a full shutdown.
                Take a look at this thread (though I make myself look like an idiot)
                http://forums.petri.com/showthread.php?t=11371&page=2

                Here's a shutdown utility that will do a full shutdown
                http://www.aumha.org/downloads/shutdown.zip
                Regards,
                Jeremy

                Network Consultant/Engineer
                Baltimore - Washington area and beyond
                www.gma-cpa.com

                Comment


                • #9
                  Re: Shutdown script at 17:00 startup at 08:30

                  stupid question maybe but how would I schedule this for all workstations at 17:00?

                  Comment


                  • #10
                    Re: Shutdown script at 17:00 startup at 08:30

                    Originally posted by Daveinholland View Post
                    stupid question maybe but how would I schedule this for all workstations at 17:00?
                    Include all your machines in the batch file and use task scheduler on a server? Thats how we do it.
                    Server 2000 MCP
                    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

                    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                    Comment


                    • #11
                      Re: Shutdown script at 17:00 startup at 08:30

                      Originally posted by JeremyW View Post
                      You mean shutdown -s -m \\computername, right?
                      Thats correct Jeremy. Half asleep when i was looking at the syntax.
                      Server 2000 MCP
                      Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

                      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                      Comment


                      • #12
                        Re: Shutdown script at 17:00 startup at 08:30

                        Originally posted by Daveinholland View Post
                        stupid question maybe but how would I schedule this for all workstations at 17:00?
                        Shedule to run a vb-script at 17:00 to shutdown the computers.
                        The script will;
                        - Query a certain OU, and enumerate all it's Computer Accounts
                        - And for those that are poweredOn, process a shutdown command.

                        example:
                        Code:
                        'script executer must be a member of the local group Administrators
                        'this script accepts also Credentials from command-line: /u:domain\admin /p:pass
                        
                        '(!)change the ldap string to match your active Directory configuration
                        strLDAP = "OU=Workstations,OU=MyComputers,DC=DOMAIN,DC=LOCAL"
                        
                        '(!)choose Shutdown option
                        '    0  = logoff
                        '    2  = reboot
                        '    4  = force logoff 
                        '    5  = force shutdown 
                        '    6  = force reboot
                        '    8  = powerdown 
                        '    12 = force power off 
                        oShutdown = "5"    '<-------- !!! set option here
                        
                        
                        'Read arguments
                        '------------------------------------------------------
                          sUsername = Null
                          sPassword = Null
                         Set Named = WScript.Arguments.Named
                         If Named.Exists("u") Then
                         sUsername = Named.Item("u")
                         End If
                         If Named.Exists("p") Then
                         sPassword = Named.Item("p")
                         End If
                        
                        
                        'Enumerate All Computer Accounts in Active Directory
                        '------------------------------------------------------
                        Const ADS_SCOPE_SUBTREE = 2
                        
                        Set objConnection = CreateObject("ADODB.Connection")
                        Set objCommand =   CreateObject("ADODB.Command")
                        objConnection.Provider = "ADsDSOObject"
                        objConnection.Open "Active Directory Provider"
                        
                        Set objCommand.ActiveConnection = objConnection
                        
                        objCommand.Properties("Page Size") = 1000
                        objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
                        
                        objCommand.CommandText = _
                            "Select Name, Location from 'LDAP://" & strLDAP & _
                            "' Where objectClass='computer'"
                        
                        Set objRecordSet = objCommand.Execute
                        
                         objRecordSet.MoveFirst
                        DO Until objRecordSet.EOF
                        
                         strComputer = objRecordSet.Fields("Name").Value
                         objRecordSet.MoveNext
                        
                         strPingStatus = PingStatus(strComputer)
                         On Error Resume next
                        
                        
                        If IsNull(sUsername) Or IsNull(sPassword) Then
                        '------------------------------------------------------
                        strAuth = "using current user credentials"
                          If strPingStatus = "Success" Then
                           Set OpSysSet = GetObject("winmgmts:{(Debug,RemoteShutdown)}//" _
                                     & strComputer & "/root/cimv2").ExecQuery( _
                                     "Select * from Win32_OperatingSystem where Primary=true")
                          RemoteShutdn()
                          End If
                        
                        Else 'If username and password given from command-line
                        '------------------------------------------------------
                        strAuth = "using credentials from the command-line"
                          If strPingStatus = "Success" Then
                           Set oLocator = CreateObject("WbemScripting.SWbemLocator") 
                           Set oConnection = oLocator.ConnectServer(strComputer, "root\cimv2", sUsername, sPassword) 
                           Set OpSysSet = oConnection.ExecQuery("Select " & "Name From Win32_OperatingSystem") 
                          RemoteShutdn()
                          End If
                        
                        End If
                        on error goto 0
                        
                        Loop
                        
                        Wscript.echo "done " &date&" "&time
                        WSCRIPT.QUIT(0)
                        
                        
                        'Process the shutdown
                        Function RemoteShutdn()
                           For Each opSys In OpSysSet
                            opSys.win32ShutDown(oShutdown)
                           next
                           Set objShell = WScript.CreateObject("WScript.Shell")
                           BtnCode = objShell.Popup(strComputer & " was found. " & strAuth, 2, "RemoteShutdown in progress", 64)
                              Select Case BtnCode
                                 case 6      '
                                 case 7      '
                                 case -1     '
                              End Select
                           Set objShell = Nothing
                        End Function
                        
                        Function PingStatus(strComputer)
                           Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
                           Set colPings = objWMIService.ExecQuery _
                             ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
                           For Each objPing in colPings
                               Select Case objPing.StatusCode
                                   Case 0 PingStatus = "Success"
                                   Case 11001 PingStatus = "Status code 11001 - Buffer Too Small"
                                   Case 11002 PingStatus = "Status code 11002 - Destination Net Unreachable"
                                   Case 11003 PingStatus = "Status code 11003 - Destination Host Unreachable"
                                   Case 11004 PingStatus = _
                                     "Status code 11004 - Destination Protocol Unreachable"
                                   Case 11005 PingStatus = "Status code 11005 - Destination Port Unreachable"
                                   Case 11006 PingStatus = "Status code 11006 - No Resources"
                                   Case 11007 PingStatus = "Status code 11007 - Bad Option"
                                   Case 11008 PingStatus = "Status code 11008 - Hardware Error"
                                   Case 11009 PingStatus = "Status code 11009 - Packet Too Big"
                                   Case 11010 PingStatus = "Status code 11010 - Request Timed Out"
                                   Case 11011 PingStatus = "Status code 11011 - Bad Request"
                                   Case 11012 PingStatus = "Status code 11012 - Bad Route"
                                   Case 11013 PingStatus = "Status code 11013 - TimeToLive Expired Transit"
                                   Case 11014 PingStatus = _
                                     "Status code 11014 - TimeToLive Expired Reassembly"
                                   Case 11015 PingStatus = "Status code 11015 - Parameter Problem"
                                   Case 11016 PingStatus = "Status code 11016 - Source Quench"
                                   Case 11017 PingStatus = "Status code 11017 - Option Too Big"
                                   Case 11018 PingStatus = "Status code 11018 - Bad Destination"
                                   Case 11032 PingStatus = "Status code 11032 - Negotiating IPSEC"
                                   Case 11050 PingStatus = "Status code 11050 - General Failure"
                                   Case Else PingStatus = "Status code " & objPing.StatusCode & _
                                      " - Unable to determine cause of failure."
                               End Select
                           Next
                        End Function
                        \Rem

                        This posting is provided "AS IS" with no warranties, and confers no rights.

                        __________________

                        ** Remember to give credit where credit's due **
                        and leave Reputation Points for meaningful posts

                        Comment


                        • #13
                          Re: Shutdown script at 17:00 startup at 08:30

                          thx a lot i am going to try this

                          Comment


                          • #14
                            Re: Shutdown script at 17:00 startup at 08:30

                            Great script Rems!
                            Marcel
                            Technical Consultant
                            Netherlands
                            http://www.phetios.com
                            http://blog.nessus.nl

                            MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                            "No matter how secure, there is always the human factor."

                            "Enjoy life today, tomorrow may never come."
                            "If you're going through hell, keep going. ~Winston Churchill"

                            Comment


                            • #15
                              Re: Shutdown script at 17:00 startup at 08:30

                              Wel , mine is not answering the complete question... yet

                              Before you can remotely Wake-up the computers, you proberably first have to set that option in the BIOS of all the clients
                              But the good thing is that after that, you can script the WOL process and schedule that too.

                              In this example I use "mc-wol.exe" to send the Magic Packets:
                              Code:
                              'Location of the files: 
                              strExecWOL = "...<path>...\mc-wol.exe"                   '<---- WOL program
                              strFileInput = "...<path>...\collected MACaddresses.csv" '<---- File contains MACaddresses
                              
                              
                              Const FOR_READING = 1
                               
                              'Start tracing Fields per Line now:
                              On Error Resume Next
                               Set objFSO = CreateObject("Scripting.FileSystemObject")
                              
                              'Breaking input stream (strInput) into array at line breaks.
                                strInput = GetInput(strFileInput)
                                arrRows = Split(strInput, VbCrLf)
                               
                              'Now breaking each Row (arrRows) into array at commas
                                 For Each strValue In arrRows
                                 arrParams = Split(strValue, ",")
                              
                               'getting value from first column
                                 strMACaddress = ""
                                 strMACaddress = arrParams(1 -1)
                                 strMACaddress = strMACaddress
                              
                               'getting value from fourth column  (...if you want to use the /a switch)
                                 strIPaddress = ""
                                 strIPaddress = arrParams(4 -1)
                                 strIPaddress = strIPaddress
                              
                              
                              'Wake-up remote Computers:
                              '-----------------------------
                              If strMACaddress = "" OR IsNull(strMACaddress) Then
                               'wscript.echo "One empty MACaddress field found: ", strMACaddress
                              Else
                              'http://www.matcode.com/wol.htm --> "mc-wol.exe"
                                Set objShell = CreateObject("WScript.Shell")
                                'Set objScriptExec = objShell.Exec(strExecWOL & " " & strMACaddress)
                                objShell.run  chr(34)&strExecWOL&chr(34) & " " & strMACaddress,1 ,False
                              
                              End IF
                              next
                              on error goto 0
                              
                              wscript.echo "  done!!!"
                              
                              'kill all stuck mc-wol.exe processes
                                strComputer = "."
                                strProcessKill = "'mc-wol.exe'" 
                                Set objWMIService = GetObject("winmgmts:" _
                                  & "{impersonationLevel=impersonate}!\\" _ 
                                  & strComputer & "\root\cimv2") 
                              
                                Set colProcess = objWMIService.ExecQuery _
                                  ("Select * from Win32_Process Where Name = " & strProcessKill )
                                For Each objProcess in colProcess
                                  objProcess.Terminate()
                                Next 
                              
                              Wscript.Quit
                              
                              
                              Function GetInput(strFileInput)
                              ' Check to see if the input file exists.
                              ' If so, dump contents of input file into a string.
                              If objFSO.FileExists(strFileInput) Then
                                Set objInputFile = objFSO.GetFile(strFileInput)
                                If objInputFile.Size > 0 Then
                                  Set objInputFile = objFSO.OpenTextFile(strFileInput, FOR_READING)
                                  strInputStream = objInputFile.ReadAll
                                  objInputFile.Close
                                  GetInput = strInputStream
                                Else
                                  Wscript.Echo strFileInput & " is empty."
                                  WScript.Quit
                                End If
                              Else
                                WScript.Echo strFileInput & " does not exist on this computer."
                                WScript.Quit
                              End If
                              End Function
                              The input file "collected MACaddresses.csv" that I used has 7 columns. But the wake-up script only uses the first field (containing the MACaddresses) of every row.

                              That inputfile can be automaticaly generated by the shutdown script [u]if you like[/]. By doing that, the same computers that where Shutteddown at night will automaticly start the next morning. And it will also give you the possibility to collect MACadresses which are needed for WOL.
                              You can realize this by adding this function at the end of the shutdown script:
                              Code:
                              '---------------------------------------
                              '  get MACadress from remote computer
                              'http://msdn2.microsoft.com/en-us/library/aa394217.aspx
                              '---------------------------------------
                              Function GetMACaddress(strNetSegment)
                              on error resume next
                              Set objWMIService = GetObject("winmgmts:" _
                               & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                              Set colNicConfigs = objWMIService.ExecQuery _
                               ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
                               
                                  strMACaddres = ""
                                  strDNSHostName = ""
                                  strDynamic = ""
                                  strIPAddress = "0.0.0.0"
                                  strNetMask = "0.0.0.0"
                                  StrDNSDomain = ""
                               
                              For Each objNicConfig In colNicConfigs
                                  subnet = ""
                                Set objNic = objWMIService.Get _
                                 ("Win32_NetworkAdapter.DeviceID=" & objNicConfig.Index)
                                For Each sIPAddress In objNicConfig.IPAddress
                                  address = sIPAddress
                                Next
                                For Each sIPSubnet In objNicConfig.IPSubnet
                                  subnet = sIPSubnet
                                Next
                              '------------------------------
                              'routine check for every adapter found on the computer, if it is used on the NetworkSegment
                              'http://www.microsoft.com/technet/prodtechnol/sms/sms2003/cpdg/plan4kv9.mspx?mfr=true
                              '(subnet.vbs - determine the subnet from IPaddress & subnetmask)
                                      WScript.Echo "IP address:  ", address
                                      WScript.Echo "subnet mask: ", subnet
                              
                                      dim addressbytes(4)
                                      dim subnetmaskbytes(4)
                                      i=0
                                      period = 1
                                      while period<>len( address ) + 2
                                             prevperiod=period
                                             period = instr( period+1, address, "." ) + 1
                                             if period = 1 then period = len( address ) + 2
                                                addressbyte = mid( address, prevperiod, period-prevperiod-1 )
                                                addressbytes(i)=addressbyte
                                             i=i+1
                                      wend
                                      i=0
                                      period = 1
                                      while period<>len( subnet ) + 2
                                             prevperiod=period
                                             period = instr( period+1, subnet, "." ) + 1
                                             if period = 1 then period = len( subnet ) + 2
                                                subnetmaskbyte = mid( subnet, prevperiod, period-prevperiod-1 )
                                                subnetmaskbytes(i)=subnetmaskbyte
                                            i=i+1
                                      wend
                                      subnet=""
                                            for i=0 to 3
                                             subnet = subnet & (addressbytes(i) AND subnetmaskbytes(i)) & "."
                                                    next
                                      subnet = left( subnet, len(subnet)-1 )
                              '------------------------------
                               If subnet = strNetSegment Then
                                  strMACaddres = objNic.MACAddress
                                  strDNSHostName = objNicConfig.DNSHostName
                                   If objNicConfig.DHCPenabled = True Then
                                        strDynamic = "dynamicIP:"
                                   Else strDynamic = "staticIP:"
                                   End If
                                  strIPAddress = Join(objNicConfig.IPAddress)
                                  strNetMask = Join(objNicConfig.IPSubnet)
                                  StrDNSDomain = objNicConfig.DNSDomain
                               End If
                              next
                               on error goto 0
                              GetMACaddress = strMACaddres &","& strDNSHostName &","& strDynamic &","& strIPAddress &","& strNetMask &","& date&" "&time &","& StrDNSDomain    
                              End Function
                              Now at the beginning of the shutdownscript add these 2 lines
                              Code:
                               strNetSegment = "192.168.0.0"       '	<------ Used to determine the right LAN-NIC to get the MACaddress from
                               strOutputFile = "collected MACaddresses.csv" '	<------ Path & filename
                              And set the right values, Notice the network segment!!! The script will calculate the subnet from every NiC found on each computer using its IPaddress and Subnetmask. The script compare that subnet if is matches the LAN's subnet you gave here. That way it writes only relevant MAC addresses to the output file.

                              Next add this code between the lines "DO Until objRecordSet.EOF" and "loop" before the shutdown part and after the ping part in the shutdown script. So add it right after the line On Error Resume next in the shutdown script:
                              Code:
                              'write file with MACaddresses! can be of use with a 'Wake On Lan' batch:
                                  strFysicaddrs = GetMACaddress(strNetSegment)
                                  Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
                                  Set oFile = FileSystem.CreateTextFile(strOutputFile, True)
                                  oFile.WriteLine (strFysicaddrs)
                              \Rem
                              Last edited by Rems; 10th February 2007, 12:27. Reason: giving the right spot now to place the File writing part

                              This posting is provided "AS IS" with no warranties, and confers no rights.

                              __________________

                              ** Remember to give credit where credit's due **
                              and leave Reputation Points for meaningful posts

                              Comment

                              Working...
                              X