Shutdown script at 17:00 startup at 08:30

Home Forums Scripting General Scripting Shutdown script at 17:00 startup at 08:30

This topic contains 14 replies, has 6 voices, and was last updated by Avatar Daveinholland 12 years, 7 months ago.

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #120744

    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?

    Avatar
    sorinso
    Member
    #264631

    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.

    Avatar
    tonyyeb
    Member
    #223625

    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

    Avatar
    sorinso
    Member
    #264640

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

    tonyyeb;56071 wrote:
    wol.exe (wake on lan) (google it)

    But only if you need badly to :google:. If not, you can use the link in my reply, it’s for wol.exe :grin:

    Avatar
    tonyyeb
    Member
    #223626

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

    Hmm just tried your link. Its dead.

    Avatar
    sorinso
    Member
    #264641

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

    :cry: :cry:
    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? :)

    Avatar
    sorinso
    Member
    #264642

    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.

    JeremyW
    JeremyW
    Moderator
    #268698

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

    Just a couple of notes on the shutdown command.

    tonyyeb;56071 wrote:
    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

    #290968

    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?

    Avatar
    tonyyeb
    Member
    #223628

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

    Daveinholland;56185 wrote:
    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.

    Avatar
    tonyyeb
    Member
    #223629

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

    JeremyW;56157 wrote:
    You mean shutdown -s -m \computername, right?

    Thats correct Jeremy. Half asleep when i was looking at the syntax.

    Rems
    Rems
    Moderator
    #226196

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

    Daveinholland;56185 wrote:
    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:domainadmin /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, "rootcimv2", 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:\.rootcimv2")
    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

    #290972

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

    thx a lot i am going to try this

    Avatar
    Dumber
    Participant
    #196284

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

    Great script Rems!:beer:

    Rems
    Rems
    Moderator
    #226207

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

    Wel :roll: , 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 = “……mc-wol.exe” ‘< —- WOL program
    strFileInput = "……collected MACaddresses.csv” ‘ “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 & “rootcimv2”)

    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 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 & “rootcimv2”)
    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 periodlen( 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 periodlen( 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

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.