Using WMI to start Remote Command File

G

Guest

I am trying to use the System.Mangement class within a c-sharp application
for the purpose of starting a simple batch file on a remote machine. The
error I receive is this:

1) Exception Information
*********************************************
Exception Type: System.InvalidCastException
Message: Specified cast is not valid.
TargetSite: System.Management.IWbemServices GetIWbemServices()
HelpLink: NULL
Source: System.Management


When I look in the WBEM Logs folder, there is no information provided and I
am having little luck figuring out what is being cast incorrectly.

Here's a code snippet -

string appPath = (Value from config file);
string filename;
ManagementClass processBatch = new ManagementClass("Win32_Process");
//Get Name of Server from App.Config
string strComputer = (name of server);

//Get an input parameters object for this method
ManagementBaseObject inParams = processBatch.GetMethodParameters("Create");

filename = string.Concat("nof_",DateTime.Today.Year.ToString(),
string.Format("{0:0#}",DateTime.Today.Month),
string.Format("{0:0#}",DateTime.Today.Day), ".zip");

ManagementClass startJob = new ManagementClass("Win32_ProcessStartup");

//Fill in input parameter values
inParams["CurrentDirectory"] = appPath;
inParams["CommandLine"] =string.Concat(@"batchname ", filename.ToString());
//batchname is batch file that creates a zip file and ftp's the file to
remote network
inParams["ProcessStartupInformation"] = startJob;

ManagementScope oWMIManagementScope;
ConnectionOptions oWMIConnect = new ConnectionOptions();
oWMIConnect.Impersonation = ImpersonationLevel.Impersonate;
oWMIConnect.Authentication = AuthenticationLevel.Connect;
string connPath = string.Concat(@"\\", strComputer, @"\root\cimv2");

oWMIManagementScope = new ManagementScope(connPath, oWMIConnect);
processBatch.Scope = oWMIManagementScope;

//Execute the method
ManagementBaseObject outParams = null;
outParams = processBatch.InvokeMethod("Create", inParams, null);


Can anyone give a suggestion on where to look next to troubleshoot? Am I
trying to do the impossible?

Thanks,
 
G

Gary Chang[MSFT]

Hi Mel,

Thank you posting!
Am I trying to do the impossible?

There is no problem to use the Win32_Process to create a remote process.
But first the Win32_Process should be the instance in the target remote
machine. Then the corresponding WMI connection's authentication mode should
not be the 'Connect', if not, you would get an access denied error.

I tested the following sample code on my side, it works well. I don't use
the Win32_ProcessStartup class in my sample, it seems unnecessary for
creating a simple non-interactive batch process:


string filename = "test.bat";
//the test.bat file is in the target machine's \windows\system32 directory

string strComputer = "ms-testmachine";
//inParams["CurrentDirectory"] = appPath;

ManagementScope oWMIManagementScope;
ConnectionOptions oWMIConnect = new ConnectionOptions();
oWMIConnect.Impersonation = ImpersonationLevel.Impersonate;

string connPath = string.Concat(@"\\", strComputer, @"\root\cimv2");

oWMIManagementScope = new ManagementScope(connPath, oWMIConnect);
ObjectGetOptions o = new ObjectGetOptions(null, System.TimeSpan.MaxValue,
true);
ManagementPath p = new ManagementPath("Win32_Process");

ManagementClass processBatch = new ManagementClass(oWMIManagementScope, p,
o);
ManagementBaseObject inParams = processBatch.GetMethodParameters("Create");
inParams["CommandLine"] = filename;

//Execute the method
ManagementBaseObject outParams = null;
outParams = processBatch.InvokeMethod("Create", inParams, null);


I hope the above sample code helps, if you have any questions or concerns,
please do not hesitate to let me know. I am standing by to help you.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks for the help, Gary.

I made your suggested changes and it has eliminated the error. However, it
looks like I have a little more tweaking to do because the batch does not
seem to be firing on the remote machine. It may take me some time to get
back to this, though, since my customer just informed me that the batch file
is no longer needed. (At least I got the opportunity to learn more about
WMI...)

Thanks again,
--
Mel


"Gary Chang[MSFT]" said:
Hi Mel,

Thank you posting!
Am I trying to do the impossible?

There is no problem to use the Win32_Process to create a remote process.
But first the Win32_Process should be the instance in the target remote
machine. Then the corresponding WMI connection's authentication mode should
not be the 'Connect', if not, you would get an access denied error.

I tested the following sample code on my side, it works well. I don't use
the Win32_ProcessStartup class in my sample, it seems unnecessary for
creating a simple non-interactive batch process:


string filename = "test.bat";
//the test.bat file is in the target machine's \windows\system32 directory

string strComputer = "ms-testmachine";
//inParams["CurrentDirectory"] = appPath;

ManagementScope oWMIManagementScope;
ConnectionOptions oWMIConnect = new ConnectionOptions();
oWMIConnect.Impersonation = ImpersonationLevel.Impersonate;

string connPath = string.Concat(@"\\", strComputer, @"\root\cimv2");

oWMIManagementScope = new ManagementScope(connPath, oWMIConnect);
ObjectGetOptions o = new ObjectGetOptions(null, System.TimeSpan.MaxValue,
true);
ManagementPath p = new ManagementPath("Win32_Process");

ManagementClass processBatch = new ManagementClass(oWMIManagementScope, p,
o);
ManagementBaseObject inParams = processBatch.GetMethodParameters("Create");
inParams["CommandLine"] = filename;

//Execute the method
ManagementBaseObject outParams = null;
outParams = processBatch.InvokeMethod("Create", inParams, null);


I hope the above sample code helps, if you have any questions or concerns,
please do not hesitate to let me know. I am standing by to help you.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Gary Chang[MSFT]

You are welcome Mel!

Thanks for your quick response.

If you still have interest on this issue, I suggest you can debug into the
program and check the variables of 'filename' and
'/inParams["CurrentDirectory"]' before invoke the Win32_Process.Create
method. My sample batch file is in the target machine's \windows\system32\
directory, and just contained a single command line to copy one file for
test.

Good Luck!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top