public int Apply(int templateId)
        {
            var    errorCount = 0;
            string basePath   = Settings.MunkiBasePath + Path.DirectorySeparatorChar + "manifests" + Path.DirectorySeparatorChar;

            var groups = BLL.GroupMunki.GetGroupsForManifestTemplate(templateId);

            if (Settings.MunkiPathType == "Local")
            {
                foreach (var munkiGroup in groups)
                {
                    var effectiveManifest = new BLL.Workflows.EffectiveMunkiTemplate().Group(munkiGroup.GroupId);
                    var computersInGroup  = BLL.Group.GetGroupMembers(munkiGroup.GroupId);
                    foreach (var computer in computersInGroup)
                    {
                        if (!WritePath(basePath + computer.Name, Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                        {
                            errorCount++;
                        }
                    }
                }
            }
            else
            {
                using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
                {
                    var smbPassword = new Helpers.Encryption().DecryptText(Settings.MunkiSMBPassword);
                    var smbDomain   = string.IsNullOrEmpty(Settings.MunkiSMBDomain) ? "" : Settings.MunkiSMBDomain;
                    if (unc.NetUseWithCredentials(Settings.MunkiBasePath, Settings.MunkiSMBUsername, smbDomain, smbPassword) || unc.LastError == 1219)
                    {
                        foreach (var munkiGroup in groups)
                        {
                            var effectiveManifest = new BLL.Workflows.EffectiveMunkiTemplate().Group(munkiGroup.GroupId);
                            var computersInGroup  = BLL.Group.GetGroupMembers(munkiGroup.GroupId);
                            foreach (var computer in computersInGroup)
                            {
                                if (!WritePath(basePath + computer.Name, Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                                {
                                    errorCount++;
                                }
                            }
                        }
                    }
                    else
                    {
                        Logger.Log("Failed to connect to " + Settings.MunkiBasePath + "\r\nLastError = " + unc.LastError);
                        foreach (var munkiGroup in groups)
                        {
                            var computersInGroup = BLL.Group.GetGroupMembers(munkiGroup.GroupId);
                            errorCount += computersInGroup.Count();
                        }
                    }
                }
            }
            var computers = BLL.ComputerMunki.GetComputersForManifestTemplate(templateId);

            if (Settings.MunkiPathType == "Local")
            {
                foreach (var munkiComputer in computers)
                {
                    var effectiveManifest = new BLL.Workflows.EffectiveMunkiTemplate().Computer(munkiComputer.ComputerId);
                    var computer          = BLL.Computer.GetComputer(munkiComputer.ComputerId);
                    if (!WritePath(basePath + computer.Name, Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                    {
                        errorCount++;
                    }
                }
            }
            else
            {
                using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
                {
                    var smbPassword = new Helpers.Encryption().DecryptText(Settings.MunkiSMBPassword);
                    var smbDomain   = string.IsNullOrEmpty(Settings.MunkiSMBDomain) ? "" : Settings.MunkiSMBDomain;
                    if (
                        unc.NetUseWithCredentials(Settings.MunkiBasePath, Settings.MunkiSMBUsername, smbDomain,
                                                  smbPassword) || unc.LastError == 1219)
                    {
                        foreach (var munkiComputer in computers)
                        {
                            var effectiveManifest =
                                new BLL.Workflows.EffectiveMunkiTemplate().Computer(munkiComputer.ComputerId);
                            var computer = BLL.Computer.GetComputer(munkiComputer.ComputerId);


                            if (
                                !WritePath(basePath + computer.Name,
                                           Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                            {
                                errorCount++;
                            }
                        }
                    }
                    else
                    {
                        Logger.Log("Failed to connect to " + Settings.MunkiBasePath + "\r\nLastError = " +
                                   unc.LastError);
                        errorCount += computers.Count();
                    }
                }
            }

            if (errorCount > 0)
            {
                return(errorCount);
            }

            var includedTemplates = new List <Models.MunkiManifestTemplate>();

            foreach (var munkiGroup in groups)
            {
                foreach (var template in BLL.GroupMunki.Get(munkiGroup.GroupId))
                {
                    includedTemplates.Add(BLL.MunkiManifestTemplate.GetManifest(template.MunkiTemplateId));
                }
            }

            foreach (var computer in computers)
            {
                foreach (var template in BLL.ComputerMunki.Get(computer.ComputerId))
                {
                    includedTemplates.Add(BLL.MunkiManifestTemplate.GetManifest(template.MunkiTemplateId));
                }
            }

            foreach (var template in includedTemplates)
            {
                template.ChangesApplied = 1;
                BLL.MunkiManifestTemplate.UpdateManifest(template);
            }

            return(0);
        }
Пример #2
0
        public int Apply(int templateId)
        {
            var errorCount = 0;
            string basePath = Settings.MunkiBasePath + Path.DirectorySeparatorChar + "manifests" + Path.DirectorySeparatorChar;

            var groups = BLL.GroupMunki.GetGroupsForManifestTemplate(templateId);
            if (Settings.MunkiPathType == "Local")
            {
                foreach (var munkiGroup in groups)
                {
                    var effectiveManifest = new BLL.Workflows.EffectiveMunkiTemplate().Group(munkiGroup.GroupId);
                    var computersInGroup = BLL.Group.GetGroupMembers(munkiGroup.GroupId);
                    foreach (var computer in computersInGroup)
                    {
                        if (!WritePath(basePath + computer.Name, Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                            errorCount++;
                    }
                }
            }
            else
            {
                using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
                {
                    var smbPassword = new Helpers.Encryption().DecryptText(Settings.MunkiSMBPassword);
                    var smbDomain = string.IsNullOrEmpty(Settings.MunkiSMBDomain) ? "" : Settings.MunkiSMBDomain;
                    if (unc.NetUseWithCredentials(Settings.MunkiBasePath, Settings.MunkiSMBUsername, smbDomain, smbPassword) || unc.LastError == 1219)
                    {
                        foreach (var munkiGroup in groups)
                        {
                            var effectiveManifest = new BLL.Workflows.EffectiveMunkiTemplate().Group(munkiGroup.GroupId);
                            var computersInGroup = BLL.Group.GetGroupMembers(munkiGroup.GroupId);
                            foreach (var computer in computersInGroup)
                            {
                                if (!WritePath(basePath + computer.Name, Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                                    errorCount++;
                            }
                        }
                    }
                    else
                    {
                        Logger.Log("Failed to connect to " + Settings.MunkiBasePath + "\r\nLastError = " + unc.LastError);
                        foreach (var munkiGroup in groups)
                        {
                            var computersInGroup = BLL.Group.GetGroupMembers(munkiGroup.GroupId);
                            errorCount += computersInGroup.Count();
                        }
                    }
                }
            }
            var computers = BLL.ComputerMunki.GetComputersForManifestTemplate(templateId);
            if (Settings.MunkiPathType == "Local")
            {
                foreach (var munkiComputer in computers)
                {
                    var effectiveManifest = new BLL.Workflows.EffectiveMunkiTemplate().Computer(munkiComputer.ComputerId);
                    var computer = BLL.Computer.GetComputer(munkiComputer.ComputerId);
                    if (!WritePath(basePath + computer.Name, Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                        errorCount++;
                }
            }
            else
            {
                using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
                {
                    var smbPassword = new Helpers.Encryption().DecryptText(Settings.MunkiSMBPassword);
                    var smbDomain = string.IsNullOrEmpty(Settings.MunkiSMBDomain) ? "" : Settings.MunkiSMBDomain;
                    if (
                        unc.NetUseWithCredentials(Settings.MunkiBasePath, Settings.MunkiSMBUsername, smbDomain,
                            smbPassword) || unc.LastError == 1219)
                    {
                        foreach (var munkiComputer in computers)
                        {
                            var effectiveManifest =
                                new BLL.Workflows.EffectiveMunkiTemplate().Computer(munkiComputer.ComputerId);
                            var computer = BLL.Computer.GetComputer(munkiComputer.ComputerId);

                            if (
                                !WritePath(basePath + computer.Name,
                                    Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                                errorCount++;
                        }
                    }
                    else
                    {
                        Logger.Log("Failed to connect to " + Settings.MunkiBasePath + "\r\nLastError = " +
                                   unc.LastError);
                        errorCount += computers.Count();
                    }
                }
            }

            if (errorCount > 0)
                return errorCount;

            var includedTemplates = new List<Models.MunkiManifestTemplate>();
            foreach (var munkiGroup in groups)
            {
                foreach (var template in BLL.GroupMunki.Get(munkiGroup.GroupId))
                {
                    includedTemplates.Add(BLL.MunkiManifestTemplate.GetManifest(template.MunkiTemplateId));
                }
            }

            foreach (var computer in computers)
            {
                foreach (var template in BLL.ComputerMunki.Get(computer.ComputerId))
                {
                    includedTemplates.Add(BLL.MunkiManifestTemplate.GetManifest(template.MunkiTemplateId));
                }
            }

            foreach (var template in includedTemplates)
            {
                template.ChangesApplied = 1;
                BLL.MunkiManifestTemplate.UpdateManifest(template);
            }

            return 0;
        }