• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Help with solving this code error?

I am getting this error for the code below.  The code actually connects fine, then when it gets to the "inParams..." line it give the error.

I am simply trying to use the WMI to access a remote server that runs Windows 2000, and start one of the native applications, and get a return.  My ID has full permission to the box.

*******************ERROR*******************************
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in system.management.dll
Additional information: Access is denied.
********************************************************

It connects to the server alright, but then errors out at this line.
***********LINE FOR THE ERROR*****************************
inParams = processClass.GetMethodParameters("Create");
********************************************************

Can someone help with this error and look at my code to see what I may be doing wrong?
Cheers

using System;
using System.Management;
namespace ConsoleApplication1
{

public class RemoteConnect
{
      public static void Main()
      {

            //Build an options object for the connection
            ManagementClass processClass = new ManagementClass("Win32_Process");
            processClass.Scope.Path.Server = @"myIP";

            ConnectionOptions options = new ConnectionOptions();
            options.Username = "myID";
            options.Password = "myPassword";

            //Make a connection to a remote computer using these options
            ManagementScope scope = new ManagementScope("\\\\MyIP\\root\\cimv2", options);
            scope.Connect();

            try
            {                        
                  
                  if (scope.IsConnected)
                  {
                        Console.WriteLine("We are connected.");
                              
                  }
                  }            
            catch
            {
                  Console.WriteLine("An Error Occurred:");            
            }       
            //Get an input parameters object for this method
            ManagementBaseObject inParams = null;
            
            inParams = processClass.GetMethodParameters("Create");

            //Fill in input parameter values
            inParams["CommandLine"] = "ap.exe";

            //Execute the method
            ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null);                              
            //Display results
            //Note: The return code of the method is provided in the "returnValue" property of the outParams object
            Console.WriteLine("Creation of calculator process returned: " + outParams["returnValue"]);
            Console.WriteLine(outParams.ToString());
            Console.WriteLine("Process ID: " + outParams["processId"]);
      }}}
0
g118481
Asked:
g118481
  • 5
  • 5
1 Solution
 
Chester_M_RagelCommented:
I checked your code in my machine and it worked without any errors.  "My ID has full permission to the box". Are you sure about this?
0
 
jk_vrCommented:
It seems that the user with supplied login credientials dont have enough permission to start a process. Please check it.
0
 
g118481Author Commented:
I double checked the loging ID and password, and it has full 'Admin' rights.
When I run the program my console pops up and it displays the "We are connected" message.
Which tells me that the ID and password is good.  But then when it tries to start the ap.exe program, it errors out.

Any ideas on this.  Any way I can test differently?

Cheers
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Chester_M_RagelCommented:
Can you use the default admin account(administrator) and test?
0
 
g118481Author Commented:
Yes, I am using the default 'Administrator' ID and password.  I can map a drive to the box. I can login with PCAnywhere with this ID and perform any task.  Also, I can login using Terminal Services Connections utility with this ID.

Is there something I have to set on the box in order for remoting (System.Management, WMI) to be able to function?
I am stuck here.  If I don't get this to work, my manager will certainly be most upset with me.

Any suggestions are most welcome.

HELP!
0
 
Chester_M_RagelCommented:
Sorry friend, I did a small change in your code.. Now only I figured it out. Remove

processClass.Scope.Path.Server = @"myIP";

from your code.

Hope it will work now.
Chester.
0
 
g118481Author Commented:
Chester,

I deleted the line "processClass.Scope.Path.Server = @"myIP";
But now instead of running the "ap.exe" program on the remote PC, the program runs on my local test machine.

How can I correct this.  I am increasing the points for this question.

Cheers
0
 
Chester_M_RagelCommented:
After you create the scope, put this code.

processClass.Scope = scope;
0
 
g118481Author Commented:
Chester,

I added "processClass.Scope = scope;", but the program stops at this line "inParams = processClass.GetMethodParameters("Create");", and gives this error message:

An unhandled exception of type 'System.Management.ManagementException' occurred in system.management.dll
Additional information: Not found
0
 
Chester_M_RagelCommented:
I'll post the the code which I'm using. It works fine whitout any errors...

using System;
using System.Management;
namespace ConsoleApplication1
{

      public class RemoteConnect
      {
            public static void Main()
            {

                  //Build an options object for the connection
                  ManagementClass processClass = new ManagementClass("Win32_Process");
                  
                  ConnectionOptions options = new ConnectionOptions();
                   options.Username = "myID";
                                                 options.Password = "myPassword";

                                               //Make a connection to a remote computer using these options
                                               ManagementScope scope = new ManagementScope("\\\\MyIP\\root\\cimv2", options);
                  scope.Connect();

                  try
                  {                    
               
                        if (scope.IsConnected)
                        {
                              Console.WriteLine("We are connected.");
                         
                        }
                  }          
                  catch
                  {
                        Console.WriteLine("An Error Occurred:");          
                  }      
                  //Get an input parameters object for this method
                  ManagementBaseObject inParams = null;
                  processClass.Scope = scope;
                  inParams = processClass.GetMethodParameters("Create");
                  

                  //Fill in input parameter values
                  inParams["CommandLine"] = "calc.exe";

                  //Execute the method
                  ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null);                        
                  //Display results
                  //Note: The return code of the method is provided in the "returnValue" property of the outParams object
                  Console.WriteLine("Creation of calculator process returned: " + outParams["returnValue"]);
                  Console.WriteLine(outParams.ToString());
                  Console.WriteLine("Process ID: " + outParams["processId"]);
                  Console.ReadLine();

            }
      }
}
0
 
g118481Author Commented:
Chester,

Your suggestion runs.  Thanks for the valuable information.  I will be opening two or three more high value questions this week, if you are interested.  

Thanks again
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now