VB.NET Shutdown from text file

N

newsgroups.jd

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\\Partition1"""
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
 
N

newsgroups.jd

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\\Partition1"""
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
 
N

newsgroups.jd

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top