none
WMI - Executing a batch file on a remote computer RRS feed

  • Question

  • Hello,

    I am trying to write a wmi script that will use a batch file to install a printer on a remote computer. Here is my code:

    =======================================================

    Const SW_NORMAL = 5
    strComputer = "xxx"
    strPrinter = "SEO Printer"
    strBaseFolder = "G:\cmatera\Remote Printer Setup\"
    strBatFile = strBaseFolder & strPrinter & ".bat"
    Set objWMIService2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set oShell = CreateObject("WScript.Shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")

    ' Configure the Notepad process to show a window
    Set objStartup = objWMIService2.Get("Win32_ProcessStartup")
    Set objConfig = objStartup.SpawnInstance_
    objConfig.ShowWindow = SW_NORMAL

    If Not oFSO.FileExists(strBatFile) Then
        MsgBox "Could not find batch file, quitting!", vbCritical + vbSystemModal, "Text search"
        WScript.Quit
    End If


    ' error = path not found
    ' possible error -> currentdirectory      *
    Error = objWMIService.Create(strBatFile, strBaseFolder, objConfig, intProcessID)

        If Error = 0 Then
            Wscript.Echo "Printer intallation was started with a process ID of " & intProcessID & "."
        Else
            Wscript.Echo "Printer intallation could not be started due to error " & Error & "."
        End If
    =================================================

    This code works perfectly if it is run on the local computer but, does not when I try run it on another computer located on the network.

    Problem 1:

    I keep getting a "path not found" error for the create method. I think this has something to do with the remote computer not being able to go into the server and retrieve the batch file.

    Problem 2:

    I tried to place the bat file on the hard drive of the remote computer which got rid of the previous error. But, i found another problem. I can start the command process with my code (i know this because i can see it open up in the task manager), but i cannot get the file to actually execute. It just runs in the background and does nothing.

     

    Any help would be greatly appreciated. Thanks.

     

    • Moved by SamAgain Wednesday, May 26, 2010 2:47 AM (From:.NET Base Class Library)
    Tuesday, May 25, 2010 8:37 PM

All replies