Results 1 to 7 of 7

Thread: Execute bat/cmd with a different user

Threaded View

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Execute bat/cmd with a different user

    Hi,

    I'm trying to execute a cmd file with a different user but what I have isn't working the log in returns true but the command doesn't run

    vb Code:
    1. Dim iu As New ImpersonateUser
    2. ' Note:  This user must be the user that installed ReaConverter Pro otherwise each image
    3. ' will be stampted with a ReaConverter logo. This is a limitation of the evaluation/trial version.
    4. ' impersonateValidUser(username, domain, password)
    5. If iu.impersonateValidUser("myusername", "mydomain", "mypassword") = True Then
    6.     Call cFunctions.WriteToFile("Impersonating user succeeded!", False, Application("LogFilePath"), Application("LogFile"))
    7.     Try
    8.         ' Execute the command
    9.         ' http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results
    10.         Dim oProcess As System.Diagnostics.Process = New System.Diagnostics.Process
    11.         Dim strCmd As String = sOutputFolderPath.Replace("\pdf", "") & "ReaConvertFile.cmd" ' path and file name of command to run
    12.         With oProcess
    13.             .StartInfo.FileName = strCmd
    14.             .StartInfo.UseShellExecute = False
    15.             .StartInfo.RedirectStandardInput = True
    16.             .StartInfo.RedirectStandardOutput = True
    17.             ' Start the process
    18.             .Start()
    19.             Dim strOutput As String = .StandardOutput.ReadToEnd
    20.             ' Wait for process to finish
    21.             oProcess.WaitForExit(2000) '5000
    22.             If .HasExited = False Then .Kill()
    23.             .Close()
    24.         End With
    25.         sFN = System.IO.Path.GetFileNameWithoutExtension(sFilePath)
    26.         sFilePath = System.IO.Path.Combine(sOutputFolderPath.Replace("\pdf", ""), sFN & ".jpg")
    27.  
    28.         'Call cFunctions.SendMail(Server.MachineName & " - Success converted image to JPG", "Converted image to JPG: \\" & Server.MachineName & "\" & sFilePath.Replace(":", "") & vbNewLine, sSupportEmails, sSupportNames, "[email protected]", "PDF Conversion Service")
    29.         Call cFunctions.WriteToFile("Success converted image to JPG: " & sFilePath & vbNewLine)
    30.     Catch ex As Exception
    31.         Call cFunctions.SendMail(Server.MachineName & " - Failed to convert image to JPG", "File path: \\" & Server.MachineName & "\" & sFilePath.Replace(":", "") & vbNewLine & ex.Message & vbNewLine & ex.StackTrace & vbNewLine, sSupportEmails, sSupportNames, "[email protected]", "PDF Conversion Service")
    32.         Call cFunctions.WriteToFile("Failed to convert image to JPG" & vbNewLine & "File path: " & sFilePath & vbNewLine & ex.Message & vbNewLine & ex.StackTrace & vbNewLine, False, Application("LogFilePath"), Application("LogFile"))
    33.     End Try
    34.     ' Finish Impersonating here
    35.     iu.undoImpersonation()
    36. Else
    37.     Call cFunctions.SendMail(Server.MachineName & " - Failed to impersonate user", "Error: converting image to JPG - failed to impersonate user!" & vbNewLine & vbNewLine & "File path: \\" & Server.MachineName & "\" & sFilePath.Replace(":", "") & vbNewLine, sSupportEmails, sSupportNames, "[email protected]", "PDF Conversion Service")
    38.     Call cFunctions.WriteToFile("Failed to impersonate user" & vbNewLine & "Error: converting image to JPG - failed to impersonate user!" & vbNewLine & "File path: " & sFilePath, False, Application("LogFilePath"), Application("LogFile"))
    39. End If

    If I remove the impersonation stuff the command executes but my images are stamped with the coversion tools logo which is a limitation of the trial version. The image conversion tool needs to be run by the user that installed and registered it. I'm confident that the impersonation works as I use this class to copy files accross the network in other areas of my applicaiton.

    The command file is also okay because if I manually run the command file (ReaConvertFile.cmd) the image file is converted.

    I know that the licence is installed correctly because if I log into the server as the relevant user and run the conversion tool manually (through the GUI or manually execute the .cmd file) the files convert and are not stamped with the conversion tools logo.

    I think something is missing the correct permissions? I tried giving my user full control of ...\system32\cmd.exe but that didn't help????

    Any thoughts or ideas will be greatly appreciated.
    Last edited by aconybeare; Jan 11th, 2011 at 07:41 AM. Reason: Fixed gramma.

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