.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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
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
Shaun VermaakTechnical SpecialistCommented:
'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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Kelly MartensAuthor Commented:
Am I allowed to answer my own question?
0
Chinmay PatelChief Technical NinjaCommented:
Of course Kelly and you can also assign point to your valid answers.
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chinmay PatelChief Technical NinjaCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Remote Access

From novice to tech pro — start learning today.