Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Using WMI to start Remote Command File

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
itsomel - 11 Jul 2006 22:41 GMT
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,
Signature

Mel

"Gary Chang[MSFT]" - 12 Jul 2006 09:01 GMT
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.
itsomel - 12 Jul 2006 17:26 GMT
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,
Signature

Mel

> Hi Mel,
>
[quoted text clipped - 51 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Gary Chang[MSFT]" - 13 Jul 2006 03:16 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.