2016-01-21 7 views
6

हो रही अपवाद का उपयोग कर सी # रिमोट मशीन में एक Windows सेवा शुरू करने के लिए या आईआईएस बंद करो और भी ... eventhough मैं रिमोट मशीनकैसे इस कोड के साथ

class Program 
    { 
     static void Main(string[] args) 
     { 
      var sc = new System.ServiceProcess.ServiceController("W3SVC", "10.201.58.114"); 
      sc.Start(); 
      sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);    
      sc.Stop(); 
      sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped); 
     } 
    } 

अपवाद में प्रशासक विशेषाधिकारों है:

खोला नहीं जा सकता सेवा सी:

प्रकार 'System.InvalidOperationException' की एक बिना क्रिया का अपवाद System.ServiceProcess.dll

अतिरिक्त जानकारी में हुई कंप्यूटर पर ऑनरोल प्रबंधक '10 .201.58.114 '। इस ऑपरेशन को अन्य विशेषाधिकारों की आवश्यकता हो सकती है।

+0

क्या लक्ष्य प्रणाली सुरक्षा सॉफ्टवेयर से मुक्त है? आप पाएंगे कि कुछ सुरक्षा सॉफ्टवेयर का यह प्रभाव हो सकता है। – Xefan

+1

क्या यह ऐप "व्यवस्थापक के रूप में" चलाने में मदद करेगा? यह भी मानते हुए कि आपके पास आईपी के अधिकार हैं ... – vidriduch

+0

कमांड प्रॉम्प्ट के साथ प्रशासक के रूप में उपयोग: 'सी: \> iisreset।exe <दूरस्थ कंप्यूटर का आईपी पता> ', त्रुटि को इस प्रकार प्राप्त करना: 'एक्सेस अस्वीकृत, आप इस कमांड का उपयोग करने के लिए दूरस्थ कंप्यूटर का व्यवस्थापक होना चाहिए। या तो आपका खाता दूरस्थ कंप्यूटर या डोमेन व्यवस्थापक वैश्विक समूह के व्यवस्थापक स्थानीय समूह में जोड़ा गया है। कृपया ध्यान दें कि मेरे पास दूरस्थ मशीन में व्यवस्थापक अधिकार हैं लेकिन अभी भी त्रुटि हो रही है। – venkat

उत्तर

0

लगता है कि आपने रिमोट मशीन

पर

आपको सम्भवता प्रबंधन व्यवस्थापकों

के साथ इस localgroup प्रशासकों के लिए

चेक नहीं जोड़े जाते उपयोगकर्ता है अगर सूची में नहीं,

चलाएं

आपको सम्भवता प्रबंधन प्रशासक/जोड़ने उपयोगकर्ता

+0

मेरा नाम पहले से ही प्रशासक के स्थानीय समूह में सूचीबद्ध है। लेकिन अभी भी एक ही अपवाद प्राप्त हो रहा है। – venkat

1

एक ही डोमेन में मशीनों हैं? यदि मशीन 1 पर Administrator नहीं है तो मशीन 2 पर Administrator समतुल्य नहीं है, इसलिए यह आपकी समस्या हो सकती है। दूसरा कोड की टिप्पणियों में

SUBINACL /SERVICE \\<MACHINE>\W3SVC /GRANT=<MACHINE>\<USER>=TO 

इस का एक उदाहरण है: तो जैसे बंद करो और शुरू करने के लिए सेवा - रिमोट मशीन पर -

एक संभावना यह है कि आप इस उपयोगकर्ता के लिए पहुँच प्रदान करनी है नीचे ब्लॉक करें (क्योंकि मुझे इन-कोड पहचान प्रतिरूपण के साथ भी इसकी आवश्यकता है)।

यदि यह समस्या का समाधान नहीं करता है, तो आप दूरस्थ उपयोगकर्ता का प्रतिरूपण करने का प्रयास कर सकते हैं। मैं निम्नलिखित कोड का उपयोग कर यह काम करने में कामयाब रहा।

सबसे पहले, एक नया वर्ग WrapperImpersonationContext.cs बनाएँ:

using System; 
using System.Runtime.InteropServices; 
using System.Security.Principal; 
using System.Security.Permissions; 
using System.ComponentModel; 

//Version from http://michiel.vanotegem.nl/2006/07/windowsimpersonationcontext-made-easy/ 

public class WrapperImpersonationContext 
{ 
    [DllImport("advapi32.dll", SetLastError = true)] 
    public static extern bool LogonUser(String lpszUsername, String lpszDomain, 
     String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
    public extern static bool CloseHandle(IntPtr handle); 

    private const int LOGON32_PROVIDER_DEFAULT = 0; 
    private const int LOGON32_LOGON_INTERACTIVE = 2; 

    private string m_Domain; 
    private string m_Password; 
    private string m_Username; 
    private IntPtr m_Token; 

    private WindowsImpersonationContext m_Context = null; 


    protected bool IsInContext 
    { 
     get { return m_Context != null; } 
    } 

    public WrapperImpersonationContext(string domain, string username, string password) 
    { 
     m_Domain = domain; 
     m_Username = username; 
     m_Password = password; 
    } 

    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] 
    public void Enter() 
    { 
     if (this.IsInContext) return; 
     m_Token = new IntPtr(0); 
     try 
     { 
      m_Token = IntPtr.Zero; 
      bool logonSuccessfull = LogonUser(
       m_Username, 
       m_Domain, 
       m_Password, 
       LOGON32_LOGON_INTERACTIVE, 
       LOGON32_PROVIDER_DEFAULT, 
       ref m_Token); 
      if (logonSuccessfull == false) 
      { 
       int error = Marshal.GetLastWin32Error(); 
       throw new Win32Exception(error); 
      } 
      WindowsIdentity identity = new WindowsIdentity(m_Token); 
      m_Context = identity.Impersonate(); 
     } 
     catch (Exception exception) 
     { 
      // Catch exceptions here 
     } 
    } 


    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] 
    public void Leave() 
    { 
     if (this.IsInContext == false) return; 
     m_Context.Undo(); 

     if (m_Token != IntPtr.Zero) CloseHandle(m_Token); 
     m_Context = null; 
    } 
} 

तो आप निम्न को चलाने के लिए सक्षम होना चाहिए। ध्यान दें कि आपको अपने सेटअप से मेल खाने के लिए मशीन-नाम, उपयोगकर्ता नाम और पासवर्ड बदलना होगा।

//Code for Program.cs demonstrating the identity impersonation for a ServiceController. 

using System; 
using System.Security.Principal; 
using System.ServiceProcess; 

namespace RemoteConnectionTest 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 

      try { 

       //Based on the code from http://michiel.vanotegem.nl/2006/07/windowsimpersonationcontext-made-easy/ 

       Console.WriteLine("Current user: " + WindowsIdentity.GetCurrent().Name); 
       //Also worked with the IP address of GARNET (which was the machine name). 
       WrapperImpersonationContext context = new WrapperImpersonationContext("GARNET", "TestAdmin1", "password123"); 
       context.Enter(); 
       // Execute code under other uses context 
       Console.WriteLine("Current user: " + WindowsIdentity.GetCurrent().Name); 

       // Code to execute. 

       //Try running the following command on the remote server first to ensure 
       //the user has the appropriate access (obviously substitute the 
       //username and machine name). 
       // runas /user:TestAdmin "sc \\GARNET stop W3SVC" 

       //Also, make sure the user on the remote server has access for 
       //services granted as described here: http://stackoverflow.com/a/5084563/201648 
       //Otherwise you may see an error along the lines of: 
       //Cannot open W3SVC service on computer '<SERVER>'. ---> System.ComponentModel.Win32Exception: Access is denied 
       //For my configuration I had to run the command: 
       // SUBINACL /SERVICE \\GARNET\W3SVC /GRANT=GARNET\TestAdmin=TO 
       //It's entirely possible that running this command will allow your existing code to work without using impersonation. 

       //You may need to install SUBINACL https://www.microsoft.com/en-au/download/details.aspx?id=23510 
       //By default SUBINACL will install to C:\Program Files (x86)\Windows Resource Kits\Tools 
       //so CD to that directory and then run the SUBINACL command above. 

       //Also worked with the IP address of GARNET (which was the machine name). 
       var sc = new ServiceController("W3SVC", "GARNET"); 
       sc.Start(); 

       sc.WaitForStatus(ServiceControllerStatus.Running);    
       sc.Stop(); 
       sc.WaitForStatus(ServiceControllerStatus.Stopped); 

       //END - code to execute. 
       context.Leave(); 
       Console.WriteLine("Your code ran successfully. Current user: " + WindowsIdentity.GetCurrent().Name); 

      } catch (Exception ex) { 
       Console.WriteLine("An exception occured - details as follows: {0}", ex.Message); 
       Console.WriteLine("The full stack trace is: {0}", ex); 
      } 

      Console.WriteLine ("Press any key to exit..."); 
      Console.ReadLine(); 

     } 
    } 

} 

सुनिश्चित प्रदान की क्रेडेंशियल्स का उपयोग कि रिमोट मशीन मूल्यांकन किया जा सकता से पहले आप जैसे कोड में यह करने के लिए प्रयास करें: वहाँ के रूप में महत्वपूर्ण सुरक्षा सेटिंग्स जानकारी है कि मैं रास्ते की खोज की भी टिप्पणी करने के लिए ध्यान देना रिमोट डेस्कटॉप के माध्यम से इस उपयोगकर्ता के रूप में कनेक्ट करके।

+0

** त्रुटि **: 'प्रकार या नामस्थान नाम' RemoteAccessHelper 'नहीं मिला (क्या आप एक प्रयोग निर्देश या असेंबली संदर्भ खो रहे हैं?) ' – venkat

+0

क्या आपने ट्यूटोरियल में कोड को देखा है http: // dotnet- assembly.blogspot.com.au/2012/11/c-accessing-remote-file-by-using.html –

+0

तो बस स्पष्ट करने के लिए 1) रिमोट मशीन के कंप्यूटर/डोमेन नाम का निर्धारण करें 2) रिमोट से कनेक्ट करने का प्रयास करें मशीन से मशीन जहां आपका कोड उस कंप्यूटर/डोमेन नाम और कोड के बाहर उपयोगकर्ता नाम और पासवर्ड का उपयोग करने से चल रहा है, उदाहरण के लिए दूरस्थ डेस्कटॉप का उपयोग 3) यदि आप सफल हैं, तो उस ट्यूटोरियल में कोड के आधार पर सी # प्रतिरूपण का प्रयास करें। साथ ही, क्या आपने यह निर्धारित किया है कि आप डोमेन पर हैं या नहीं? यदि दोनों मशीनें एक ही डोमेन पर हैं तो यह कुछ अन्य समस्या हो सकती है। –

संबंधित मुद्दे