Open lazywinadmin opened 5 years ago
My personnal, very personnal opinion : i prefer ps3+ modules.
example of PS Class for log (provided by @lxlechat
Class Log {
$LogPath = $null
Log ([String]$a){
if ( !(test-path $a) ) {
New-Item -Path $a -ItemType file
$this.LogPath = $a
} else {
$this.LogPath = $a
}
}
Log ([String]$a,[Bool]$FileTimeStamp){
$leaf = $(get-date -Format "ddMMyyyy_hhmmss") + "_" + $(split-path $a -Leaf)
$path = Join-Path -Path (split-path $a) -ChildPath $leaf
if ( !(test-path $path) ) {
New-Item -Path $path -ItemType file
$this.LogPath = $path
} else {
$this.LogPath = $path
}
}
Log () {}
[void] WriteMessage ([String]$m, [Bool]$n){
If ( $this.LogPath -ne $null ) {
If ( $n ) {
$(get-date -Format "dd/MM/yyyy hh:mm:ss") + ' - ' + $m >> $this.LogPath
} else {
$m >> $this.LogPath
}
}
}
}
$Log = [Log]::New($LogPath)
$Log.writeMessage("Start Script...",$True)
$true represent the timestamp at the beginning of the line
I would like to use classes just to learn classes and for maintainability. But I understand that if we want to have retrocompatibility we should avoid classes
Classes add flexibility. For example, you instantiate a new log file (wich contains properties and methods) then you can simply call the methods you like on this log file. Plus you can wrap a ps function to create your log instance, so it's more accessible for common users.
downside like said before: ps version.
I think it would be interesting to look at classes too. PS 5 was released in 2015/2016... I think we can take the risk of only supporting PS5+
non mais les gars, bous en managez bcp des servers encore qui ont pas plus haut que ps3?
Et franchement si c'est le cas, y a pas un pb de lifecycle quelque part? powershell 7 est quasi la, donc on fais pas de chichi, on arrrete de ce trouver des excuses, et on ce met le prerequisite de ps5.1 Et on commence avec les classes !! ;)
et donc pr repondre finalement a la question de l'issue: Oui, utiliser des classes fera bcp plus de sense car on pourra bcp plus facilement designer / discuter / documenter le module.
De plus, c'est l'occasion idéale pour apprendre / approfondir les classes pr la communauté Française.
Pr moi, la version ps est un faux prétexte, et n'est pas une raison valide pour écrire du 'légacy' code.
Cons: Won't work prior to PS5 👎