Category: C# General

Which ever command you run will run under the impersonated user which .NET is running so make sure the user you are impersonating has permission to do anything that the commandline command wishes to execute, save, read etc.

Add a new reference to System.Management Assembly. Use the library in code as below:

using System.Management;

Add the following function to a class:

 private static void RunWMI(string appString, string argString)
    {
        try
        {
            ConnectionOptions options = new ConnectionOptions();
            // because we are running against the local machine we can't validate
            // we are impersonating at this point so we have the correct security
            // level
            ManagementScope WmiScope = new ManagementScope();
            WmiScope.Connect();
            ManagementClass processClass = new ManagementClass("Win32_Process");
            processClass.Scope = WmiScope;
            //Get an input parameters object for this method
            ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
            //Fill in input parameter values
            inParams["CommandLine" = appString + " " + argString;
            // this will execute our command but it will not wait for the job to
            //complete...
            ManagementBaseObject outParams = processClass.InvokeMethod("Create",
            inParams, null);
        }
        catch
        {
            throw;
        }
    }

To use impersonation, use inline code impersonation for the code block only, this will ensure that the exe is run using the user permissions assigned.

Execute the function as below:

RunWMI(@"D:ffmpgffmpeg.exe", @"-y -i D:ffmpgkalin.wmv -f flv -ar 44100 D:ffmpgkalin.flv");

 

Tags:

C#

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count: