Link to home
Start Free TrialLog in
Avatar of Kelly Martens
Kelly Martens

asked on

.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
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

just in case I am not misunderstanding your requirement, a web service is called which in turn invokes your VBS on a remote machine?
Avatar of Kelly Martens
Kelly Martens

ASKER

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
'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
Am I allowed to answer my own question?
Of course Kelly and you can also assign point to your valid answers.
ASKER CERTIFIED SOLUTION
Avatar of Kelly Martens
Kelly Martens

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
See the comment with the code block.