468,268 Members | 1,753 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

VB.NET Shutdown from text file

Let me start by saying I am a noob to VB.net and am not a programmer
anyways
I am trying to learn and would like to get this to work... so please be
gentle, If Im doing it wrong please tell me...

I have users that leave their computers on all night and I want them to
shut them off. Local admin is removed from the computers, so I had
difficulties just kix scripting a task on the local PC to shutdown at a
given time... Plan 2 - Run a program on an admin PC everynight pulling
from a text file to shutdown those PCs...

I kinda got it to work - but it wont work it the PC is locked. Plus I
really dont need a form,
so not sure how to get rid of that. I tried to just use VB code in
VS2005, but when I did that the My.Computer.Network.Ping(computer)
showed My as undeclared variable...

Anyways here is the code... I did pull alot of this of the net and
msdn, so just want to make sure to give credit

Option Explicit On
Option Strict On
Imports System
Imports System.IO
Imports System.Management
Imports Microsoft.VisualBasic.Devices

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
' Create an instance of StreamReader to read from a file.
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 o As ManagementObject
Dim BWManagementPath As New ManagementPath
Dim inParams As System.Management.ManagementBaseObject
'Dim OptParams As InvokeMethodOptions
Dim OutParams As ManagementBaseObject
Dim enableprivileges As Boolean

logMessage = " has been shutdown"
Do
computer = computerList.ReadLine()
Console.WriteLine(computer)
logFile.WriteLine("{0}", DateTime.Now())
logFile.WriteLine("{0} {1}", computer, logMessage)
If My.Computer.Network.Ping(computer) Then
MsgBox("Ping to computer " + computer + " was
successful. Shutting it down")
BWManagementPath.Server = computer
BWManagementPath.NamespacePath = "root\CIMV2"
BWManagementPath.RelativePath =
"Win32_OperatingSystem.Name=""Microsoft Windows XP
Professional|C:\\WINDOWS|\\Device\\Harddisk0\\Part ition1"""
o = New ManagementObject(BWManagementPath)
enableprivileges = o.Scope.Options.EnablePrivileges
o.Scope.Options.EnablePrivileges = True
OutParams = o.InvokeMethod("Shutdown", inParams,
Nothing)
o.Scope.Options.EnablePrivileges = enableprivileges
Else
MsgBox("Ping to computer " + computer + " timed
out")
End If
Loop Until computerList.EndOfStream
computerList.Close()
logFile.Close()
Catch
Console.WriteLine("An error occurred.")
End Try
End Sub
End Class

Any help would be appreciated.

Thanks for any help and guidance
JD

Jan 28 '06 #1
3 2057
Let me start by saying I am a noob to VB.net and am not a programmer
anyways
I am trying to learn and would like to get this to work... so please be

gentle, If Im doing it wrong please tell me...
I have users that leave their computers on all night and I want them to

shut them off. Local admin is removed from the computers, so I had
difficulties just kix scripting a task on the local PC to shutdown at a

given time... Plan 2 - Run a program on an admin PC everynight pulling

from a text file to shutdown those PCs...
I kinda got it to work - but it wont work it the PC is locked. Plus I
really dont need a form,
so not sure how to get rid of that. I tried to just use VB code in
VS2005, but when I did that the My.Computer.Network.Ping(computer)
showed My as undeclared variable...
Anyways here is the code... I did pull alot of this of the net and
msdn, so just want to make sure to give credit
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Imports System.Management
Imports Microsoft.VisualBasic.Devices
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
' Create an instance of StreamReader to read from a file.
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 o As ManagementObject
Dim BWManagementPath As New ManagementPath
Dim inParams As System.Management.ManagementBaseObject
'Dim OptParams As InvokeMethodOptions
Dim OutParams As ManagementBaseObject
Dim enableprivileges As Boolean
logMessage = " has been shutdown"
Do
computer = computerList.ReadLine()
Console.WriteLine(computer)
logFile.WriteLine("{0}", DateTime.Now())
logFile.WriteLine("{0} {1}", computer, logMessage)
If My.Computer.Network.Ping(computer) Then
MsgBox("Ping to computer " + computer + " was
successful. Shutting it down")
BWManagementPath.Server = computer
BWManagementPath.NamespacePath = "root\CIMV2"
BWManagementPath.RelativePath =
"Win32_OperatingSystem.Name=""Microsoft Windows XP
Professional|C:\\WINDOWS|\\Device\\Harddisk0\\Part ition1"""
o = New ManagementObject(BWManagementPath)
enableprivileges = o.Scope.Options.EnablePrivileges

o.Scope.Options.EnablePrivileges = True
OutParams = o.InvokeMethod("Shutdown", inParams,
Nothing)
o.Scope.Options.EnablePrivileges = enableprivileges

Else
MsgBox("Ping to computer " + computer + " timed
out")
End If
Loop Until computerList.EndOfStream
computerList.Close()
logFile.Close()
Catch
Console.WriteLine("An error occurred.")
End Try
End Sub
End Class
Any help would be appreciated.
Thanks for any help and guidance
JD

Jan 30 '06 #2
Any help on this would be appreciated...

Jan 30 '06 #3
No one helped me here or there, but I did manage to figure a solution
out - posting for the next noob that comes along and want to do the
sale thing...

Have a list of computers as follows C:\shutdownvbs\computers.txt
Have a text file for logging in the follows C:\shutdownvbs\logFile.txt
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
Again may be a better way to do it, but what can I say - im a noob

John

Jan 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Maarten | last post: by
7 posts views Thread by Lynn | last post: by
8 posts views Thread by Bill Sonia | last post: by
3 posts views Thread by Mohammad-Reza | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.