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); } }