• 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

    • all these "selling for great prices" articles feel like filler anymore....
    • It has the same switch to allow whitelisting or not as the regular AdBlock extension.
    • These 20 crypto phishing applications are scamming Play Store users by Hamid Ganji Google Play Store is the main venue for Android users to download applications. While Google has strict rules and policies for verifying apps, some malicious apps somehow slip through anyway. Meanwhile, when it comes to crypto wallet apps, both Google app auditors and Play Store users need to be even more cautious. Cyble Research and Intelligence Labs has identified at least 20 crypto phishing applications on the Google Play Store that impersonate legitimate and popular crypto wallet apps and try to steal users' crypto credentials. By impersonation, these malicious apps trick users into downloading them and then start to capture the user's actual login data. "What makes this campaign particularly dangerous is the use of seemingly legitimate applications, hosted under previously benign or compromised developer accounts, combined with a large-scale phishing infrastructure linked to over 50 domains. This extends the campaign's reach and lowers the likelihood of immediate detection by traditional defenses." Cyble writes. Some of these malicious apps have the same name but come with a different package name. After removing duplicate names, here's the list of 9 newly discovered crypto phishing applications on the Play Store: Pancake Swap Suite Wallet Hyperliquid Raydium BullX Crypto OpenOcean Exchange Meteora Exchange SushiSwap Harvest Finance Blog According to Cyble, these apps prompt users to enter their 12-word mnemonic phrase to access the fake crypto wallet. Also, scammers use accounts that were previously used to distribute legitimate apps to minimize the risk of getting caught by Google. These accounts are more likely to be compromised and then taken over by scammers. If you've downloaded any of these fake crypto wallet apps from the Play Store, make sure to delete them as soon as possible. In 2024, revenue from crypto scams was estimated to be around $9.9 billion. This billion-dollar crypto scam business is expected to grow massively in 2025 thanks to AI.
  • Recent Achievements

    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
    • Week One Done
      maimutza earned a badge
      Week One Done
    • Week One Done
      abortretryfail earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      482
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      239
    4. 4
      ATLien_0
      229
    5. 5
      Edouard
      179
  • Tell a friend

    Love Neowin? Tell a friend!