0

how to retrieve gMSA account password in C#?

I need to use gMSA account to disable/enable AD account.

But I don't know how to get gMSA password?

public static bool DisableUserAccount(string sUserName)
        {
           
            UserPrincipal oUserPrincipal = GetUserForDoamin("D-Domain", sUserName);
            oUserPrincipal.Enabled = false;
            oUserPrincipal.Save();
           
            return true;

        }
               
        public static UserPrincipal GetUserForDoamin(string sDomain, string sUserName)
        {
           
            PrincipalContext PrincipalDomain = null;
            
                    PrincipalDomain = GetPrincipalContext("D-Domain", "NONE"); ;
             

            UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(PrincipalDomain, sUserName);
             
                return oUserPrincipal;

        }

        public static PrincipalContext GetPrincipalContext(string sDomainZone, string sOU)
        {
           
                PrincipalContext ServerCredentials = null;
            string sDomainName = "";

            
                    sDomainName = "D-Domain";
                    ServerCredentials = new PrincipalContext(ContextType.Domain, sDomainName, "gMSA$", gMSApassword);
                    break;

            

            return ServerCredentials;
            }

At [GetPrincipalContext], gMSApassword is not nullable column. I try "" & null, but it is not working. I don't know how to setup.

Frank Kuo
  • 17
  • 2

0 Answers0