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);
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
 protected override void BeginProcessing()
 {
     WriteObject(HostFileHandler.GetHostEntryColletion(hostFile));
 }