.NET Web Service call to run script on remote machine using WMI

I need to run a vbs script on a remote machine from WMI in a vb.net web service. The path of the vbs file on the remote machine is called
C:\ShippingSystem\Wave\wave.vbs

What I have so far.... (Remote Machine, Path on the remote machine, username, password)

Sub RunIt()
CreateProcess("REMOTEMACHINE", "C:\ShippingSystem\Wave\Wave.vbs", "DOMAIN\USERNAME", "password")
End Sub

Private Sub CreateProcess(ByVal strComputer As String, ByVal strProcess As String, ByVal UserName As String, ByVal Password As String)
 

Dim processBatch As ManagementClass = New ManagementClass("Win32_Process")
        Dim inParams As ManagementBaseObject = processBatch.GetMethodParameters("Create")
        Dim msc As ManagementScope

        inParams("CurrentDirectory") = Nothing
        inParams("CommandLine") = strProcess
        Dim co As ConnectionOptions = New ConnectionOptions()
        co.Username = UserName
        co.Password = Password

        Try
            If (strComputer = System.Environment.MachineName) Then
                msc = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2")
            Else
                msc = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
                'msc = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
            End If

            msc.Connect()

            Debug.WriteLine(msc.Path)
            processBatch.Scope = msc

Open in new window


'WHERE DO I GO FROM HERE

End Sub
Kelly MartensAsked:
Who is Participating?
 
Kelly MartensAuthor Commented:
Feel free to critique. I was under the gun and this works. I did have to run the service with elevated permissions. Whoever rips me to shreds most and provides suggestions to make it better gets the points. I guess those points are important things around here.

<WebMethod()> Public Function Run_Along_Home() As String
        ErrorMessage = ""
        If ServiceCall_Run("server", "username", "password") = "COMPLETE" Then
            Return "COMPLETE"
        Else
            Return ErrorMessage
        End If

    End Function

Function ServiceCall_Run(ByVal ServerName As String, ByVal UserName As String, ByVal Password As String) As String
        Dim parterror As String = ""
        Try
            Dim co As ConnectionOptions = New ConnectionOptions()
            Dim msc As ManagementScope
            co.Username = UserName
            co.Password = Password
            msc = New System.Management.ManagementScope("\\" & ServerName & "\root\cimv2:Win32_Process", co)
            parterror = "CONNECTION"
            msc.Connect()
            parterror = "processClass"
            Dim processClass = New ManagementClass("\\" & ServerName & "\root\cimv2:Win32_Process")
            parterror = "methodArgs"
            Dim methodArgs As Object() = {IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cscript.exe") & " putyourfilepathhere.vbs"}
            parterror = "Invoke"
            Dim result As Object = processClass.InvokeMethod("Create", methodArgs)
            Return "COMPLETE"
        Catch ex As Exception
            ErrorMessage = "FAIL: " & parterror & " " & ex.ToString
            Return "FAIL: " & parterror & " " & ex.ToString
        End Try
    End Function

Open in new window

0
 
Chinmay PatelEnterprise ArchitectCommented:
just in case I am not misunderstanding your requirement, a web service is called which in turn invokes your VBS on a remote machine?
0
 
Kelly MartensAuthor Commented:
the webservice exists on a machine. it connects to a remote machine and runs the vbs script on the remote machine. on the remote machine the path of the file is
C:\ShippingSystem\Wave\wave.vbs
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Shaun VermaakTechnical Specialist/DeveloperCommented:
'WHERE DO I GO FROM HERE
Does your code work? I would create listening Windows Service running on these remote computers to execute those commands.
I would not use System.Management unless I have no other option
0
 
Kelly MartensAuthor Commented:
Am I allowed to answer my own question?
0
 
Chinmay PatelEnterprise ArchitectCommented:
Of course Kelly and you can also assign point to your valid answers.
0
 
Chinmay PatelEnterprise ArchitectCommented:
EE is very different than Stack in that sense. I do not think any one will rip apart someone for finding out a solution on their own. And coming to look at your code, I don't see issues. Maybe some other expert will be able to leverage their experience and find something.

There are reservations against WMI as it is a dated technology and it has its own challenges w.r.t. functionality and consistency. I am sure you / your client are fully aware of those. All the best.
0
 
Kelly MartensAuthor Commented:
See the comment with the code block.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.