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
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