/// <summary>
        /// Reset the registry values
        /// </summary>
        private static void ResetRegistry()
        {
            // sql
            string sqlInstanceName    = GetSQLServerInstanceNameStr(false);
            string wapSqlInstanceName = GetSQLServerInstanceNameStr(true);

            WriteSqlRegistryValue(SetupConstants.InstanceNameRegistryValueName, sqlInstanceName);
            WriteWapSqlRegistryValue(SetupConstants.WapInstanceNameRegistryValueName, wapSqlInstanceName);
            string dbName = (String)SetupInputs.Instance.FindItem(SetupInputTags.SqlDatabaseNameTag);

            WriteSqlRegistryValue(SetupConstants.DBNameRegistryValueName, dbName);
            string wapDBName = (String)SetupInputs.Instance.FindItem(SetupInputTags.WapSqlDatabaseNameTag);

            WriteWapSqlRegistryValue(SetupConstants.WapDbNameRegistryValueName, wapDBName);
            string partialConnectionString    = SetupDatabaseHelper.ConstructConnectionString(sqlInstanceName);
            string wapPartialConnectionString = SetupDatabaseHelper.ConstructConnectionString(wapSqlInstanceName);
            string connectionString           = String.Format("{0}database={1}", partialConnectionString, dbName);
            string wapConnectionString        = String.Format("{0}database={1}", partialConnectionString, wapDBName);

            WriteSqlRegistryValue(SetupConstants.ConnectionStringRegistryValueName, connectionString);
            WriteWapSqlRegistryValue(SetupConstants.ConnectionStringRegistryValueName, wapConnectionString);

            string sqlMachineName  = DnsHelper.GetComputerNameFromFqdnOrNetBios((String)SetupInputs.Instance.FindItem(SetupInputTags.GetSqlMachineNameTag(false)));
            bool   onRemoteMachine = String.Compare(sqlMachineName, Environment.MachineName, true) != 0;

            WriteSqlRegistryValue(SetupConstants.OnRemoteRegistryValueName, onRemoteMachine ? 1 : 0);
            WriteSqlRegistryValue(SetupConstants.MachineNameRegistryValueName, sqlMachineName);
            String sqlMachineFqdn = DnsHelper.GetFullyQualifiedName(sqlMachineName);

            WriteSqlRegistryValue(SetupConstants.FqdnRegistryValueName, sqlMachineFqdn);

            sqlMachineName  = DnsHelper.GetComputerNameFromFqdnOrNetBios((String)SetupInputs.Instance.FindItem(SetupInputTags.GetSqlMachineNameTag(true)));
            onRemoteMachine = String.Compare(sqlMachineName, Environment.MachineName, true) != 0;
            WriteWapSqlRegistryValue(SetupConstants.OnRemoteRegistryValueName, onRemoteMachine ? 1 : 0);
            WriteWapSqlRegistryValue(SetupConstants.MachineNameRegistryValueName, sqlMachineName);
            sqlMachineFqdn = DnsHelper.GetFullyQualifiedName(sqlMachineName);
            WriteWapSqlRegistryValue(SetupConstants.FqdnRegistryValueName, sqlMachineFqdn);

            // user name, company name under server
            String userName = SetupInputs.Instance.FindItem(SetupInputTags.UserNameTag);

            WriteServerRegistrationRegistryValue(SetupConstants.UserNameRegistryValueName, userName);

            // VmmServiceAccount
            String serviceAccount = UserAccountHelper.GetServiceAccount();

            if (SetupInputs.Instance.FindItem(SetupInputTags.CmpServiceLocalAccountTag))
            {
                serviceAccount = SetupConstants.LocalSystem;
            }

            WriteConfigSettingsRegistryValue(SetupConstants.ServerSetupInfoRegKey, SetupConstants.VmmServiceAccountValueName, serviceAccount);
        }
        public static string ConfigureServerCommandLineArguments()
        {
            StringBuilder arguments = new StringBuilder();

            // Check to see if this is an uninstall
            if (PropertyBagDictionary.Instance.PropertyExists(PropertyBagConstants.Uninstall))
            {
                arguments.Append("REMOVE=ALL ");
                arguments.AppendFormat(CultureInfo.InvariantCulture, "UNINSTALLSERVER = 1 ");
            }
            else
            {
                String serviceAccount = UserAccountHelper.GetServiceAccount();
                arguments.AppendFormat(CultureInfo.InvariantCulture, "SERVERACCOUNTNAME=\"{0}\" ", serviceAccount);  // for agent installation
                if (SetupInputs.Instance.FindItem(SetupInputTags.CmpServiceLocalAccountTag))
                {
                    serviceAccount = SetupConstants.LocalSystem;
                }
                arguments.AppendFormat(CultureInfo.InvariantCulture, "SERVICEACCOUNT=\"{0}\" ", serviceAccount); // for registry
                arguments.AppendFormat(CultureInfo.InvariantCulture, "USERNAME=\"{0}\" ", (String)SetupInputs.Instance.FindItem(SetupInputTags.UserNameTag));

                arguments.AppendFormat(CultureInfo.InvariantCulture, "SQLPORT=\"{0}\" ", (int)SetupInputs.Instance.FindItem(SetupInputTags.SqlServerPortTag));
                string sqlInstanceName = InstallItemCustomDelegates.GetSQLServerInstanceNameStr(false);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "INSTANCENAME=\"{0}\" ", sqlInstanceName);

                String dbName = (String)SetupInputs.Instance.FindItem(SetupInputTags.SqlDatabaseNameTag);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "DATABASENAME=\"{0}\" ", dbName);
                string partialConnectionString = SetupDatabaseHelper.ConstructConnectionString(sqlInstanceName);
                string connectionString        = String.Format("{0}database={1}", partialConnectionString, dbName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "CONNECTIONSTR=\"{0}\" ", connectionString);

                string sqlMachineName  = DnsHelper.GetComputerNameFromFqdnOrNetBios((String)SetupInputs.Instance.FindItem(SetupInputTags.GetSqlMachineNameTag(false)));
                bool   onRemoteMachine = String.Compare(sqlMachineName, Environment.MachineName, true) != 0;
                arguments.AppendFormat(CultureInfo.InvariantCulture, "ONREMOTESERVER=\"{0}\" ", onRemoteMachine ? 1 : 0);

                String sqlMachineFqdn = DnsHelper.GetFullyQualifiedName(sqlMachineName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "SQLMACHINENAME=\"{0}\" ", sqlMachineName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "SQLMACHINEFQDN=\"{0}\" ", sqlMachineFqdn);

                arguments.AppendFormat(CultureInfo.InvariantCulture, "SETUPLANGUAGE=\"{0}\" ", CultureInfo.CurrentUICulture.Name);

                String certificateThumbprint = (String)SetupInputs.Instance.FindItem(SetupInputTags.CmpCertificateThumbprintTag);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "CERTIFICATETHUMBPRINT=\"{0}\" ", "LocalMachine,My," + certificateThumbprint);

                // Encrypt the password of the worker service so that WAP extensions can use it
                InputParameter pwd            = SetupInputs.Instance.FindItem(SetupInputTags.SqlDBAdminPasswordTag);
                string         passwordAsText = null;
                if (pwd != null)
                {
                    IntPtr unmanagedString = IntPtr.Zero;
                    try
                    {
                        unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(pwd);
                        passwordAsText  = Marshal.PtrToStringUni(unmanagedString);
                    }
                    finally
                    {
                        Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
                    }
                }
                string encryptedPassword = String.Format("{0}{1}{2}", "[KText]", UserAccountHelper.EncryptStringUsingLocalCertificate(passwordAsText, certificateThumbprint), "[KText]");

                arguments.AppendFormat(CultureInfo.InvariantCulture, "CMPCONTEXTPASSWORDSTRING=\"{0}\" ", encryptedPassword);
            }

            return(arguments.ToString());
        }
        public static string ConfigureWAPExtensionCommonCommandLineArguments()
        {
            StringBuilder arguments = new StringBuilder();

            // Check to see if this is an uninstall
            if (PropertyBagDictionary.Instance.PropertyExists(PropertyBagConstants.Uninstall))
            {
                arguments.Append("REMOVE=ALL ");
            }
            else
            {
                arguments.Append("ADDLOCAL=ProductFeature,ServiceFeature ");

                // Save the user name to use on the cmp service database
                String userName = SetupInputs.Instance.FindItem(SetupInputTags.SqlDBAdminNameTag);

                // Add the SQL database information so that it can be written to the registry and accessed by the services
                arguments.AppendFormat(CultureInfo.InvariantCulture, "SQLPORT=\"{0}\" ", (int)SetupInputs.Instance.FindItem(SetupInputTags.SqlServerPortTag));
                string sqlInstanceName = InstallItemCustomDelegates.GetSQLServerInstanceNameStr(false);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "INSTANCENAME=\"{0}\" ", sqlInstanceName);

                arguments.AppendFormat(CultureInfo.InvariantCulture, "WAPSQLPORT=\"{0}\" ", (int)SetupInputs.Instance.FindItem(SetupInputTags.WapSqlServerPortTag));
                string wapSqlInstanceName = InstallItemCustomDelegates.GetSQLServerInstanceNameStr(true);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "WAPINSTANCENAME=\"{0}\" ", wapSqlInstanceName);

                String dbName = (String)SetupInputs.Instance.FindItem(SetupInputTags.SqlDatabaseNameTag);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "DATABASENAME=\"{0}\" ", dbName);
                string partialConnectionString = SetupDatabaseHelper.ConstructWebsiteConnectionString(SetupDatabaseHelper.SqlUsernameDuringInstall, sqlInstanceName);
                string connectionString        = String.Format("{0}database={1}", partialConnectionString, dbName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "CMPCONNECTIONSTR=\"{0}\" ", connectionString);

                String wapDbName = (String)SetupInputs.Instance.FindItem(SetupInputTags.WapSqlDatabaseNameTag);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "WAPDATABASENAME=\"{0}\" ", wapDbName);
                string wapPartialConnectionString = SetupDatabaseHelper.ConstructWebsiteConnectionString(SetupDatabaseHelper.SqlUsernameDuringInstall, wapSqlInstanceName);
                string wapConnectionString        = String.Format("{0}database={1}", wapPartialConnectionString, wapDbName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "WAPCONNECTIONSTR=\"{0}\" ", wapConnectionString);

                string sqlMachineName  = DnsHelper.GetComputerNameFromFqdnOrNetBios((String)SetupInputs.Instance.FindItem(SetupInputTags.GetSqlMachineNameTag(false)));
                bool   onRemoteMachine = String.Compare(sqlMachineName, Environment.MachineName, StringComparison.OrdinalIgnoreCase) != 0;
                arguments.AppendFormat(CultureInfo.InvariantCulture, "ONREMOTESERVER=\"{0}\" ", onRemoteMachine ? 1 : 0);

                string wapSqlMachineName  = DnsHelper.GetComputerNameFromFqdnOrNetBios((String)SetupInputs.Instance.FindItem(SetupInputTags.GetSqlMachineNameTag(true)));
                bool   wapOnRemoteMachine = String.Compare(wapSqlMachineName, Environment.MachineName, StringComparison.OrdinalIgnoreCase) != 0;
                arguments.AppendFormat(CultureInfo.InvariantCulture, "WAPONREMOTESERVER=\"{0}\" ", wapOnRemoteMachine ? 1 : 0);

                String sqlMachineFqdn = DnsHelper.GetFullyQualifiedName(sqlMachineName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "SQLMACHINENAME=\"{0}\" ", sqlMachineName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "SQLMACHINEFQDN=\"{0}\" ", sqlMachineFqdn);

                String wapSqlMachineFqdn = DnsHelper.GetFullyQualifiedName(sqlMachineName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "WAPSQLMACHINENAME=\"{0}\" ", wapSqlMachineName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "WAPSQLMACHINEFQDN=\"{0}\" ", wapSqlMachineFqdn);

                String certificateThumbprint = (String)SetupInputs.Instance.FindItem(SetupInputTags.CmpCertificateThumbprintTag);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "CERTIFICATETHUMBPRINT=\"{0}\" ", "LocalMachine,My," + certificateThumbprint);

                // Write the cmp database connection string
                sqlInstanceName            = InstallItemCustomDelegates.GetSQLServerInstanceNameStr(false);
                wapDbName                  = (String)SetupInputs.Instance.FindItem(SetupInputTags.SqlDatabaseNameTag);
                wapPartialConnectionString = SetupDatabaseHelper.ConstructWebsiteConnectionString(SetupDatabaseHelper.SqlUsernameDuringInstall, sqlInstanceName);
                wapConnectionString        = String.Format("{0}database={1}", wapPartialConnectionString, wapDbName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "CMPCONNECTIONSTR=\"{0}\" ", wapConnectionString);

                String domain       = SetupInputs.Instance.FindItem(SetupInputTags.SqlDBAdminDomainTag);
                string fullUserName = String.IsNullOrEmpty(domain) ? userName : domain + @"\" + userName;
                arguments.AppendFormat(CultureInfo.InvariantCulture, "CMPDATABASEUSERNAME=\"{0}\" ", fullUserName);

                // Encrypt the password of the worker service so that WAP extensions can use it
                InputParameter pwd            = SetupInputs.Instance.FindItem(SetupInputTags.SqlDBAdminPasswordTag);
                string         passwordAsText = null;
                if (pwd != null)
                {
                    IntPtr unmanagedString = IntPtr.Zero;
                    try
                    {
                        unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(pwd);
                        passwordAsText  = Marshal.PtrToStringUni(unmanagedString);
                    }
                    finally
                    {
                        Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
                    }
                }
                string encryptedPassword = String.Format("{0}{1}{2}", "[KText]", UserAccountHelper.EncryptStringUsingLocalCertificate(passwordAsText, certificateThumbprint), "[KText]");

                arguments.AppendFormat(CultureInfo.InvariantCulture, "CMPCONTEXTPASSWORDSTRING=\"{0}\" ", encryptedPassword);

                //Write the MicrosoftMgmtSvcStoreContext connection string (part of WAP original installation)
                wapDbName = SetupConstants.DefaultWapStoreDBName;
                wapPartialConnectionString = SetupDatabaseHelper.ConstructWebsiteConnectionString(SetupDatabaseHelper.SqlUsernameDuringInstall, sqlInstanceName);
                wapConnectionString        = String.Format("{0}database={1}", wapPartialConnectionString, wapDbName);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "WAPSTORECONNECTIONSTR=\"{0}\" ", wapConnectionString);

                X509Krypto krypto = new X509Krypto("My", "LocalMachine", certificateThumbprint);
                // Encrypt the password of the sql user that will be used by the website to access CMP DB
                //string encryptedCmpDbPassword = String.Format("{0}{1}{2}", "[KText]", UserAccountHelper.EncryptStringUsingLocalCertificate(SetupDatabaseHelper.SqlDbUserPassword, certificateThumbprint), "[KText]");

                string encryptedCmpDbPassword = krypto.EncyptKText(SetupDatabaseHelper.SqlDbUserPassword);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "CMPDBPASSWORDSTRING=\"{0}\" ", encryptedCmpDbPassword);

                // Encrypt the password of the sql user that will be used by the website to access CMP WAP DB
                //string encryptedCmpWapDbPassword = String.Format("{0}{1}{2}", "[KText]", UserAccountHelper.EncryptStringUsingLocalCertificate(SetupDatabaseHelper.SqlDbUserPassword, certificateThumbprint), "[KText]");
                string encryptedCmpWapDbPassword = krypto.EncyptKText(SetupDatabaseHelper.SqlDbUserPassword);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "CMPWAPDBPASSWORDSTRING=\"{0}\" ", encryptedCmpWapDbPassword);

                // Encrypt the password of the sql user that will be used by the website to access WAP's Microsoft.MgmtSvc.Store DB
                //string encryptedMgmtStoreDbPassword = String.Format("{0}{1}{2}", "[KText]", UserAccountHelper.EncryptStringUsingLocalCertificate(SetupDatabaseHelper.SqlDbUserPassword, certificateThumbprint), "[KText]");
                string encryptedMgmtStoreDbPassword = krypto.EncyptKText(SetupDatabaseHelper.SqlDbUserPassword);
                arguments.AppendFormat(CultureInfo.InvariantCulture, "MGMTSTOREPASSWORDSTRING=\"{0}\" ", encryptedMgmtStoreDbPassword);
            }

            return(arguments.ToString());
        }