protected override void BeginProcessing()
        {
            base.BeginProcessing();

            if (hostFile == string.Empty)
            {
                hostFile = Path.Combine(Environment.SystemDirectory, "drivers\\etc\\hosts");
            }

            WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());

            if (!wp.IsInRole(WindowsBuiltInRole.Administrator))
            {
                ThrowTerminatingError(new ErrorRecord(new UnauthorizedAccessException("You must have Administrative permissions to run this command"), "100", ErrorCategory.PermissionDenied, null));
            }
            else
            {
                HostEntryCollection entryCollection = HostFileHandler.GetHostEntryColletion(hostFile);

                if (removeIP == string.Empty && removeHostname == string.Empty)
                {
                    ThrowTerminatingError(new ErrorRecord(new ArgumentException("You must supply either IP or host to remove"), "200", ErrorCategory.InvalidArgument, null));
                }

                if (removeIP != string.Empty)
                {
                    IPAddress removeAddress = System.Net.IPAddress.Parse(removeIP);
                    HostEntry hostEntry     = entryCollection[removeAddress];
                    if (hostEntry != null)
                    {
                        entryCollection.Remove(hostEntry);
                    }
                }

                if (removeHostname != string.Empty)
                {
                    HostEntry hostEntry = entryCollection[removeHostname];
                    if (hostEntry != null)
                    {
                        hostEntry.RemoveHostname(removeHostname);
                    }

                    if (hostEntry.Count == 0)
                    {
                        entryCollection.Remove(hostEntry);
                    }
                }

                HostFileHandler.PersistHostEntryCollection(hostFile, entryCollection);
            }
        }
        protected override void BeginProcessing()
        {
            base.BeginProcessing();

            if (hostFile == string.Empty)
            {
                hostFile = Path.Combine(Environment.SystemDirectory, "drivers\\etc\\hosts");
            }

            WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());

            if (!wp.IsInRole(WindowsBuiltInRole.Administrator))
            {
                ThrowTerminatingError(new ErrorRecord(new UnauthorizedAccessException("You must have Administrative permissions to run this command"), "100", ErrorCategory.PermissionDenied, null));
            }
            else
            {
                HostEntryCollection entryCollection = HostFileHandler.GetHostEntryColletion(hostFile);
                IPAddress           setAddress      = System.Net.IPAddress.Parse(setIP);

                foreach (string newHost in setHostnames)
                {
                    if (entryCollection[newHost] == null)
                    {
                        if (entryCollection[setAddress] == null)
                        {
                            HostEntry newEntry = new HostEntry();
                            newEntry.IPAddress = setAddress;
                            newEntry.AddHostname(newHost);
                            newEntry.Comment = comment;
                            entryCollection.Add(newEntry);
                        }
                        else
                        {
                            entryCollection[setAddress].AddHostname(newHost);
                        }
                    }
                    else
                    {
                        WriteError(new ErrorRecord(new InvalidDataException(string.Format("Host '{0}' already present in the host file and will not be added", newHost)), "200", ErrorCategory.InvalidData, null));
                    }
                }

                HostFileHandler.PersistHostEntryCollection(hostFile, entryCollection);
            }
        }
        public static void PersistHostEntryCollection(string hostFile, HostEntryCollection hosts)
        {
            StringBuilder result = new StringBuilder();

            foreach (HostEntry host in hosts)
            {
                string hostNames = string.Empty;
                foreach (string hostName in host.Hostnames)
                {
                    hostNames = string.Format("{0} {1}", hostNames, hostName);
                }

                string comment = (host.Comment == null) ? string.Empty : string.Format("#{0}", host.Comment.Trim());

                result.AppendFormat("{0} {1} {2}{3}", host.IPAddress.ToString(), hostNames.Trim(), comment, Environment.NewLine);
            }

            StreamWriter writer = new StreamWriter(hostFile, false);

            writer.Write(result.ToString());
            writer.Close();
        }
        public static HostEntryCollection GetHostEntryColletion(string hostFile)
        {
            HostEntryCollection result = new HostEntryCollection();

            if (hostFile == string.Empty)
            {
                hostFile = Path.Combine(Environment.SystemDirectory, "drivers\\etc\\hosts");
            }

            string[] fileContent = File.ReadAllLines(hostFile);

            foreach (string fileRow in fileContent)
            {
                string dataToProcess = fileRow.Trim();

                if (!dataToProcess.StartsWith("#"))
                {
                    if (dataToProcess.Contains(' '))
                    {
                        string[] fileValues = dataToProcess.Split(new char[] { ' ' }, 2);

                        // Get the IPAddress
                        IPAddress ipAddresser = IPAddress.Parse(fileValues[0].Trim());

                        HostEntry hostEntry = result[ipAddresser];
                        if (hostEntry == null)
                        {
                            hostEntry           = new HostEntry();
                            hostEntry.IPAddress = ipAddresser;
                            result.Add(hostEntry);
                        }

                        // Get the Comment & hostnames
                        string[] commentValue   = fileValues[1].Split('#');
                        string   hostnameString = string.Empty;
                        if (commentValue.Length > 1)
                        {
                            string commentString = commentValue[1].Trim();
                            if (commentString.Length > 0)
                            {
                                hostEntry.Comment = (commentValue[1] + " " + hostEntry.Comment).Trim();
                                hostnameString    = fileValues[1].Replace(commentValue[1], "").Replace("#", "").Trim();
                            }
                            else
                            {
                                hostnameString = fileValues[1].Replace("#", "").Trim();
                            }
                        }
                        else
                        {
                            hostnameString = fileValues[1].Trim();
                        }

                        List <string> hostNames = new List <string>();
                        string[]      fileHosts = hostnameString.Split(' ');

                        foreach (string fileHost in fileHosts)
                        {
                            string addFile = fileHost.Trim();
                            if (addFile != null && addFile != string.Empty)
                            {
                                if (!hostEntry.ContainsHostname(addFile))
                                {
                                    hostEntry.AddHostname(addFile);
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }