Wij moeten binnenkort onze storage gaan upgraden en daarvoor moeten we 's-nachts eerst de Windows hosts uitzetten (Windows Server 2003)
Nu heb ik een Vbscript gemaakt die dat kan, maar ik zou graag willen weten of dit script helemaal goed is of dat het nog verbeterd kan worden, en wat jullie ideeen daarover zijn.
Wat het script dus moet doen
1.Servers uit een text bestand lezen
2.Servers een shutdown commando versturen
3.Shutdown naar een logbestand schrijven
[code=vb]
'Setting Constants
Const FORCESHUTDOWN = 5
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Overwrite = True
'Dim variables to memory
Dim Wshshell, ObjEnv, FSO, StrTempDir, StrWinDir, StrSysDrv, ServerFileShutdownList, StrComputer
Dim LogFile
'Setting Variables
Set Wshshell = CreateObject("Wscript.Shell")
Set ObjEnv = WshShell.Environment("Process")
Set FSO = CreateObject("Scripting.FileSystemObject")
StrTempDir = objEnv("TEMP")
StrWinDir = objEnv("WINDIR")
StrSysDrv = objEnv("SystemDrive")
'Input box to enter Server Name
ServerFileShutdownList = InputBox("Enter File Path here and File Name","Datacenter Shutdown File Prompt")
If ServerFileShutdownlist <> "" Then
If FSO.FileExists(ServerFileShutdownList) Then
'Read Input File and turn lines into Variables
Set File = FSO.OpenTextFile(ServerFileShutdownList)
'Loop through Text File
Do While File.AtEndOfStream <> True
StrComputer = File.ReadLine()
' Call Shutdown Function
ServerShutdown()
Loop
Else
MsgBox "ERROR!! The File that was selected cannot be located. Please check the File location", vbCritical, "ERROR!"
'Exiting Script with Error
WScript.Quit
End If
Else
MsgBox "ERROR!! No Input File. Please Input a Server List to Shutdown.", vbCritical, "File Input Error"
'Exiting Script with Error
WScript.Quit
End If
WScript.Quit
Function ServerShutdown()
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(FORCESHUTDOWN)
If FSO.FileExists(StrSysDrv & "\LogFile.txt") Then
Set LogFile = FSO.OpenTextFile(StrSysDrv & "\LogFile.txt",ForAppending)
LogFunction()
Else
FSO.CreateTextFile(StrSysDrv & "\LogFile.txt")
Set LogFile = FSO.OpenTextFile(StrSysDrv & "\LogFile.txt",ForAppending)
LogFunction()
End If
Next
End Function
Function LogFunction()
LogFile.Write String(10,"-")
LogFile.Write VbCrLf
LogFile.Write StrComputer & " has been issued a shutdown command at " & time() & " on " & date()
LogFile.Write VbCrLf
Logfile.Close
End Function
[/code=vb]
Graag hoor ik eventuele verbeteringen of aanvullingen die ten goede komen van dit script.
Ik heb ook op het Technet Scriptcenter gekeken, maar dat staat het helaas alleen beschreven voor een server, en er wordt niks naar een logbestand geschreven.
Nu heb ik een Vbscript gemaakt die dat kan, maar ik zou graag willen weten of dit script helemaal goed is of dat het nog verbeterd kan worden, en wat jullie ideeen daarover zijn.
Wat het script dus moet doen
1.Servers uit een text bestand lezen
2.Servers een shutdown commando versturen
3.Shutdown naar een logbestand schrijven
[code=vb]
'Setting Constants
Const FORCESHUTDOWN = 5
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Overwrite = True
'Dim variables to memory
Dim Wshshell, ObjEnv, FSO, StrTempDir, StrWinDir, StrSysDrv, ServerFileShutdownList, StrComputer
Dim LogFile
'Setting Variables
Set Wshshell = CreateObject("Wscript.Shell")
Set ObjEnv = WshShell.Environment("Process")
Set FSO = CreateObject("Scripting.FileSystemObject")
StrTempDir = objEnv("TEMP")
StrWinDir = objEnv("WINDIR")
StrSysDrv = objEnv("SystemDrive")
'Input box to enter Server Name
ServerFileShutdownList = InputBox("Enter File Path here and File Name","Datacenter Shutdown File Prompt")
If ServerFileShutdownlist <> "" Then
If FSO.FileExists(ServerFileShutdownList) Then
'Read Input File and turn lines into Variables
Set File = FSO.OpenTextFile(ServerFileShutdownList)
'Loop through Text File
Do While File.AtEndOfStream <> True
StrComputer = File.ReadLine()
' Call Shutdown Function
ServerShutdown()
Loop
Else
MsgBox "ERROR!! The File that was selected cannot be located. Please check the File location", vbCritical, "ERROR!"
'Exiting Script with Error
WScript.Quit
End If
Else
MsgBox "ERROR!! No Input File. Please Input a Server List to Shutdown.", vbCritical, "File Input Error"
'Exiting Script with Error
WScript.Quit
End If
WScript.Quit
Function ServerShutdown()
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(FORCESHUTDOWN)
If FSO.FileExists(StrSysDrv & "\LogFile.txt") Then
Set LogFile = FSO.OpenTextFile(StrSysDrv & "\LogFile.txt",ForAppending)
LogFunction()
Else
FSO.CreateTextFile(StrSysDrv & "\LogFile.txt")
Set LogFile = FSO.OpenTextFile(StrSysDrv & "\LogFile.txt",ForAppending)
LogFunction()
End If
Next
End Function
Function LogFunction()
LogFile.Write String(10,"-")
LogFile.Write VbCrLf
LogFile.Write StrComputer & " has been issued a shutdown command at " & time() & " on " & date()
LogFile.Write VbCrLf
Logfile.Close
End Function
[/code=vb]
Graag hoor ik eventuele verbeteringen of aanvullingen die ten goede komen van dit script.
Ik heb ook op het Technet Scriptcenter gekeken, maar dat staat het helaas alleen beschreven voor een server, en er wordt niks naar een logbestand geschreven.