-2

I am trying to get a list of services from a remote machine using C# but it doesn't work but the same works when I run the PowerShell script under the same user.

 $global:websess = New-PSSession -Computername $webserverNameList.Text -Credential $global:cred -ConfigurationName *JEAconfigname* -Authentication Negotiate

The above code is from the PowerShell script that uses the same domain\username to get a list of services from a remote machine. I tried doing the same from my C# code using ServiceController class and also a combination of ConnectionOption class and ManagementScope but I get access denied error.

        ConnectionOptions connection = new ConnectionOptions();
        connection.Username = "domain\username";
        connection.Password = "password";
        connection.Authority = "";
        connection.EnablePrivileges = true;
        connection.Authentication = AuthenticationLevel.PacketPrivacy;
        connection.Impersonation = ImpersonationLevel.Impersonate;
        ManagementScope scope = new ManagementScope(@"\\" + lstServerNames.SelectedValue.ToString() + @"\root\cimv2");
        scope.Connect(); // Fails here: System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
        ObjectQuery query = new ObjectQuery(
                "SELECT * FROM Win32_Service WHERE Name like '%Vend%'");

        ManagementObjectSearcher searcher =
              new ManagementObjectSearcher(scope, query);

        foreach (ManagementObject queryObj in searcher.Get())
        {
            cmbServices.Add(queryObj["Name"].ToString());
        }

i have tried this too:

List<ServiceController> serviceList = ServiceController.GetServices(lstServerNames.SelectedValue.ToString()).Where(x => x.ServiceName.ToLower().StartsWith("vend") || x.ServiceName.ToLower().StartsWith("vstsagent")).ToList(); 
// Fails here: System.InvalidOperationException: 'Cannot open Service Control Manager on computer '*server name*'. This operation might require other privileges.'
        foreach (ServiceController service in serviceList)
        {
            cmbServices.Add(service.ServiceName);
        }

I know the user is now given admin access. The existing PowerShell using Just Enough Administration (JEA) access to run the script. https://msdn.microsoft.com/en-us/library/dn896648.aspx

Aswin Francis
  • 87
  • 1
  • 13
  • Possible duplicate of [Connect to Microsoft Exchange PowerShell within C#](https://stackoverflow.com/questions/36236897/connect-to-microsoft-exchange-powershell-within-c-sharp). The answers in this question should get you close to what you're trying to do. – Bacon Bits Oct 13 '17 at 12:36
  • How about showing some example code of the failing C# app? doesn't make much sense to share working PoSh example and ask for help with PowerShell... – Clijsters Oct 13 '17 at 12:37
  • @Clijsters i have updated the code – Aswin Francis Oct 13 '17 at 12:54

1 Answers1

0

You're using the wrong ManagementScope constructor. You're not specifying the options.

Try:

ManagementScope scope = new ManagementScope(@"\\" + lstServerNames.SelectedValue.ToString() + @"\root\cimv2", connection);
scope.Connect();

Read through the example in the linked document above, too.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • I am sorry I didn't update the code. I had added the connection options but it still won't work. The thing is the logged in user has access to the remote server but I cant get it to load services and such. With PowerShell, it works using the idea of JEA(Just enough administration). I was wondering if we have something similar to C# so i can call it the same way – Aswin Francis Oct 16 '17 at 05:19