473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2250
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Maarten | last post by:
hallow all earlier i posted a question about shutingdown a computer. there was someone who helped me with the code? i don't know anymore who he was. i remember there was an error in the code,...
5
by: j vickroy | last post by:
Hello, I'm trying to understand the behavior of the Python 2.3 logging module (MS Windows 2k) with regard to RotatingFileHandler. The following script illustrates a puzzling problem. What is...
7
by: Lynn | last post by:
I'm running Oracle 8.1.7.4.1 on W2K and have the services for my oracle instances set to automatically startup and shutdown with the W2K server. Unfortunately, neither is working. Nothing gets...
8
by: Bill Sonia | last post by:
I've written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that for OnShutDown, once my...
3
by: Mohammad-Reza | last post by:
Hi everybody I want to shutdown my computer with my program. I searched in MSDN to find a .NET method that can do that but did'nt find anything but ExitWindowsEx Api. I write following codes to...
2
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess,...
3
by: tbone | last post by:
In trying to improve the throughput of a classic ASP app I wrote a few years ago, I added monitoring to the application and session start and end methods. I'm counting the total number of sessions...
5
by: Phil Tusa | last post by:
Greetings to all .... I have a need to issue a shutdown and/or Restart Windows XP inside my application. Any help or example code would be appreciated! -- Phil
1
by: lil rahmer | last post by:
@echo off C: set choice= set /p choice=Do you wish to continue? If %choice%==no GOTO SHUTDOWN If %choice%==yes GOTO next :next echo It didn't work exit :SHUTDOWN
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.