Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make module easier to use: Add powershell Functions #4

Open
9 of 21 tasks
Stephanevg opened this issue Oct 5, 2018 · 9 comments
Open
9 of 21 tasks

Make module easier to use: Add powershell Functions #4

Stephanevg opened this issue Oct 5, 2018 · 9 comments

Comments

@Stephanevg
Copy link
Owner

Stephanevg commented Oct 5, 2018

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

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

Get-HFMHostsFileContent

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

New-HFMHostsFileEntry

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

Set-HFMHostsFileEntry

  • Set-HFMHostsFileEntry
  • 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

  • Remove-HFHMHostsFileEntry
  • Remove-HFHMHostsFileEntry Examples
  • Remove-HFHMHostsFileEntry Tests
@LxLeChat
Copy link
Collaborator

LxLeChat commented Oct 8, 2018

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 ?

@Stephanevg
Copy link
Owner Author

Stephanevg commented Oct 8, 2018

Yeah, I love it!
Shoot a PR!

@LxLeChat
Copy link
Collaborator

LxLeChat commented Oct 9, 2018

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

@Stephanevg Stephanevg mentioned this issue Oct 9, 2018
4 tasks
@Stephanevg
Copy link
Owner Author

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

@LxLeChat
Copy link
Collaborator

LxLeChat commented Oct 9, 2018

Working on Save-HFMHostFileEntry and New-HFMHostsFileBackup

@Stephanevg
Copy link
Owner Author

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

@LxLeChat
Copy link
Collaborator

LxLeChat commented Oct 9, 2018

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
Copy link
Owner Author

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
Copy link
Collaborator

LxLeChat commented Oct 11, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants