Ejemplo n.º 1
0
        private RIPMessage PrepareEvent(string sender, string receiver, int reqId, int responseId, RequestType type, SerializableDictionary <int, string> data)
        {
            RIPMessage e = new RIPMessage(new object());

            RequestMessage msg = new RequestMessage()
            {
                SenderID      = sender,
                ReceiverID    = receiver,
                TimeOfSending = DateTime.Now,
                RequestID     = reqId,
                RequestType   = type,
                DataParams    = data,
            };

            //msg.DataParams.Add(1, "dddd");
            string xmlString = GenericDataContractSerializer <RequestMessage> .SerializeObject(msg);

            e.Request = xmlString;
            return(e);
        }
        public void ExecuteWMIForOneApp(CheckLicenseModel chc, string username, string password, int opreationidi, List <ControlPointDTO> checklist, bool isRemote)
        {
            var levelid   = 0;
            var softwares = "";
            var software  = chc.App;
            var guids     = Guid.NewGuid();
            var guidId    = guids.ToString();
            var logs      = new List <LogData>();
            var remote    = String.IsNullOrEmpty(chc.Name) ? chc.Ip : chc.Name;
            var xmlreq    = new RequestedXml {
                ComputerName = chc.Name, Ip = chc.Ip, IsRemote = isRemote.ToString(), Password = password, UserName = username, WorkDate = DateTime.Now.ToString("dd-MM-yyyy")
            };
            ProcessWMI process = new ProcessWMI(remote, username, password, isRemote);

            try
            {
                logs.Add(new LogData {
                    AppName = remote, Id = guidId, StartTime = DateTime.Now, Method = "ExecuteWMI", LogDataType = LogDataType.InitiliazeProcess, ComputerId = (int)chc.ComputerId
                });
                logs.Add(new LogData {
                    AppName = remote, ReqXml = GenericDataContractSerializer <RequestedXml> .SerializeObject(xmlreq), Id = guidId, LevelId = levelid, Method = "ExecuteWMI", StartTime = DateTime.Now, LogDataType = LogDataType.InitiliazeItemOfProcess, OperationId = (int)chc.OperationId
                });
                chc.LogId = guids;
                chc.InitiliazeObject();
                process.Connect();

                foreach (var item in checklist)
                {
                    if (item.Name == "Add or Remove Programs" && item.WillChecked == true)
                    {
                        var addremove = "";
                        try
                        {
                            levelid++;
                            logs.Add(new LogData {
                                ReqXml = GenericDataContractSerializer <ControlPointDTO> .SerializeObject(item), Id = guidId, LevelId = levelid, Method = "Add or Remove Programs", StartTime = DateTime.Now, LogDataType = LogDataType.InitiliazeItemOfProcess
                            });
                            addremove = "ADD OR REMOVE PROGRAMS" + "\n\r" + "\n\r";
                            var win32product = process.GetProductWithWMI(software);
                            outputs.Win32_products = win32product;
                            if (win32product.Count > 0)
                            {
                                foreach (var prd in win32product)
                                {
                                    addremove += prd.Name + " InstallDate : " + prd.InstallDate + ")" + "\n\r";
                                }
                                chc.Installed    = true;
                                chc.IsFound      = true;
                                chc.SerialNumber = win32product[0].ProductID;
                            }
                            else
                            {
                                addremove += "Programs are not found" + "\n\r";
                            }
                        }
                        catch (Exception ex)
                        {
                            addremove += "Fail : " + ex.Message + "\n\r";
                        }
                        softwares += addremove;
                        logs.Add(new LogData {
                            Id = guidId, ResXml = addremove, State = LogDataState.Success, LevelId = levelid, EndTime = DateTime.Now, LogDataType = LogDataType.UpdateItemOfProcess
                        });
                    }
                    if (item.Name == "Registry Key" && item.WillChecked == true)
                    {
                        var registerys = "";
                        try
                        {
                            levelid++;
                            logs.Add(new LogData {
                                ReqXml = GenericDataContractSerializer <ControlPointDTO> .SerializeObject(item), Id = guidId, LevelId = levelid, Method = "Registry Key", StartTime = DateTime.Now, LogDataType = LogDataType.InitiliazeItemOfProcess
                            });
                            registerys += "\n\r" + "REGISTERY KEY" + "\n\r" + "\n\r";

                            var registeryproduct = process.ReadRegisteryusingWMI(software);

                            outputs.RegistryAutoDesk = registeryproduct;
                            if (registeryproduct.Count > 0)
                            {
                                foreach (var reg in registeryproduct)
                                {
                                    registerys += reg.DisplayName + " InstallDate" + reg.InstallDate + ")" + "\n\r";
                                }
                                chc.Installed = true;
                                chc.IsFound   = true;
                            }
                            else
                            {
                                registerys += "Registry keys are not found." + "\n\r";
                            }
                        }
                        catch (Exception ex)
                        {
                            registerys += "Fail : " + ex.Message + "\n\r";
                        }
                        softwares += registerys;

                        logs.Add(new LogData {
                            Id = guidId, ResXml = registerys, State = LogDataState.Success, LevelId = levelid, EndTime = DateTime.Now, LogDataType = LogDataType.UpdateItemOfProcess
                        });
                    }
                    if (item.Name == "Application Events" && item.WillChecked == true)
                    {
                        levelid++;
                        var appevent = "";
                        logs.Add(new LogData {
                            ReqXml = GenericDataContractSerializer <ControlPointDTO> .SerializeObject(item), Id = guidId, LevelId = levelid, Method = "Application Events", StartTime = DateTime.Now, LogDataType = LogDataType.InitiliazeItemOfProcess
                        });
                        try
                        {
                            appevent += "APPLICATION EVENTS" + "\n\r" + "\n\r";

                            var events = process.GetApplicationEvent(software);

                            outputs.ApplicationEvents = events;
                            if (events.Count > 0)
                            {
                                foreach (var evt in events)
                                {
                                    appevent += evt.Message + "(" + evt.TimeWritten + ")" + "\n\r";
                                }
                                if (chc.Installed == false)
                                {
                                    chc.Uninstalled = true;
                                }
                                chc.IsFound = true;
                            }
                            else
                            {
                                appevent += "Application events are not found" + "\n\r";
                            }
                        }
                        catch (Exception ex)
                        {
                            appevent += "Fail : " + ex.Message + "\n\r";
                        }
                        softwares += appevent;
                        logs.Add(new LogData {
                            Id = guidId, ResXml = appevent, State = LogDataState.Success, LevelId = levelid, EndTime = DateTime.Now, LogDataType = LogDataType.UpdateItemOfProcess
                        });
                    }
                    if (item.Name == "File Explorer" && item.WillChecked == true)
                    {
                        var fileexplorer = "";
                        levelid++;
                        logs.Add(new LogData {
                            ReqXml = GenericDataContractSerializer <ControlPointDTO> .SerializeObject(item), Id = guidId, LevelId = levelid, Method = "File Explorer", StartTime = DateTime.Now, LogDataType = LogDataType.InitiliazeItemOfProcess
                        });
                        try
                        {
                            fileexplorer += "FILE EXPLORER" + "\n\r" + "\n\r" + "\n\r";

                            var model = process.DirectoryChecklist(client.GetFEControlList(software.Id).ToList());


                            if (model.directories.Count > 0 || model.files.Count > 0)
                            {
                                foreach (var dir in model.directories)
                                {
                                    fileexplorer += dir.Name + " InstallDate :  (" + dir.InstallDate + ")" + "\n\r";
                                }
                                foreach (var dir in model.files)
                                {
                                    fileexplorer += dir.Name + "  InstallDate : (" + dir.InstallDate + ")" + "\n\r";
                                }
                                outputs.FileExplorerModel = model;
                                if (chc.Installed == false)
                                {
                                    chc.Uninstalled = true;
                                }
                                chc.IsFound = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            fileexplorer += "Fail : " + ex.Message + "\n\r";
                        }
                        softwares += fileexplorer;
                        logs.Add(new LogData {
                            Id = guidId, ResXml = fileexplorer, State = LogDataState.Success, LevelId = levelid, EndTime = DateTime.Now, LogDataType = LogDataType.UpdateItemOfProcess
                        });
                    }
                }

                chc.Success = true;

                if (outputs.Win32_products != null && outputs.Win32_products.Count > 0)
                {
                    try
                    {
                        chc.InstallDate = outputs.Win32_products.GroupBy(s => s.ProductID).Select(s => s.OrderByDescending(x => x.InstallDate).FirstOrDefault().InstallDate).First();
                        chc.Installed   = true;
                        chc.Uninstalled = false;
                        chc.Description = "Install Date is found from add or remove programs...";
                    }
                    catch (Exception ex)
                    {
                        chc.Output += "Fail When Get Install Date from Win32_products  :" + ex.Message + "\n\r";
                    }
                }
                else if (outputs.RegistryAutoDesk != null && outputs.RegistryAutoDesk.Count > 0)
                {
                    try
                    {
                        chc.InstallDate = outputs.RegistryAutoDesk.Where(x => x.InstallDate != null).GroupBy(s => s.DisplayName).Select(s => s.OrderByDescending(x => x.InstallDate).FirstOrDefault().InstallDate).First();
                        chc.Installed   = true;
                        chc.Uninstalled = false;
                        chc.Description = "Install Date is found from registery keys...";
                    }
                    catch (Exception ex)
                    {
                        chc.Output += "Fail When Get Install Date from RegistryAutoDesk  :" + ex.Message + "\n\r";
                    }
                }
                else if (outputs.ApplicationEvents != null && outputs.ApplicationEvents.Count > 0)
                {
                    try
                    {
                        chc.UnInstallDate = outputs.ApplicationEvents.GroupBy(s => s.ComputerName).Select(s => s.OrderByDescending(x => x.TimeWritten).FirstOrDefault().TimeWritten).First();
                        chc.Installed     = false;
                        chc.Uninstalled   = true;
                        chc.Description   = "UnInstall Date is found from Application Events...";
                    }
                    catch (Exception ex)
                    {
                        chc.Output += "Fail When Get Install Date from ApplicationEvents :" + ex.Message + "\n\r";
                    }
                }
                else if (outputs.FileExplorerModel != null && outputs.FileExplorerModel.directories.Count > 0)
                {
                    try
                    {
                        chc.UnInstallDate = outputs.FileExplorerModel.directories.GroupBy(s => s.Name).Select(s => s.OrderByDescending(x => x.LastAccessed).FirstOrDefault().LastAccessed).First();
                        chc.Installed     = false;
                        chc.Uninstalled   = true;
                        chc.Description   = "UnInstall Date is found from File Explorer";
                    }
                    catch (Exception ex)
                    {
                        chc.Output += "Fail When Get Install Date from FileExplorerModel :" + ex.Message + "\n\r";
                    }
                }
                chc.Success = true;
                chc.Output  = softwares;
            }
            catch (Exception ex)
            {
                softwares  += "\r\n" + ex.Message;
                chc.Fail    = true;
                chc.Success = false;
                chc.Output  = softwares;
                logs.Add(new LogData {
                    AppName = remote, Id = guidId, ResXml = chc.Output, State = LogDataState.Success, LevelId = 0, EndTime = DateTime.Now, LogDataType = LogDataType.UpdateItemOfProcess
                });
                logs.Add(new LogData()
                {
                    AppName = remote, Id = guidId, EndTime = DateTime.Now, LogDataType = LogDataType.UpdateProcess
                });
                try
                {
                    client.LogToDb(logs.ToArray());
                }
                catch (Exception e)
                {
                    var message = e.Message;
                }

                chc.QueryState = CategorizeProcessResult(chc.Output, chc.App.AppName);
                chc.State      = (int)chc.QueryState;

                return;
            }
            try
            {
                logs.Add(new LogData {
                    AppName = remote, Id = guidId, ResXml = chc.Output, State = LogDataState.Success, LevelId = 0, EndTime = DateTime.Now, LogDataType = LogDataType.UpdateItemOfProcess
                });
                logs.Add(new LogData()
                {
                    AppName = remote, Id = guidId, EndTime = DateTime.Now, LogDataType = LogDataType.UpdateProcess
                });
                client.LogToDb(logs.ToArray());
                chc.QueryState = CategorizeProcessResult(chc.Output, chc.App.AppName);
                chc.State      = (int)chc.QueryState;
            }
            catch (Exception ex)
            {
                var message = ex.Message;
            }
            return;
        }