• 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

    • Fresh leak suggests OnePlus Pad Lite in the works, key specs revealed by Sagar Naresh Bhavsar OnePlus recently unveiled the mid-range OnePlus 13s and the OnePlus Pad 3 Android tablet in India (the tablet is also coming to the U.S.). Now, it appears that the company is working on another tablet, this time, a budget model called the OnePlus Pad Lite. Supposedly, it will be the successor to the OnePlus Pad Go, which launched in the U.K., India, and European countries. According to the leaked images, OnePlus Pad Lite follows the same design elements as earlier OnePlus tablets. The tablet is shown to have a circular camera at the center of the rear panel, with the logo sitting in the middle. The USB-C port and speaker grills are located on the right side frame of the tablet. The alleged OnePlus Pad Lite measures 254.9 x 166.5 x 7.4mm and weighs 539 grams, which is slightly taller, wider, and thinner, but weighs less compared to the OnePlus Pad Go. The tablet, courtesy of 91Mobiles and OnLeaks, is displayed in blue, which could be the only color option. One image shows the tablet with a cover that doubles as a kickstand, but may be sold separately. Gallery: OnePlus Pad Lite Based on the leaked specs, the OnePlus Pad Lite could feature an 11-inch LCD 1920x1080 resolution 90Hz display. It could be powered by the MediaTek Helio G100 processor and paired with the Mali G57 GPU. The tablet may come with 6GB of RAM and 128GB of onboard storage, with more storage and RAM options available at launch. Reportedly, the OnePlus Pad Lite could feature a 5MP primary camera and a 5MP selfie camera. Under the hood, it could be juiced by a 9,340 mAh battery. On the software side, OnePlus Pad Lite may come with OxygenOS 15.0.1 based on Android 15 out of the box. There is no clarity on which market OnePlus plans to launch the OnePlus Pad Lite in. However, the report suggests that it could be priced under ₹20,000 (roughly $231). Images by 91mobiles x OnLeaks
    • Zen Browser 1.13b by Razvan Serea Zen Browser is a privacy-focused, open-source web browser built on Mozilla Firefox, offering users a secure and customizable browsing experience. It emphasizes privacy by blocking trackers, ads, and ensuring your data isn't collected. With Zen Mods, users can enhance their browser experience with various customization options, including features like split views and vertical tabs. The browser is designed for efficiency, providing fast browsing speeds and a lightweight interface. Zen Browser prioritizes user control over the browsing experience, offering a minimal yet powerful alternative to traditional web browsers while keeping your online activity private. Zen Browser’s DRM limitation Zen Browser currently lacks support for DRM-protected content, meaning streaming services like Netflix and HBO Max are inaccessible. This is due to the absence of a Widevine license, which requires significant costs and is financially unfeasible for the developer. Additionally, applying for this license would require Zen to be part of a larger company, similar to Mozilla or Brave. Therefore, DRM-protected media won't be supported in Zen Browser for the foreseeable future. Zen Browser offers features that improve user experience, privacy, and customization: Privacy-Focused: Blocks trackers and minimizes data collection. Automatic Updates: Keeps the browser updated with security patches. Zen Mods: Customizable themes and layouts. Workspaces: Organize tabs into different workspaces. Compact Mode: Maximizes screen space by minimizing UI elements. Zen Glance: Quick website previews. Split Views: View multiple tabs in the same window. Sidebar: Access bookmarks and tools quickly. Vertical Tabs: Manage tabs vertically. Container Tabs: Separate browsing sessions. Fast Profile Switcher: Switch between profiles easily. Tab Folders: Organize tabs into folders. Customizable UI: Personalize browser interface. Security Features: Inherits Firefox’s robust security. Fast Performance: Lightweight and optimized for speed. Zen Mods Customization: Deep customization with mods. Quick Access: Easy access to favorite websites. Open Source: Built on Mozilla Firefox with community collaboration. Community-Driven: Active development and feedback from users. GitHub Repository: Contribute and review the source code. Zen Browser 1.13b changes: New Features There's a new way to manage spaces, which brings a more intuitive and user-friendly experience Updated to firefox 139.0.4 Added support for Google safebrowsing for better security Collapsed toolbarr gets a slight UI redesign Fixes Fixed issues related to glance and split view Fixed performance issues and high GPU usage for some users Other small fixes and improvements Breaking Changes Customizable UI buttons at the bottom has been reset to a new default state Download: Zen Browser | 73.6 MB (Open Source) Download: Zen Browser ARM64 | Other Operating Systems View: Zen Browser Home Page | Screenshots 1 | 2 | Reddit Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • TBF, it has had PST support for quite a while now. But I still want them to add the ability to drag & drop between accounts / PSTs.
  • Recent Achievements

    • Week One Done
      julien02 earned a badge
      Week One Done
    • One Year In
      Drewidian1 earned a badge
      One Year In
    • Explorer
      Case_f went up a rank
      Explorer
    • Conversation Starter
      Jamie Smith earned a badge
      Conversation Starter
    • First Post
      NeoToad777 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      544
    2. 2
      ATLien_0
      227
    3. 3
      +FloatingFatMan
      160
    4. 4
      Michael Scrip
      113
    5. 5
      +Edouard
      102
  • Tell a friend

    Love Neowin? Tell a friend!