Stephanevg / HostsFileManagement

Hosts file management on Windows systems using PowerShell classes
27 stars 8 forks source link

Make module easier to use: Add powershell Functions #4

Open Stephanevg opened 6 years ago

Stephanevg commented 6 years ago

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:

Please fork, and create an individual branch per function.

Get-HFMHostsFile

Get-HFMHostsFileContent

New-HFMHostsFileEntry

Set-HFMHostsFileEntry

Save-HFMHostFileEntry

New-HFMHostsFileBackup

LxLeChat commented 6 years ago

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

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

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 ?

Stephanevg commented 6 years ago

Yeah, I love it! Shoot a PR!

LxLeChat commented 6 years ago

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

Stephanevg commented 6 years ago

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

LxLeChat commented 6 years ago

Working on Save-HFMHostFileEntry and New-HFMHostsFileBackup

Stephanevg commented 6 years ago

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

LxLeChat commented 6 years ago

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 ?

Stephanevg commented 6 years ago

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.

LxLeChat commented 6 years ago

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 :)