• 0

[VB.NET] Remote Shutdown


Question

I want to make a VB program to be a front end to the shutdown command. Basically, I want to run the following command:

shutdown /s /m \\Lab-1 /t 600 /f

I was able to do this with the following code:

Dim WshShell = CreateObject("WScript.Shell")

WshShell.Exec("shutdownm \\Lab-1 /t 600 /f")

The only thing is that:

A) This opens a command window, which I don't want the end user to see.

B) I want to know how to receive the result of this command, so I know if it was successful or if it failed.

Link to comment
https://www.neowin.net/forum/topic/545941-vbnet-remote-shutdown/
Share on other sites

7 answers to this question

Recommended Posts

  • 0
  JK1150 said:
I want to make a VB program to be a front end to the shutdown command. Basically, I want to run the following command:

shutdown /s /m \\Lab-1 /t 600 /f

I was able to do this with the following code:

Dim WshShell = CreateObject("WScript.Shell")

WshShell.Exec("shutdownm \\Lab-1 /t 600 /f")

The only thing is that:

A) This opens a command window, which I don't want the end user to see.

B) I want to know how to receive the result of this command, so I know if it was successful or if it failed.

Try this to hide anything

  Quote
Dim WshShell = CreateObject("WScript.Shell")
WshShell.Exec("shutdownm \\Lab-1 /t 600 /f"),0,True

I do not know if this is what you want, but it pings the computer

  Quote
strComputer = "."
Set objWMIService = GetObject(_ 
	"winmgmts:\\" & strComputer & "\root\cimv2")
Set colPings = objWMIService.ExecQuery _
	("Select * From Win32_PingStatus where Address = 'PLACE_IP_ADDRESS_HERE'")

For Each objStatus in colPings
	If IsNull(objStatus.StatusCode) _
		or objStatus.StatusCode<>0 Then 
		WScript.Echo "Computer did not respond." 
	Else
		Wscript.Echo "Computer responded."
	End If
Next
  • 0

Thanks for both of those, they'll definitely help. I was wondering if the result of the command can be returned to with a .toString. For instance, if a "Network path not found." reply is returned, I want the user to know about that.

Also if I do

Dim WshShell = CreateObject("WScript.Shell")

WshShell.Exec("shutdownm \\Lab-1 /t 600 /f"),0,True

I get an end of statement expected on ,0,True, when I put it inside the brackets, the command does not seem to execute.

  • 0

There is an API available to shutdown the local and remote systems, give you a bit more control than just invoking WSH.

Anyway, API call is InitiateSystemShutdown, this single API allows timed shutdowns, instant shutdowns, reboots, shutdowns with messages, remote shutdown, etc., everything you will need, and AbortSystemShutdown (of course) can abort the timed shutdown.

Just pass null to the computer name to target the local system.

I have some old VB6 code that could handle it (would automatically attempt to get the privileges for it, if not it would fail nicely), but looking at it now, I see that it would be a problem converting it to .NET

  • 0

	Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" ( _
		ByVal lpMachineName As String, _
		ByVal lpMessage As String, _
		ByVal dwTimeout As Long, _
		ByVal bForceAppsClosed As Long, _
		ByVal bRebootAfterShutdown As Long _
	) As Long

	Private Declare Function AbortSystemShutdown Lib "advapi32.dll" Alias "AbortSystemShutdownA" _
	  (ByVal lpMachineName As String) As Long

	Public Sub goShutDownLab()
		Try
			InitiateSystemShutdown("Lab-Computer-1", "Lab will shut down in 10 minutes", 600, 1, 0)

That's the part with the shutdown, everything else works fine.

  • 0

Could try this.

<DllImport("advapi32.dll", SetLastError:=True)> _
Public Shared Function InitiateSystemShutdown(ByVal lpMachineName As String,ByVal lpMessage As String, ByVal dwTimeout As Integer, ByVal bForceAppsClosed As Boolean, ByVal bRebootAfterShutdown As Boolean) As Boolean
End Function

Or this to save time.

http://www.mentalis.org/soft/class.qpx?id=7

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • First of all, Mars' atmosphere is way to thin for hang gliding, second the only reason rare minerals are worth a lot of money is because they are rare, they won't be worth trillions if there's a bunch of it in space and third, nobody alive today or their children's children is going to see this happen.
    • If somebody actually provided those upgrades to windows 98... I saw welcome, somehow somebody still wants to maintain it.
    • We were fine, if not even better back in the XP-7 days where we could pick and choose the updates. Wait for the brave to grab everything and report back on what update broke what (not that they were breaking their own OS anywhaere near as hard back then every few seconds) The security scare is just that. This isn'tsome locked down company machine. I'm supposed to be the master of it. Not them. The basic folk eating up their security above all to the point that options and control are drying up for those of us who are capable. It's not cool.
    • We can't even fix our own environment here on Earth after screwing it up, but we're going to terraform another planet. Sure...
    • So you still have to rely on the network supporting it? Some networks, in fact most networks in the U.K, are using Googles version of RCS, which means the Iphone RCS will not work., SMS don't have any encryption, but then I don't send anything that is important or need encrypting, if I do need to tell someone something that is so important that I am bothered about my messages being intercepted then I phone them. But 99.9% of the messages I send are not important and are things like, do you want to pop out for a pint tomorrow, or do you want to meet for coffee and then setting the time and place. Nothing that important and SMS does what I need, I have no need for RCS or any other messaging system. The only reason i keep Faceache messenger on my phone is because of a couple of people
  • Recent Achievements

    • First Post
      CyberCeps666 earned a badge
      First Post
    • Explorer
      CHUNWEI went up a rank
      Explorer
    • Contributor
      SnyPer456 went up a rank
      Contributor
    • First Post
      LELO2025 earned a badge
      First Post
    • First Post
      Caltor earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      386
    2. 2
      +FloatingFatMan
      151
    3. 3
      ATLien_0
      149
    4. 4
      macoman
      64
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!