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,
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,