I was just wondering if there was a way to shutdown different computers that were on your network through writing a custom vb application.
If so, what do I need to look into, such as certain objects and such.
Thanks,
Justin Fox
Printable View
I was just wondering if there was a way to shutdown different computers that were on your network through writing a custom vb application.
If so, what do I need to look into, such as certain objects and such.
Thanks,
Justin Fox
Yes and no. You can execute any file from VB code by calling Process.Start, so if there was a program that would shut down a remote computer you could just run it like that. As it happens, Windows has shutdown.exe that will do just that. Open a command prompt and type "shutdown /?" to get help on it, then write VB code to call it with the appropriate commandline to shutdown the remote machine of your choice.
You probably will also need Domain Administrator permissions.
Here's the code I use all the time. Works well enough. The account running it needs admin rights on the target machine.
Code:Private Sub RebootMachine(comp)
If MessageBox.Show("Are you sure you wish to reboot " & comp & "?", "Confirm reboot", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then
Me.GenFunctions.WriteLog("Rebooting machine")
Using RebootProc As New Process
With RebootProc.StartInfo
.CreateNoWindow = True
.FileName = "shutdown.exe"
.Arguments = " -r -m \\" & comp & " -c " & _
ChrW(34) & "Windows is shutting down" & ChrW(34) & " -t 1"
.UseShellExecute = False
.WindowStyle = ProcessWindowStyle.Hidden
.RedirectStandardOutput = True
.RedirectStandardInput = True
End With
RebootProc.Start()
End Using
End If
End Sub
I'd suggest using String.Format rather than string concatentation for the Arguments. What's nicer? This:or this:vb.net Code:
.Arguments = " -r -m \\" & comp & " -c " & _ ChrW(34) & "Windows is shutting down" & ChrW(34) & " -t 1"vb.net Code:
.Arguments = String.Format("-r -m \\{0} -c ""Windows is shutting down"" -t 1", comp)"
Thanks for the JMC. That does look better.
Of course, now I have to go through and change all the times I've used concatenation ;)
does a computer have to be in your "workgroup" for you to be able to shut it down?
because my laptop is in my network, I can go through it and everything, but when I try to execute the shutdown command to it, it says that the network path doesn't exist?
Is it because the laptop is on my wireless network? Or does that not matter?
Thanks,
Justin Fox
Oh, and btw, how do you become recognized as a MVP? Do you just have to help a lot of people out in the msdn forums or what...
Thanks again,
Justin Fox
It shouldnt matter that its on a wireless network, as long as you are running your app as a user account that has admin permissions on the remote machine.Quote:
Originally Posted by MonkOFox
In a domain environment this kind of thing would usually be run as the domain admin account but as you are in a workgroup then permissions can be a bit more of a pain (assuming you are running windows xp home edition).
Its also possible that your remote PC has a firewall on it that is stopping your connections getting through.
What exactly do you mean when you say you can "go through it and everything"?
If you can get in then that doesnt mean you have admin permissions, only that the drive/system is having "File and Print Sharing" turned on. You need to be a local administrator of the system you wish to shut down.
:blush:
1. We already established that you need to call shutdown.exe 24 hours ago.Quote:
Originally Posted by minitech
2. There's no point to specifying a path because the PATH environment variable will take care of that, so it will work with any OS that supports shutdown.exe by omitting the path.
3. That command line doesn't address the need to shutdown a remote computer.
Strike three! :rolleyes:
John, you forgot to mention - need to have local admin perms.
Strike four on you :D
Random shutdown.exe fact of the day: Run Shutdown.exe -i to make a GUI for the shutdown commands appear, where you can add a list of remote PCs to be shutdown, specify a message and time delay etc etc
What? You already knew that? :(
Come on guy, we can do better than that, calling an .exe? :)
Here's a vbs version, I'm sure we can all convert that to vb.net right?
Code:'check flag
' if equal 1 (TRUE) then perform Win32Shutdown action on remote PC
' and display a confirmation message
' if not equal 1 (FALSE) then skip the action and script ends
if flag then
Set OpSysSet=GetObject("winmgmts:{(Debug,RemoteShutdown)}//" _
& ComputerName & "/root/cimv2").ExecQuery( _
"Select * from Win32_OperatingSystem where Primary=true")
for each OpSys in OpSysSet
OpSys.Win32Shutdown(x)
y = msgbox(strAction,vbOKOnly + vbInformation,"Mission Accomplished")
next
end If
Well thats just using WMI so yeah that can be done in VB.NET but I dunno I have had mixed success rates with WMI scripts... personally I would rather use the shutdown.exe but as you say, its not good really having to rely on an external exe ... I suppose just use which ever solution seems to work best in your environment
There was a specific reason I didn't use WMI for shutdowns in my application. I can't, for the life of me, remember what is was now though.
umm, my desktop is xp and my laptop is vista, so could that be the reason why when I try to shutdown my laptop, it gives me a 'network path not found' error?
Thanks,
Justin
I wouldnt imagine so but I guess it is possible. As long as you are running the shutdown command as a user account that is an administrator on the laptop then it shouldnt matter.
Can you post the code that you are using to try execute the shutdown command? Or are you just using shutdown.exe through command prompt?
Its not always good to rely upon WMI because it may not be enabled as some network admins disable it.
You could also enable Remote Desktop on the PC you wish to shut down. Remote into it from another PC and just do a shutdown. No code to write, or software to buy.
Unless a more automated / programmed solution is desired.
Well Remote Desktop is not enabled by default on XP, although you can edit some registry keys remotely to enable it but sometimes this requires a reboot so it wouldnt be a very reliable solution really and like Robdogg said, an automated solution might be required. Perhaps to save the user the time it would take to log on to 10 PCs with Remote Desktop and manually shut them down.Quote:
Originally Posted by LloydAZ
hi all
i have a small network with 8 computers on it.
i wrote to programs 1 to do the shutting down and the other to send the command and both programs monitored a folder for commands.
i used a ini file to send information to each of the programs.
i used vb6 to write the programs. i lost all my project when my computer died..
remember to set a permissions to the folder so read write is ok
i wish i still had the project i would post it:cry::cry:
Why have you posted in a 2 year old thread? You dont even appear to be asking a question or posting an answer to the thread...
hi
i never looked at the post date..
never mind