0

I was trying to call a .cmd file remotely thru WMI in C# with code below:

ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass
            (manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
        inParams["CommandLine"] = @"C:\temp\text.cmd";

Above code returned no error, it even got process ID with outParams["processId"] However, it didn't do anything at remote server. I then tried calling a vbs script with same procedure and it worked just fine. So my question is, what is the proper way to call a .cmd file through WMI? For vbs, I knew I should include the CScript command. But how about .CMD file?

Thanks in advance.

1 Answer 1

1

You should try running "cmd.exe /c c:\temp\text.cmd". I think you need to invoke the command processor to make it work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply.
I did follow your suggestion but the script still didn't do anything. However, after I put the file into Windows/system32 without specify file location in code, it finally works! I did try to set the inParams["CurrentDirectory"] to somewhere else, but it just not working.
Got this all work out. With the suggestion above plus the "CurrentDirectory" setting and now it works! Thanks for your help, Palle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.