Avatar of wally_davis
wally_davisFlag for United States of America

asked on 

using VB.NET and SysInternals PSService.exe (or C:\Winnt\System32\SC.exe) to pipe status, no luck

Good day experts.
I'm attempting to use the SysInternals PSService.exe (or C:\Winnt\System32\SC.exe) to pipe status information to a text file, with no luck. It looks as if it wants to write to the file but never flushes.
Here are the two syntaxes I use in Command Shell and it works for both.
1. C:\Tools\SysInternals\PS Service\psservice.exe \\ComputerName -u usernam -p password QUERY "ServiceName" > Somefile.txt. This works in Command Shell, but not Shell in VB.NET
2. C:\Winnt\System32\sc.exe "\\ComputerName" "ServiceName" > somefile.txt...and this works in Command Shell but not VB.NET Shell. For this one it says "File Not Found".
Below is the attached code. I'm trying to use either one of these or WMI. I'll give you both code pieces.
Many thanks and utmost gratitude,
Wallace
--- VB.NET CODE ---
Dim strHost As String = "B001279AE624B"
        Dim strPath As String = "C:\Tools\SysInternals\PS Service\"
        Dim swPingSuccess As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Ping_Success, False)
 
        Dim swLogFile As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(logFile, False)
        'Dim strSrvcStatus As String = "" & strPath & "psservice.exe" & " \\" & strHost & " -u " & "Username" & " -p " & "Password" & " query " & """Platform Validation Tool""" & " > " & logFile & "" OR THIS
        Dim strSrvcStatus As String = """\\" & strHost & """" & " QUERY " & """Platform Validation Tool""" & " > " & logFile & ""
        Dim strStop As String = """\\" & strHost & """" & " STOP " & """Platform Validation Tool"""
        Dim strStart As String = """\\" & strHost & """" & " START " & """Platform Validation Tool"""
        'Dim strStop As String = "" & strPath & "psservice.exe" & " \\" & strHost & " -u " & "Username" & " -p " & "Password" & " stop"
        'Dim strStart As String = "" & strPath & "psservice.exe" & " \\" & strHost & " -u " & "Username" & " -p " & "Password" & " start"
 
 
        'Dim psi As New ProcessStartInfo()
        'psi.WorkingDirectory = "C:\Winnt\System32"
        'psi.FileName = "C:\Winnt\System32\sc.exe "
        'psi.UseShellExecute = True
        'psi.Arguments = strSrvcStatus
        'Err.Clear()
        'Process.Start(psi)
        Shell(strSrvcStatus, AppWinStyle.Hide, True)
        swLogFile.Flush()
        swLogFile.Close()
        Dim srLog As StreamReader = New StreamReader(logFile)
        If srLog.ReadToEnd.Contains("STOPPED") Then
            Shell(strStart, AppWinStyle.Hide)
            Shell(strSrvcStatus, AppWinStyle.Hide)
            If srLog.ReadToEnd.Contains("RUNNING") Then
                swPingSuccess.WriteLine("PVT Service Status: RUNNING")
            ElseIf srLog.ReadToEnd.Contains("STOPPED") Then
                swPingSuccess.WriteLine("SERVICE WON'T START")
                'SET STATUS = SERVICE BROKE
            End If
        ElseIf srLog.ReadToEnd.Contains("RUNNING") Then
            Shell(strStop, AppWinStyle.Hide)
            Shell(strSrvcStatus, AppWinStyle.Hide)
            If srLog.ReadToEnd.Contains("RUNNING") Then
                swPingSuccess.WriteLine("PVT Service Status: RUNNING")
            ElseIf srLog.ReadToEnd.Contains("STOPPED") Then
                swPingSuccess.WriteLine("SERVICE WON'T START")
                'SET STATUS = SERVICE BROKE
            End If
        ElseIf srLog.ReadToEnd.Contains("") Then
            swPingSuccess.WriteLine("PVT Service Status: SERVICE NON-EXISTANT OR WON'T START")
        End If
    End Sub
--- VB.NET CODE USING WMI --- I'm close on this one but can't get the loop or the values from the Array extracted properly.
 
Imports System
Imports System.Management
Imports System.Windows.Forms
 
Namespace WMIState
 
    Public Class MyWMIQuery
 
        Public Overloads Shared Function Main() As Integer
 
            Try
	Dim host() as string = {"B001279AE624B", "B001E0BA97371"}
                Dim searcher As New ManagementObjectSearcher( _
                    "\\" & host() & "\root\CIMV2", _
                    "SELECT State FROM Win32_Service Where Name = 'Platform Validation Tool'") 
 
	Dim i as Integer
	For i = 0 To host.Length - 1
	Console.WriteLine("Computer: {0}", i)
                For Each queryObj As ManagementObject in searcher.Get()
	    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Win32_Service instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("State: {0}", queryObj("State"))
                Next
	Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Function
    End Class
End Namespace

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment
philipjonathan
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

What about below. It reads the output from the stream.
ProcessStartInfo psi = new ProcessStartInfo("C:\\Tools\\SysInternals\\PS Service\\psservice.exe", "\\\\ComputerName -u usernam -p password QUERY "\"ServiceName"\");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
 
Process p = Process.Start(psi);
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of wally_davis
wally_davis
Flag of United States of America image

ASKER

philipjonathan, Even though it was in C#, it was easy enough to translate.
Works great, Thank you!
Oops, forgot that you posted in VB zone. But glad that you made it :)
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo