; ------------------------------ ; Network Shutdown Script ; ------------------------------ ; This script will allow you to ; remotely Shutdown or Restart ; networked computers ; ------------------------------ ; Script by Mike Petersen ; mgpeter@pcc-services.com ; ------------------------------ ; Updated - 7 August 2007 ; ------------------------------ ; Dependencies... ; - Kixtart 2001 Version 4.22 Tested ; - KixForms Version 2.4.6 Tested ; - NT/2K/XP/Vista Workstations (not 9x) ; ------------------------------ Break On ;----- Variables -----; $completed = "%TEMP%\sdcompleted.log" $netviewlist = "%TEMP%\sdnetview.lst" $logfile = "%TEMP%\shutdown.log" Call K2E_RegisterKixforms() ;----- Delete old Log file if it exists -----; If Exist ("$logfile") Del "$logfile" EndIf ;----- Delete Completed List if it exists -----; If Exist ("$completed") Del "$completed" EndIf $ = open(1,$completed,5) $ = Close (1) ;---------------- Graphical Setup ---------------; $System = CreateObject("Kixtart.System") ;---- Initialize Main Window -----; $Form = $System.Form() $Form.Width = 400 $Form.Height = 410 $Form.Text = "Network Shutdown Utility" ;---------------- Initialize the Menu ----------------; $MainMenu1 = $System.MainMenu() $FileMenu = $MainMenu1.MenuItems.Add() $FileMenu.Text = "File" $FileExit = $FileMenu.MenuItems.Add() $FileExit.Text = "Exit" $FileExit.OnClick = "Exit_Program()" $ViewMenu = $MainMenu1.MenuItems.Add() $ViewMenu.Text = "View" $ViewAll = $ViewMenu.MenuItems.Add() $ViewAll.Text = "View All Domain Computers" $ViewAll.OnClick = "ViewAll_Computers()" $ViewOn = $ViewMenu.MenuItems.Add() $ViewOn.Text = "View Only Computers Turned On" $ViewOn.OnClick = "ViewOn_Computers()" $ViewOn.Checked = true $HelpMenu = $MainMenu1.MenuItems.Add() $HelpMenu.Text = "Help" $HelpAbout = $HelpMenu.MenuItems.Add() $HelpAbout.Text = "About" $HelpAbout.OnClick = "View_HelpAbout()" $Form.Menu = $mainMenu1; ;----- Initialize Domain List -----; $Domaincomputers = $Form.Controls.ListView $= $Domaincomputers.Columns.Add("Domain Computers",165,0) $Domaincomputers.Left = 5 $Domaincomputers.Width = 185 $Domaincomputers.Height = 195 $Domaincomputers.Top = 5 $Domaincomputers.MultiSelect = true ;----- Initialize Refresh Button -----; $RefreshButton = $Form.Controls.Add("Button") $RefreshButton.Left = 5 $RefreshButton.Top = 205 $RefreshButton.Width = 75 $RefreshButton.Height = 25 $RefreshButton.Text = "Refresh" $RefreshButton.OnClick = "RefreshButton_Click()" ;----- Initialize Shutdown Button -----; $ShutdownButton = $Form.Controls.Add("Button") $ShutdownButton.Left = 115 $ShutdownButton.Top = 205 $ShutdownButton.Width = 75 $ShutdownButton.Height = 25 $ShutdownButton.Text = "Shutdown" $ShutdownButton.OnClick = "ShutdownButton_Click()" ;----- Initialize Message Box ----; $Label = $Form.Controls.Add("Label") $Label.Text = "Message to be Broadcast..." $Label.Left = 205 $Label.Top = 8 $Label.Width = 180 $ShutdownMessage = $Form.Controls.TextBox $ShutdownMessage.Left = 205 $ShutdownMessage.Width = 180 $ShutdownMessage.Height = 110 $ShutdownMessage.Top = 30 $ShutdownMessage.Enabled = True $ShutdownMessage.WordWrap = True $ShutdownMessage.Multiline = True $ShutdownMessage.MaxLength = 232 $ShutdownMessage.Text = "Computer is going down for routine maintenance, please close all open files and finish what you are working on. Thank You" ;----- Initilize Delay Input -----; $DelayLabel = $Form.Controls.Add("Label") $DelayLabel.Text = "Delay before Shutdown (in minutes)" $DelayLabel.Left = 205 $DelayLabel.Top = 150 $DelayLabel.Width = 120 $DelayLabel.Height = 30 $DelayEntry = $Form.Controls.Add("MaskedTextBox") $DelayEntry.Left = 325 $DelayEntry.Top = 154 $DelayEntry.Width = 20 $DelayEntry.Height = 20 $DelayEntry.Mask = 0 $DelayEntry.Text = 1 ;----- Initialize the Shutdown/restart Choice -----; $GroupBox = $Form.Controls.Add("GroupBox") $GroupBox.Left = 205 $GroupBox.Top = 180 $GroupBox.Width = 180 $GroupBox.Height = 50 $SelShutdown = $GroupBox.Controls.Add("RadioButton") $SelShutdown.Left = 20 $SelShutdown.Top = 3 $SelShutdown.Width = 150 $SelShutdown.Text = "Shutdown Computers" $SelShutdown.Checked = True $SelRestart = $GroupBox.Controls.Add("RadioButton") $SelRestart.Left = 20 $SelRestart.Top = 24 $SelRestart.Width = 150 $SelRestart.Text = "Restart Computers" ;----- Initialize Window to display progress -----; $ProgressBox = $Form.Controls.Add("ListBox") $ProgressBox.Left = 5 $ProgressBox.Width = 380 $ProgressBox.Height = 120 $ProgressBox.Top = 235 $ProgressBox.Enabled = True ;----- Refresh the Lists before showing the form -----; Call RefreshDomain ;----- Show the Application Window -----; $Form.Show While $Form.Visible $=Execute($Form.DoEvents) Loop Call K2E_UnregisterKixforms() Exit 0 ;---------------- Functions Start Here ---------------; ;---------- Function to populate domain list ----------; Function RefreshDomain() If $ViewAllCPUs = "Yes" $Domaincomputers.Items.Clear $domain = getobject("WinNT://"+@domain) $domain.filter = "computer","" for each $computer in $domain $cpudone = 0 $ = open(2,$completed,2) Do $completedcpu = readline(2) If $completedcpu=$computer.name $cpudone=$cpudone + 1 EndIf Until @ERROR = -1 $ = Close (2) If $cpudone < 1 $Item = $Domaincomputers.Items.Add($computer.name) EndIf next Else If Exist ("$netviewlist") Del "$netviewlist" EndIf Shell "%COMSPEC% /C NET VIEW >$netviewlist" $Domaincomputers.Items.Clear $ = Open (3, "$netviewlist") $Line = Readline (3) Do If InStr ($Line,"\\") $OnComputer = Trim (SubStr($Line,3,12)) $cpudone = 0 $ = open(2,$completed,2) Do $completedcpu = readline(2) If $completedcpu=$OnComputer $cpudone=$cpudone + 1 EndIf Until @ERROR = -1 $ = Close (2) If $cpudone < 1 ;----- Now lets make sure the computer is in the Domain -----; $domain = getobject("WinNT://"+@domain) $domain.filter = "computer","" for each $computer in $domain If $OnComputer = $computer.name $Item = $Domaincomputers.Items.Add($OnComputer) EndIf next EndIf EndIf $Line = ReadLine (3) Until @ERROR = -1 $ = Close (3) Del "$netviewlist" EndIf EndFunction ;---------- Refresh Button Function ----------; Function RefreshButton_Click() Call RefreshDomain EndFunction ;---------- View All Domain CPUs Function ---------; Function ViewAll_Computers() $ViewAllCPUs = "Yes" $ViewOn.Checked = false $ViewAll.Checked = true Call RefreshDomain EndFunction ;---------- View Only Turned On CPUs Function ---------; Function ViewOn_Computers() $ViewAllCPUs = "No" $ViewAll.Checked = false $ViewOn.Checked = true Call RefreshDomain EndFunction ;---------- Show the Help About Function ---------; Function View_HelpAbout() $HelpAbout = $System.Form() $HelpAbout.Width = 300 $HelpAbout.Height = 225 $HelpAbout.Text = "Copyright Info" $HelpTitle = $HelpAbout.Controls.Add("Label") $HelpTitle.Text = "Network Shutdown Util - ver 1.0" $HelpTitle.FontBold = True $HelpTitle.FontSize = 12 $HelpTitle.TextAlign = MiddleCenter $HelpTitle.Top = 4 $HelpTitle.Width = 290 $HelpMyName = $HelpAbout.Controls.Add("Label") $HelpMyName.Text = "By Mike Petersen" $HelpMyName.TextAlign = MiddleCenter $HelpMyName.FontSize = 11 $HelpMyName.Top = 24 $HelpMyName.Width = 290 $HelpGPL = $HelpAbout.Controls.Add("Label") $HelpGPL.Text = "Released under the GNU GPL v2 (or later)" $HelpGPL.TextAlign = MiddleCenter $HelpGPL.Top = 40 $HelpGPL.Width = 290 $MyHyperLink = $HelpAbout.Controls.HyperLink $MyHyperLink.Text = "http://www.pcc-services.com/kixtart/" $MyHyperLink.Value = "http://www.pcc-services.com/kixtart/" $MyHyperLink.Left = 55 $MyHyperLink.Top = 60 $MyHyperLink.Width = 290 $HelpLine1 = $HelpAbout.Controls.Add("Label") $HelpLine1.Text = "- - - - - - - - - - - - - - - - - - -" $HelpLine1.TextAlign = MiddleCenter $HelpLine1.FontBold = True $HelpLine1.Top = 78 $HelpLine1.Width = 290 $HelpKix = $HelpAbout.Controls.Add("Label") $HelpKix.Text = "Kixtart (Copyright Ruud van Velsen)" $HelpKix.TextAlign = MiddleCenter $HelpKix.Top = 100 $HelpKix.Width = 290 $HelpKixCopy = $HelpAbout.Controls.Add("Label") $HelpKixCopy.Text = "KiXtart is Careware - see kixtart_license.txt" $HelpKixCopy.TextAlign = MiddleCenter $HelpKixCopy.Top = 118 $HelpKixCopy.Width = 290 $HelpLine2 = $HelpAbout.Controls.Add("Label") $HelpLine2.Text = "- - - - - - - - - - - - - - - - - - -" $HelpLine2.TextAlign = MiddleCenter $HelpLine2.FontBold = True $HelpLine2.Top = 135 $HelpLine2.Width = 290 $HelpKixForms = $HelpAbout.Controls.Add("Label") $HelpKixForms.Text = "KixForms Classic" $HelpKixForms.TextAlign = MiddleCenter $HelpKixForms.Top = 155 $HelpKixForms.Width = 290 $HelpKixFormsCopy = $HelpAbout.Controls.Add("Label") $HelpKixFormsCopy.Text = "(c) Copyright - Freeware" $HelpKixFormsCopy.TextAlign = MiddleCenter $HelpKixFormsCopy.Top = 172 $HelpKixFormsCopy.Width = 290 $HelpAbout.Show EndFunction ;---------- Exit the Program Function ----------; Function Exit_Program() $Form.Visible = 0 EndFunction ;---------- This Function does all the work, the Install Button Function ----------; Function ShutdownButton_Click() ;----- Open the Logfile -----; $ = open(1,$logfile,5) $ = Writeline (1," - - - - - Shutdown Script Starting -- @DATE - @TIME - - - - -" + @CRLF) $ = $ProgressBox.AddItem("Shutdown Script Starting...") For Each $Item In $Domaincomputers.SelectedItems $workstation = "\\"+$Item.Text ;----- First see if computer is on to avoid long timeouts -----; If Exist ("$netviewlist") Del "$netviewlist" EndIf Shell "%COMSPEC% /C NET VIEW >$netviewlist" $cpudefon = 0 $ = Open (3, "$netviewlist") $Line = Readline (3) Do If InStr ($Line,"\\") $OnComputer = Trim (SubStr($Line,3,12)) $cpuon = 0 If $Item.Text=$OnComputer $cpuon=$cpuon + 1 EndIf ;----- If computer is on, continue on -----; If $cpuon >= 1 ;----- Execute this if computer is on -----; $cpudefon = 1 $Message = '"' + $ShutdownMessage.Text + '"' $x = CInt ($DelayEntry.Text) $Delay = $x * 60 If $SelRestart.CheckState = "1" $Shutdownopt = 1 Else $Shutdownopt = 0 EndIf $RC = SHUTDOWN( "$workstation","$Message", $Delay, 1, $Shutdownopt ) -----; $ = $ProgressBox.AddItem( $workstation + " now Shutting Down") ;----- Write Computer to Completed List -----; $ = open(2,$completed,5) $ = Writeline (2, $Item.Text + @CRLF) $ = Close (2) ;----- End computer on Execution -----; :EndComputerOnExec EndIf ;----- Back From CPU is on check -----; EndIf $Line = ReadLine (3) Until @ERROR = -1 $ = Close (3) Del "$netviewlist" ;----- If computer is not on inform user & write to log -----; IF $cpudefon < 1 $ = $ProgressBox.AddItem($Item.Text + " does not seem to be on") $ = Writeline (1, $workstation + " does not seem to be on .. aborting" + @CRLF) EndIf Next $Domaincomputers.SelectedItems.Clear Call RefreshDomain ;----- Close the Logfile -----; $ = Close (1) $ = $ProgressBox.AddItem("The Shutdown Process is Finished for the computers you selected") EndFunction