Question Command line ?

wwfc_barmy_army

New member
Joined
Apr 18, 2008
Messages
3
Programming Experience
Beginner
Hello.

First off, i'm still rather new to Vb.Net but am enjoying learning it, I have this code to run a shutdown:

VB.NET:
    '// Thread Process for Shutdown Process
    Private Sub BackgroundProcess()
        If System.IO.File.Exists(dbfile) = True Then
            Try
                Dim shutdownCount As Integer
                shutdownCount = 0
                Dim shutdownmess As String = """This computer is now shutting down. If you wish to continue working on the computer please press cancel now."""
                cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=rc.mdb;")
                'provider to be used when working with access database
                cn.Open()
                cmd = New OleDbCommand("select Comp from sheet1 WHERE Comp LIKE 'B10-%'", cn)
                dr = cmd.ExecuteReader
                While dr.Read()
                    Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), "Shutdown has Started!")
                    Dim p As Process = New Process 'This is the name of the process we want to execute 
                    p.StartInfo.FileName = ("cmd.exe")
                    p.StartInfo.UseShellExecute = False 'need to set this to false to redirect output
                    p.StartInfo.RedirectStandardOutput = True
                    p.StartInfo.CreateNoWindow = True
                    p.StartInfo.RedirectStandardInput = True
                    p.Start()
                    p.StandardInput.WriteLine("psshutdown.exe \\" & dr(0) & " -u administrator -p **** -t 120 -f -k -c -m " & shutdownmess)
                    Dim compout As String
                    Threading.Thread.Sleep(5000)
                    compout = "Computer " + dr(0) + " has been sent the shutdown command."
                    Dim SROutput As System.IO.StreamReader = p.StandardOutput
                    While (p.HasExited = False)
                        Dim sLine As String = p.StandardOutput.ReadLine
                        If (Not String.IsNullOrEmpty(sLine)) Then
                            If sLine.Contains("scheduled to power off") Then
                                'We are on a worker thread so marshal the call to the UI thread.
                                Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), sLine & vbCrLf)
                            End If
                        End If
                    End While
                    Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), compout)
                    shutdownCount = shutdownCount + 1
                    Threading.Thread.Sleep(2000)
                End While
                Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), "Shutdown is Complete!")
            Catch exc As Exception
                MessageBox.Show("Error: " & exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
            End Try
        Else
            MessageBox.Show("Error: The database file" & dbfile & " does not exist!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

It runs ok. Currently it looks through a number of computer names and shuts them down. But sometimes it can take up to a minute for anything to be returned by command line if for example a computer is switched off. So what i'm trying to do is get it so it doesn't continue the loop UNTIL there has been something returned by command line. Some maybe to do with While p.HasExited or something? Is this possible?

thanks for any advice.
 

Latest posts

Back
Top