Run Command Line Using Windows Management Instrumentation#

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 libray 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:\ffmpg\ffmpeg.exe", @"-y -i D:\ffmpg\kalin.wmv -f flv -ar 44100 D:\ffmpg\kalin.flv");
Back to C#

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-1) was last changed on 24-May-2017 15:30 by UnknownAuthor
G’day (anonymous guest) My Prefs
  • View Page Source
  • This clear IPSec security association,
    clear ipsec sa peer X.X.X.X
    

All Pages

Page views: 1950

Private Tomcat

Linux

MySQL

Email

SQL Server

ASP

JSP

C#

Web Mail

Windows Plesk

Linux Plesk

PHP

Gaming

ASP.NET

Persits ASPUpload

Wiki Help

Referring Pages:
...nobody

JSPWiki v2.8.1