HostsFileManagement icon indicating copy to clipboard operation
HostsFileManagement copied to clipboard

Make module easier to use: Add powershell Functions

Open Stephanevg opened this issue 7 years ago • 9 comments

This module is classes based. With experience, I noticed that end users don't really embrace using classes that much, and are more willing to work with functions / cmdlets, thing they are used too since a few years already.

In order to make this module easier for the public embrace, and to abstract the complexity for the end users, and add an encapsulation layer, I would like to add the following cmdlets / functions (See below)

Technical implementation details:

Each cmdlet should have the following:

  • Comment based help
  • At least 2 examples.
  • A unit test.

Please fork, and create an individual branch per function.

Get-HFMHostsFile

  • [x] Get-HFMHostsfile -> Get the Hostsfilecontent (Remotley or locally).
  • [x] Get-HFMHostsfile Examples
  • [ ] Get-HFMHostsfile Pester Tests

Get-HFMHostsFileContent

  • [x] Get-HFMHostsFileContent -> Reads the HostsFileContent Received from Get-HFMHostsFile
  • [x] Get-HFMHostsFileContent Examples
  • [ ] Get-HFMHostsFileContent Pester Tests

New-HFMHostsFileEntry

  • [x] New-HFMHostsFileEntry -> Creates an HostFileEntry object (Of type [HostsEntry])
    • Parameters: -Type (Must be of type [HostsEntryType])
  • [x] New-HFMHostsFileEntry Examples
  • [ ] New-HFMHostsFileEntry PesterTests

Set-HFMHostsFileEntry

  • [x] Set-HFMHostsFileEntry
  • [x] Set-HFMHostsFileEntry Examples
  • [ ] Set-HFMHostsFileEntry Pester Tests

Save-HFMHostFileEntry

  • [ ] Save-HFMHostFileEntry
  • [ ] Save-HFMHostFileEntry Examples
  • [ ] Save-HFMHostFileEntryPester Tests

New-HFMHostsFileBackup

  • [ ] New-HFMHostsFileBackup
  • [ ] New-HFMHostsFileBackup Examples
  • [ ] New-HFMHostsFileBackup Tests

Remove-HFHMHostsFileEntry

  • [x] Remove-HFHMHostsFileEntry
  • [ ] Remove-HFHMHostsFileEntry Examples
  • [ ] Remove-HFHMHostsFileEntry Tests

Stephanevg avatar Oct 05 '18 10:10 Stephanevg

Hi, i worked on 2 basic functions Get-HFMFHostsFile and Get-HFHMHostsFileContent

  • Get-HFMHostsfile

You specify a computername, default behavior uses localhost, to create and return a new instance of [HostsFile]

  • Get-HFMHostsFileContent Accept only a Path parameter of type [HostsFile], return an array of [HostsEntry]

Example 1: $a = Get-HFMHostsfile -ComputerName Localhost Get-HFMHostsFileContent -Path $a

Example 2: $a = Get-HFMHostsfile Get-HFMHostsFileContent $a

Example 3 Get-HFMHostsfile | Get-HFMHostsFileContent

Example 4 "localhost" | Get-HFMHostsfile | Get-HFMHostsFileContent`

What do you think ?

LxLeChat avatar Oct 08 '18 20:10 LxLeChat

Yeah, I love it! Shoot a PR!

Stephanevg avatar Oct 08 '18 21:10 Stephanevg

PR for the 2 functions Working on New-HFMHostsFileEntry & Set-HFMHostsFileEntry

LxLeChat avatar Oct 09 '18 08:10 LxLeChat

Updated the Ticket with infos from PR #5 of @LxLeChat

Stephanevg avatar Oct 09 '18 12:10 Stephanevg

Working on Save-HFMHostFileEntry and New-HFMHostsFileBackup

LxLeChat avatar Oct 09 '18 13:10 LxLeChat

Updated issue with pr #12 from @LxLeChat . Looking forward to the next ones @LxLeChat 👍

Stephanevg avatar Oct 09 '18 14:10 Stephanevg

I implemented the save-hfmhostsfiles code can be found here for tests: Save-HFMHostsFile But i think i found a small quirk in the class

Get-HFMostsFile | Save-HFMHostsFile This works great and as a maxium of 6 (?) backup in the default folder..

Get-HFMHostsfile | Save-HFMHostsFile -BackupFolder c:\temp -MaxLogRotation 3 it works too, but the logrotation is not working...

this is how i implemented it If ( $MaxLogRotation ) { $HostPath.LogRotation = $MaxLogRotation } $hostspath is a [HostsFile] type of the current hostsFile

Should i PR our do i check ths class directly ?

LxLeChat avatar Oct 09 '18 21:10 LxLeChat

Hi, this behaviour is normal, actually. I have writen a fail safe mechanism to avoid to have 100000 backups. It will keep the 5 latest ones.

Class HostsFile {
    hidden [HostsEntry[]]$Entries
    [string]$Path
    hidden [int]$LogRotation = 5

I see you found the property. As a best practise when working with classes, you should never set a property directy, but always use a method.

I would suggest you add the following method to the class [HostsFile]

SetLogRotation($MaxLogRotation) It should return void, and will actually contain that line tha you have above. In the function call, you should simply call this method instead of your above example.

This will allow us, to be able to change the implementation od SetLogRotation easier.

Stephanevg avatar Oct 09 '18 21:10 Stephanevg

I think we need a Remove-HFMHostsFileEntry

Maybe the Removing process should work like this :

$a = Get-HFMHostsFile
$b = Get-HFMHostsFileContent
Remove-HFHMHostsFileEntry -Entries $b[0..5] #remove 6 first entries

And maybe me can add a switch that removes all comments... ! pretty useless, but whatever :)

LxLeChat avatar Oct 11 '18 13:10 LxLeChat