Solved

Visual Studio 2012 - Remote shutdown command not working

Posted on 2014-04-14
2
758 Views
Last Modified: 2014-04-15
I am putting together some code to shutdown or restart a remote PC but it just wont work!

Imports System.Net.NetworkInformation
Imports System.Net
Imports System.Threading
Imports System.Text
Imports System.Diagnostics

Public Class Shutdown

    Private _isNull As Object


    Private Sub Shutdown_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtPCName.Text = Main.txtHostName.Text()
    End Sub

    Private Sub txtPCName_TextChanged(sender As Object, e As EventArgs) Handles txtPCName.TextChanged

    End Sub

    Private Sub btnShutdown_Click(sender As Object, e As EventArgs) Handles btnShutdown.Click

        Dim result = MessageBox.Show("The computer " & txtPCName.Text & "  will be shut down, are you sure you want to do this?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
        If DialogResult = Windows.Forms.DialogResult.Yes Then
            System.Diagnostics.Process.Start("C:\Windows\System32\shutdown.exe", " /m \\" & txtPCName.Text & " /s /t 10 /d p:0:0")
            Dim result2 = MessageBox.Show("The shutdown command has been sent to " & txtPCName.Text & " and it will shutdown in 10 seconds unless you click Cancel...", "Shutdown Command Sent", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
            If DialogResult = Windows.Forms.DialogResult.Cancel Then
                System.Diagnostics.Process.Start("C:\Windows\System32\shutdown.exe", " /m \\" & txtPCName.Text & " /a")
            Else

            End If
        ElseIf DialogResult = Windows.Forms.DialogResult.No Then
        End If

    End Sub

    Private Sub btnRestart_Click(sender As Object, e As EventArgs) Handles btnRestart.Click
        Dim result = MessageBox.Show("The computer " & txtPCName.Text & " will be restarted, are you sure you want to do this?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
        If DialogResult = Windows.Forms.DialogResult.Yes Then
            Process.Start("C:\Windows\System32\shutdown.exe", " /m \\" & txtPCName.Text & " /r /t 10 /d p:0:0")
        ElseIf DialogResult = Windows.Forms.DialogResult.No Then
        End If
    End Sub
End Class

Open in new window


The first message appears with a Yes/No question but then nothing else happens after that and the shutdown command doesnt run.

Can anyone offer some advice?
0
Comment
Question by:fruitloopy
2 Comments
 
LVL 18

Accepted Solution

by:
dj_alik earned 500 total points
ID: 40001279
first of all test from command line, may be you need credentials like username or passowrd
if you can do it from command line, read here:
Shutdown a remote computer connected in LAN in any preferable language
http://stackoverflow.com/questions/685528/shutdown-a-remote-computer-connected-in-lan-in-any-preferable-language
0
 
LVL 2

Author Closing Comment

by:fruitloopy
ID: 40001342
I solved it myself in the end. The problem was caused by the line:
"If Dialogresult = "
When it should have read:
"If Result = "

Here's the full code if anyone needs it...

    Private Sub btnShutdown_Click(sender As Object, e As EventArgs) Handles btnShutdown.Click
        Dim Timer As String
        Timer = ComTime.Text
        Dim result = MessageBox.Show("The computer " & txtPCName.Text & " will be shut down, are you sure you want to do this?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
        If result = Windows.Forms.DialogResult.Yes Then
            System.Diagnostics.Process.Start("C:\Windows\System32\shutdown.exe", " /m \\" & txtPCName.Text & " /s /t " & Timer & " /d p:0:0")
            Dim result2 = MessageBox.Show("The shutdown command has been sent to " & txtPCName.Text & " and it will shutdown in " & Timer & " seconds unless you click Cancel...", "Shutdown Command Sent", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
            If result2 = Windows.Forms.DialogResult.Cancel Then
                System.Diagnostics.Process.Start("C:\Windows\System32\shutdown.exe", " /m \\" & txtPCName.Text & " /a")
            Else

            End If
        ElseIf result = Windows.Forms.DialogResult.No Then
        End If
    End Sub
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question