Solved

Visual Studio 2012 - Remote shutdown command not working

Posted on 2014-04-14
2
747 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

816 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

11 Experts available now in Live!

Get 1:1 Help Now