Home All Groups Group Topic Archive Search About

Shutdown a list of computers using VB.net

Author
1 Feb 2006 1:46 PM
newsgroups.jd
Im sure there is a better way to do this - just posting what I did to
get it to work.

Please feel free to comment with suggestions - this was my first vb.net
program and I am not a programmer, so im sure it has room for
improvement


Assumptions:
list of computers in C:\shutdownvbs\computers.txt
log file at C:\shutdownvbs\logFile.txt

Aslo had to add reference to System.Management
Project > Add reference > System.Management


Imports System.IO
Imports System.Management

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Try
            Dim computerList As StreamReader = New
StreamReader("C:\shutdownvbs\computers.txt")
            Dim logFile As StreamWriter =
File.AppendText("C:\shutdownvbs\logFile.txt")
            Dim computer As String, logMessage As String
            Dim command As String
            logMessage = " has been shutdown"
            Do
                computer = computerList.ReadLine()
                Console.WriteLine(computer)
                If My.Computer.Network.Ping(computer) Then
                    Dim theDate As String =
FormatDateTime(DateTime.Now, DateFormat.ShortDate)
                    Dim theTime As String =
FormatDateTime(DateTime.Now, DateFormat.ShortTime)
                    logMessage = (theDate + " " + theTime + " " +
computer + " has been shutdown")
                    command = "shutdown -f -s -t 0 -m \\" + computer
                    Shell(command)
                    logFile.WriteLine("{0}", logMessage)
                Else
                End If
            Loop Until computerList.EndOfStream
            computerList.Close()
            logFile.Close()
        Catch
            Console.WriteLine("An error occurred.")
        End Try
    End Sub
End Class

Author
1 Feb 2006 2:25 PM
newsgroups.jd
One thing I have not figured out yet is that if there is a computer in
the list that does not resolve it breaks the task and does not
continue.

JD
Author
1 Feb 2006 5:21 PM
Andrew Morton
newsgroups***@gmail.com wrote:
> One thing I have not figured out yet is that if there is a computer in
> the list that does not resolve it breaks the task and does not
> continue.

Does psshutdown from www.sysinternals.com cope with that situation?

Andrew
Author
2 Feb 2006 9:36 PM
Del
I wrote a program in VB.NET that shuts down all computer on the domain using
wired or wireless

There are two ways to do it; InitializeSystemShutdown (API), but the
machines need the remote shutdown privilige set. In XP, you can use
shutdown.exe using the switches. InitializeSystemShutdown works across
platforms, shutdown.exe doesn't

Example:

Dim pi As New ProcessInfo

With pi
    .Filename = "shutdown.exe"
    .Arguments = "-s -m \\" & YourComputerNameHere & " -c " & chr(34) &
"Windows is shutting down" & chrs(34) & " -t 120"
End With

System.Diagnostics.Process.Start(pi)

Just typed the above in here. You will have to double-check the above to get
the code 100% perfect...

You have the main idea though

Crouchie1998
BA (HONS) MCP MCSE

I don't recomment the SysemInternals tool