• 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

    • You do realize that if you are using Full Self Driving that you aren't actually driving, right?
    • I see several on eBay. Am I blind, or is this something else? Link (I know this link isn't in UK, but an example)
    • When my Tab S9 FE tablets were upgraded to Android 15 and Samsung UI 7.0 there were some default systems changes. One of them was. when I wanted to Power Down the tablet and tapped the Power button, Gemini popped up and I had to say Power Off which then brought up the Power Off menu resulting in an extra step in powering down the tablet. After some sleuthing I found that I could turn off Gemini and go back to the old menu, so I did. Maybe one day I will be comfortable talking to a machine but that day is not today.
    • Rufus 4.8.2253 by Razvan Serea Rufus is a small utility that helps format and create bootable USB flash drives, such as USB keys/pendrives, memory sticks, etc. Despite its small size, Rufus provides everything you need! Oh, and Rufus is fast. For instance it's about twice as fast as UNetbootin, Universal USB Installer or Windows 7 USB download tool, on the creation of a Windows 7 USB installation drive from an ISO (with honorable mention to WiNToBootic for managing to keep up). It is also marginally faster on the creation of Linux bootable USBs from ISOs. A non-exhaustive list of Rufus supported ISOs is available here. It can be especially useful for cases where: you need to create USB installation media from bootable ISOs (Windows, Linux, UEFI, etc.) you need to work on a system that doesn't have an OS installed you need to flash a BIOS or other firmware from DOS you want to run a low-level utility Rufus 4.8.2253 changelog: Switch to wimlib for all WIM image processing: Greatly speeds up image analysis when opening Windows ISOs Can speed up Windows To Go drive creation (But won't do miracles if you have a crap drive) Might help with Parallels limitations on Mac (But Rufus on Parallels is still unsupported) Enables the splitting of >4GB files with Alt-E (But still WAY SLOWER than using UEFI:NTFS) Others Switch to using Visual Studio binaries everywhere, due to MinGW DLL delay-loading limitations Add more exceptions for Linux ISOs that restrict themselves to DD mode (Nobara, openSUSE, ...) Improve reporting of UEFI bootloaders in the log, with info on the Secure Boot status Fix an issue with size limitations when writing an uncompressed VHD back to the same drive Fix a crash when opening the log with the 32-bit MinGW compiled version Fix commandline parameters not being forwarded to original Windows setup.exe Download: Rufus 4.8 | Portable | ~2.0 MB (Open Source) Download: Rufus 32-bit | ARM64 Links: Rufus Home Page | Project Page @GitHub | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Posting Machine
      David Uzondu earned a badge
      Posting Machine
    • One Month Later
      Stokenking earned a badge
      One Month Later
    • One Month Later
      Kevin Jones earned a badge
      One Month Later
    • Week One Done
      Kevin Jones earned a badge
      Week One Done
    • One Month Later
      henryj earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      531
    2. 2
      ATLien_0
      267
    3. 3
      +Edouard
      199
    4. 4
      +FloatingFatMan
      189
    5. 5
      snowy owl
      138
  • Tell a friend

    Love Neowin? Tell a friend!