Solved

Visual Studio 2012 - Remote shutdown command not working

Posted on 2014-04-14
2
727 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now