Home All Groups Group Topic Archive Search About

VB.NET Shutdown from text file

Author
30 Jan 2006 1:01 PM
newsgroups.jd
Let me start by saying I am a noob to VB.net and am not a programmer.
I am trying to learn and would like to get this to work...

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

Author
30 Jan 2006 1:09 PM
Ralph
<newsgroups***@gmail.com> wrote in message
Show quoteHide quote
news:1138624798.463748.27030@g49g2000cwa.googlegroups.com...
> Let me start by saying I am a noob to VB.net and am not a programmer.
> I am trying to learn and would like to get this to work...
>
> 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 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
> JD
>

You need to post this in a dotnet newsgroup....

--
<response type="generic" language="VB.Net">
This newsgroup is for users of Visual Basic version 6.0
and earlier and not the misleadingly named VB.Net
or VB 200x.  Solutions, and often even the questions,
for one platform will be meaningless in the other.
When VB.Net was released Microsoft created new newsgroups
devoted to the new platform so that neither group of
developers need wade through the clutter of unrelated
topics.  Look for newsgroups with the words "dotnet" or
"vsnet" in their name.  For the msnews.microsoft.com news
server try these:

news://msnews.microsoft.com/
   microsoft.public.dotnet.general
   microsoft.public.dotnet.languages.vb
   microsoft.public.vsnet.general
   microsoft.public.vstudio.general
</response>
Author
31 Jan 2006 1:30 PM
newsgroups.jd
No one helped me here or there, but I did manage to firgure 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
Author
31 Jan 2006 2:04 PM
Grant
No helped you here cuz you are in the wrong group. Ralph gave you the
correct group so go there for your future questions.  Thanks for leaving
your answer to your problem here thought however no one is going to come
here to read the answer to your problem.





<newsgroups***@gmail.com> wrote in message
Show quoteHide quote
news:1138714218.775201.21810@f14g2000cwb.googlegroups.com...
> No one helped me here or there, but I did manage to firgure 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
>