CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2012
    Location
    Kolkata, WestBengal, India
    Posts
    15

    Can not connect to SQL Server using WMI Services to Remote Computer

    I have a VBScript using which I was trying to connect to a server and try to run an EXE - this executable internally is connecting to a SQL Server.
    When I run this from my local computer it failed and say -
    "Invalid connection string attribute"
    When I remote-login to that server and run that EXE directly, it successfully connect to that SQL Server.

    Here is the SQL Server Connection string -
    ConnString=Provider=SQLOLEDB;Server=xxx.aa.conn\xxxx;Database=COMMONDB;Username=;trusted_connection=yes

    Here I am using the same domain, user_id, password to connect to my current computer and that remote computer. No doubt, it connects to that remote server and run whatever command/exe I would like to run.
    Here is my script -

    Code:
    ' Run it as -
    ' cscript //nologo test.vbs <server_name> "command to run task/job"
    ' E.g. -
    ' cscript //nologo test.vbs . "notepad.exe"
    
    Option Explicit
    
        Dim strComputer:strComputer = WScript.Arguments.Item(0)
        Dim strCmd:strCmd = WScript.Arguments.Item(1)
    
        Dim objWMIService, objWMIService1, errReturn, i
        Dim intProcessID, colProcesses, objProcess
    
        'Get the WMI service object for Win32 Process
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & _
        "\root\cimv2:Win32_Process")
    
        'Get the WMI service object
        Set objWMIService1 = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
        'Create a process for the to-be-run-job
        errReturn = objWMIService.Create(strCmd,null,null,intProcessID)
    
        'Successfull launch of the process returns 0
        if errReturn = 0 Then
    	Wscript.Echo "Process ( " & intProcessID & " ) : " & strCmd & " has Started"
        Else
            Wscript.Echo strCmd & " Could not be Started. Error: " & errReturn & "."
    	Wscript.Quit 99
        End If
    
        'Get notification of the processes currently executing
        Set colProcesses = objWMIService1.ExecNotificationQuery _
                ("Select * From __InstanceDeletionEvent " _ 
                & "Within 1 Where TargetInstance ISA 'Win32_Process'")
    
        'Check for the process that you ran for it's completion
        'once done - notify by echoing end message
        Do Until i = 999
            Set objProcess = colProcesses.NextEvent
            If objProcess.TargetInstance.ProcessID = intProcessID Then
    	    Wscript.Echo "Process ( " & intProcessID & " ) : " & strCmd & " has Ended"
                Exit Do
            End If
        Loop

    So, any kind of help/ suggestion is really appreciable.
    Please revert, if you would like to know any other information from my side.
    Thanks in advance.

    Raju2001006

    Please put [CODE][/CODE] tags around your code to preserve indentation and make it more readable...

  2. #2
    Join Date
    Jun 2009
    Posts
    113

    Re: Can not connect to SQL Server using WMI Services to Remote Computer

    I don't think it can be a problem with the script you posted as it's an old Scripting Guys script which has been tested and used countless times. The obvious test is to RDP into that server and run Task Manager, then from your PC run that script and get it to launch NotePad - then kill this process using Task Manager and check the script terminates locally quite happily.
    My assumption is that the use of Trusted_Connection requires it to authenticate against Windows, however because you're running remotely your credentials have already been used once, and therefore any process that requires them fails the double-hop impersonation. What happens if you copy that EXE over to your PC and run it locally? If it runs - which also proves that your credentials can access the SQL Server - then it is a double-hop issue.

  3. #3
    Join Date
    Nov 2012
    Location
    Kolkata, WestBengal, India
    Posts
    15

    Re: Can not connect to SQL Server using WMI Services to Remote Computer

    Thanks a lot for your comments. As I hardly have info on "double-hop issue" - let me dig a bit and I surely will keep this post updated.
    Or, if you post any basic info on this, it would be really helpful.
    Thanks again.

    Raju2001006

    Please put [CODE][/CODE] tags around your code to preserve indentation and make it more readable...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured