?
Solved

Reboot a Remote Computer with VB.NET

Posted on 2006-04-19
4
Medium Priority
?
623 Views
Last Modified: 2013-12-05
We have a training room that needs machines rebooted frequently. I attempted to write a VB.net Script to do this but am stuck.  Basically I have a form with a button that can reboot all the machines, but I want to input a text box that I can lable machine name, type the node name of the computer in there, and then hit a button that will reboot that one node name. I have nothing so far.

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        If Trim(TextBox1.Text) = "" Then
            MsgBox("Please enter Client Node Name!!!", MsgBoxStyle.Information, "node name???")
            TextBox1.Focus()
            Exit Sub
        End If
        If (TextBox1.Text) = "" Then



        End If
    End Sub
0
Comment
Question by:solomonacquah
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16491957
You can use the Shutdown command from the command line.

Bob
0
 
LVL 4

Author Comment

by:solomonacquah
ID: 16492222
I know that but I already created this cool looking form and my life wouldn't be complete unless I learned something about VB.NET
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 375 total points
ID: 16492508
I certainly wouldn't want to deprive you of any opportunity to learn something new.

Here is an untested WMI class:

' Add a reference to System.Management.dll to the project.

Imports System.Management

Public Enum PlatformType
  Unknown
  WorkStation
  DomainController
  Server
End Enum 'PlatformType

Public Enum SystemType
  Unknown
  Other
  MACROS
  ATTUNIX
  DGUX
  DECNT
  DigitalUnix
  OpenVMS
  HPUX
  AIX
  MVS
  OS400
  OS_2
  JavaVM
  MSDOS
  WIN3x
  WIN95
  WIN98
  WINNT
  WINCE
  NCR3000
  NetWare
  OSF
  DC_OS
  ReliantUNIX
  SCO_UnixWare
  SCO_OpenServer
  Sequent
  IRIX
  Solaris
  SunOS
  U6000
  ASERIES
  TandemNSK
  TandemNT
  BS2000
  LINUX
  Lynx
  XENIX
  VM_ESA
  Interactive_UNIX
  BSDUNIX
  FreeBSD
  NetBSD
  GNU_Hurd
  OS9
  MACH_Kernel
  Inferno
  QNX
  EPOC
  IxWorks
  VxWorks
  MiNT
  BeOS
  HP_MPE
  NextStep
  PalmPilot
  Rhapsody
End Enum 'SystemType

Public Class Win32_OperatingSystem

  Public BootDevice As String
  Public BuildNumber As String
  Public BuildType As String
  Public CodeSet As String
  Public CountryCode As String
  Public CurrentTimeZone As Integer
  Public Locale As String
  Public Manufacturer As String
  Public MaxNumberOfProcesses As Int64
  Public MaxProcessMemorySize As Int64
  Public Name As String
  Public NumberOfLicensedUsers As Int32
  Public NumberOfProcesses As Int32
  Public NumberOfUsers As Int32
  Public Organization As String
  Public OSLanguage As Int32
  Public OSProductSuite As Int32
  Public OSType As SystemType
  Public PlusProductID As String
  Public PlusVersionNumber As String
  Public Primary As Boolean
  Public ProductType As PlatformType
  Public RegisteredUser As String
  Public SerialNumber As String
  Public ServicePackMajorVersion As Int16
  Public ServicePackMinorVersion As Int16
  Public Version As String
  Public WindowsDirectory As String
  Friend ManagementObject As ManagementObject

  Public Overloads Shared ReadOnly Property GetList() As Win32_OperatingSystem()
    Get
      Return GetList(String.Empty)
    End Get
  End Property  'GetList

  Public Overloads Shared ReadOnly Property GetList(ByVal machineName As String) As Win32_OperatingSystem()
    Get
      Dim osClass As ManagementClass
      Dim list As New ArrayList

      If machineName.Length = 0 Then
        osClass = New ManagementClass("Win32_OperatingSystem")
      Else
        Dim path As New ManagementPath
        path.Server = "\\" & machineName.TrimStart("\") & "\root\cimv2"
        path.Path = "Win32_OperatingSystem"

        osClass = New ManagementClass(path)
      End If

      For Each osObject As ManagementObject In osClass.GetInstances()

        Dim info As New Win32_OperatingSystem

        info.BootDevice = osObject("BootDevice")
        info.BuildNumber = osObject("BuildNumber")
        info.BuildType = osObject("BuildType")
        info.CodeSet = osObject("CodeSet")
        info.CountryCode = osObject("CountryCode")
        info.CurrentTimeZone = Convert.ToInt32(osObject("CurrentTimeZone"))
        info.Locale = osObject("Locale")
        info.Manufacturer = osObject("Manufacturer")
        info.MaxNumberOfProcesses = Convert.ToInt64(osObject("MaxNumberOfProcesses"))
        info.MaxProcessMemorySize = Convert.ToInt64(osObject("MaxProcessMemorySize"))
        info.Name = osObject("Name")
        info.NumberOfLicensedUsers = Convert.ToInt32(osObject("NumberOfLicensedUsers"))
        info.NumberOfProcesses = Convert.ToInt32(osObject("NumberOfProcesses"))
        info.NumberOfUsers = Convert.ToInt32(osObject("NumberOfUsers"))
        info.Organization = osObject("Organization")
        info.OSLanguage = Convert.ToInt32(osObject("OSLanguage"))
        info.OSProductSuite = Convert.ToInt32(osObject("OSProductSuite"))
        info.OSType = Convert.ToInt32(osObject("OSType"))
        info.PlusProductID = osObject("PlusProductID")
        info.PlusVersionNumber = osObject("PlusVersionNumber")
        info.Primary = osObject("Primary")
        info.ProductType = Convert.ToInt32(osObject("ProductType"))
        info.RegisteredUser = osObject("RegisteredUser")
        info.SerialNumber = osObject("SerialNumber")
        info.ServicePackMajorVersion = Convert.ToInt16(osObject("ServicePackMajorVersion"))
        info.ServicePackMinorVersion = Convert.ToInt16(osObject("ServicePackMinorVersion"))
        info.Version = osObject("Version")
        info.WindowsDirectory = osObject("WindowsDirectory")
        info.ManagementObject = osObject

        list.Add(info)

      Next osObject

      Return list.ToArray(GetType(Win32_OperatingSystem))

    End Get
  End Property  'GetList(machine)

  Public Sub RebootMachine(ByVal system As Win32_OperatingSystem)

    system.ManagementObject.InvokeMethod("Reboot", New Object() {})

  End Sub  'RebootMachine

End Class

Sample usage:

  Dim systems() As Win32_OperatingSystem = Win32_OperatingSystem.GetList("machine name goes here")
  systems(0).RebootMachine()

Bob
0
 
LVL 4

Author Comment

by:solomonacquah
ID: 16502164
Thank You.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question