Пример #1
0
        public void TestConstructors()
        {
            Context.Validate();

            var smoServer = new SmoServer(Context.Connection);

            Assert.IsNotNull(smoServer);

            smoServer.LoadProperty(TEST_PROP);
            Assert.IsNotNull(smoServer.Triggers);
            Assert.IsInstanceOfType(smoServer.Triggers, typeof(ServerDdlTriggerCollection));

            smoServer.LoadProperty(BAD_PROP);
            Server servObj = null;

            try
            {
                servObj = smoServer;
            }
            catch
            {
                Assert.Fail("Implicit conversion of SmoServer to Server failed.");
            }
            // and back...
            try
            {
                var another = (SmoServer)servObj;
            }
            catch
            {
                Assert.Fail("Explicit conversion of Server to SmoServer failed.");
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            var argS = string.Empty;
            var argD = string.Empty;
            var argU = string.Empty;
            var argP = string.Empty;
            var argO = string.Empty;

            if (args.Length != 5)
            {
                ShowHelp();
                return;
            }

            foreach (var arg in args)
            {
                if (arg.StartsWith("-S="))
                {
                    argS = arg.Replace("-S=", "");
                }
                if (arg.StartsWith("-d="))
                {
                    argD = arg.Replace("-d=", "");
                }
                if (arg.StartsWith("-U="))
                {
                    argU = arg.Replace("-U=", "");
                }
                if (arg.StartsWith("-P="))
                {
                    argP = arg.Replace("-P=", "");
                }
                if (arg.StartsWith("-O="))
                {
                    argO = arg.Replace("-O=", "");
                }
            }

            if (string.IsNullOrEmpty(argS) ||
                string.IsNullOrEmpty(argD) ||
                string.IsNullOrEmpty(argU) ||
                string.IsNullOrEmpty(argP) ||
                string.IsNullOrEmpty(argO))
            {
                ShowHelp();
                return;
            }

            //argS = "主機"; argD = "資料庫"; argU = "帳號"; argP = "密碼"; argO = "輸出路徑";

            SmoServer oGenerateDdl = new SmoServer(argS, argD, argU, argP);

            oGenerateDdl.ExportFile(argO);
        }
        public void ExecuteCmdletBase(PSCmdlet callingCmdlet)
        {
            SqlAssemblies sqlAssemblies = new SqlAssemblies();

            if (!sqlAssemblies.LoadedSuccessfully)
            {
                callingCmdlet.WriteWarning(sqlAssemblies.ErrorMessage);
                return;
            }

            try
            {
                SmoServer server = new SmoServer(sqlAssemblies, DatabaseServer);
                server.SetApplicationName("GlymaNewMapSecurityDbInstaller");

                SmoDatabase database = new SmoDatabase(sqlAssemblies, server, DatabaseName);

                SmoFileGroup fileGroup = new SmoFileGroup(sqlAssemblies, database, "PRIMARY");
                database.AddFileGroup(fileGroup);

                SmoDataFile dataFile = new SmoDataFile(sqlAssemblies, fileGroup, DatabaseName);
                fileGroup.AddDataFile(dataFile);
                dataFile.SetFileName(server.GetMasterDbPath() + "\\" + DatabaseName + ".mdf");
                dataFile.SetSize(50.0 * 1024.0);
                dataFile.SetGrowthType("KB");
                dataFile.SetGrowth(1024.0);
                dataFile.SetIsPrimaryFile(true);

                SmoLogFile logFile = new SmoLogFile(sqlAssemblies, database, DatabaseName + "_Log");
                database.AddLogFile(logFile);
                logFile.SetFileName(server.GetMasterDbPath() + "\\" + DatabaseName + "_Log.ldf");
                logFile.SetSize(164.0 * 1024.0);
                logFile.SetGrowthType("Percent");
                logFile.SetGrowth(10.0);

                database.Create();

                fileGroup = database.GetFileGroup("PRIMARY");
                fileGroup.SetIsDefault(true);
                fileGroup.Alter();
                database.Alter();

                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();

                    if (!string.IsNullOrEmpty(WebApplicationPoolAccount))
                    {
                        if (!IsWebApplicationPoolAccountDbo)
                        {
                            EmbeddedSqlScript createWebUserScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateUser.sql");
                            createWebUserScript.AddToken("[ACCOUNT_NAME]", WebApplicationPoolAccount);
                            createWebUserScript.ExecuteNonQuery(connection);

                            EmbeddedSqlScript grantWebUserRoleScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.GrantUserRole.sql");
                            grantWebUserRoleScript.AddToken("[ACCOUNT_NAME]", WebApplicationPoolAccount);
                            grantWebUserRoleScript.AddToken("[ROLE]", "db_owner");
                            grantWebUserRoleScript.ExecuteNonQuery(connection);
                        }
                    }
                    else
                    {
                        callingCmdlet.WriteWarning("There was no web application pool account provided. As we are skipping this step, the web application pool account will need to be manually given db_owner rights to this Glyma Security DB.");
                    }

                    if (!string.IsNullOrEmpty(GlymaServiceApplicationPoolAccount))
                    {
                        if (!IsGlymaServiceApplicationPoolAccountDbo)
                        {
                            EmbeddedSqlScript createGlymaServiceUserScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateUser.sql");
                            createGlymaServiceUserScript.AddToken("[ACCOUNT_NAME]", GlymaServiceApplicationPoolAccount);
                            createGlymaServiceUserScript.ExecuteNonQuery(connection);

                            EmbeddedSqlScript grantGlymaServiceUserRoleScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.GrantUserRole.sql");
                            grantGlymaServiceUserRoleScript.AddToken("[ACCOUNT_NAME]", GlymaServiceApplicationPoolAccount);
                            grantGlymaServiceUserRoleScript.AddToken("[ROLE]", "db_owner");
                            grantGlymaServiceUserRoleScript.ExecuteNonQuery(connection);
                        }
                    }
                    else
                    {
                        callingCmdlet.WriteWarning("There was no Glyma service application pool account provided. As we are skipping this step, the Glyma application pool account will need to be manually given db_owner rights to this Glyma Security DB.");
                    }

                    EmbeddedSqlScript createTablesAndConstraintsScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateTablesAndConstraints.sql");
                    createTablesAndConstraintsScript.ExecuteNonQuery(connection);

                    EmbeddedSqlScript createGetAllowedGroupsSP = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateGetAllowedGroupsSP.sql");
                    createGetAllowedGroupsSP.ExecuteNonQuery(connection);

                    EmbeddedSqlScript createGetSiteCollectionAssociationsSP = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateGetSiteCollectionAssociationsSP.sql");
                    createGetSiteCollectionAssociationsSP.ExecuteNonQuery(connection);

                    if (!string.IsNullOrEmpty(SearchCrawlAccount))
                    {
                        if (!IsSearchCrawlAccountDbo)
                        {
                            EmbeddedSqlScript createSearchUserScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateUser.sql");
                            createSearchUserScript.AddToken("[ACCOUNT_NAME]", SearchCrawlAccount);
                            createSearchUserScript.ExecuteNonQuery(connection);

                            EmbeddedSqlScript grantSearchPermissionsScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.GrantSearchPermissions.sql");
                            grantSearchPermissionsScript.AddToken("[ACCOUNT_NAME]", SearchCrawlAccount);
                            grantSearchPermissionsScript.ExecuteNonQuery(connection);
                        }
                    }
                    else
                    {
                        callingCmdlet.WriteWarning("There was no search crawl account provided. As we are skipping this step, the Glyma search crawl account will need to be manually granted execute permissions on the GetAllowedGroups stored proecedure.");
                    }


                    connection.Close();
                }
            }
            catch (TargetInvocationException tiex)
            {
                //If it was a Target Invocation Exception find the last InnerException and throw it so the user gets a useful message to troubleshoot.
                Exception ex = Util.FindLastException(tiex);
                throw ex;
            }
        }
Пример #4
0
        public void ExecuteCmdletBase(PSCmdlet callingCmdlet)
        {
            SqlAssemblies sqlAssemblies = new SqlAssemblies();

            if (!sqlAssemblies.LoadedSuccessfully)
            {
                callingCmdlet.WriteWarning(sqlAssemblies.ErrorMessage);
                return;
            }

            SmoServer server = new SmoServer(sqlAssemblies, DatabaseServer);

            server.SetApplicationName("GlymaNewMapSecurityDbInstaller");

            SmoDatabase database = new SmoDatabase(sqlAssemblies, server, DatabaseName);

            SmoFileGroup fileGroup = new SmoFileGroup(sqlAssemblies, database, "PRIMARY");

            database.AddFileGroup(fileGroup);

            SmoDataFile dataFile = new SmoDataFile(sqlAssemblies, fileGroup, DatabaseName);

            fileGroup.AddDataFile(dataFile);
            dataFile.SetFileName(server.GetMasterDbPath() + "\\" + DatabaseName + ".mdf");
            dataFile.SetSize(50.0 * 1024.0);
            dataFile.SetGrowthType("KB");
            dataFile.SetGrowth(1024.0);
            dataFile.SetIsPrimaryFile(true);

            SmoLogFile logFile = new SmoLogFile(sqlAssemblies, database, DatabaseName + "_Log");

            database.AddLogFile(logFile);
            logFile.SetFileName(server.GetMasterDbPath() + "\\" + DatabaseName + "_Log.ldf");
            logFile.SetSize(164.0 * 1024.0);
            logFile.SetGrowthType("Percent");
            logFile.SetGrowth(10.0);

            database.Create();

            fileGroup = database.GetFileGroup("PRIMARY");
            fileGroup.SetIsDefault(true);
            fileGroup.Alter();
            database.Alter();

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                EmbeddedSqlScript createUserScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateUser.sql");
                createUserScript.AddToken("[ACCOUNT_NAME]", ApplicationPoolAccount);
                createUserScript.ExecuteNonQuery(connection);

                EmbeddedSqlScript grantUserRoleScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.GrantUserRole.sql");
                grantUserRoleScript.AddToken("[ACCOUNT_NAME]", ApplicationPoolAccount);
                grantUserRoleScript.AddToken("[ROLE]", "db_owner");
                grantUserRoleScript.ExecuteNonQuery(connection);

                // TODO Create security database.

                EmbeddedSqlScript createGetRootMapsSPScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateGetRootMapsSP.sql");
                createGetRootMapsSPScript.ExecuteNonQuery(connection);

                EmbeddedSqlScript createSearchUserScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.CreateUser.sql");
                createSearchUserScript.AddToken("[ACCOUNT_NAME]", SearchCrawlAccount);
                createSearchUserScript.ExecuteNonQuery(connection);

                EmbeddedSqlScript grantSearchPermissionsScript = new EmbeddedSqlScript("Glyma.Powershell.Resources.New_GLMapSecurityDB.GrantSearchPermissions.sql");
                grantSearchPermissionsScript.AddToken("[ACCOUNT_NAME]", SearchCrawlAccount);
                grantSearchPermissionsScript.ExecuteNonQuery(connection);

                connection.Close();
            }
        }