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

PSFramework Quick-Wins #122

Closed
FriedrichWeinmann opened this issue Oct 6, 2024 · 1 comment
Closed

PSFramework Quick-Wins #122

FriedrichWeinmann opened this issue Oct 6, 2024 · 1 comment

Comments

@FriedrichWeinmann
Copy link

Heya, thanks again for the help on PSModuleDevelopment.
I must admit, I got a bit curious on about what you were using it for and found this rather interesting project here :)

Wow, you spent quite some time on all this 👍

That said, I've found a few things where I thought "Huh, a few pointers might make it more convenient for you", so ... while there's nothing wrong, really, I found a few things I wanted to show how PSFramework can make your life a bit easier :)

Type Conversion and Select-PSFObject

Example Location)

Select-PSFObject can also do inline type-conversion.
This ...

Select-PSFObject -TypeName D365FO.TOOLS.Azure.Blob "name", @{Name = "Size"; Expression = { [PSFSize]$_.Length } }, @{Name = "LastModified"; Expression = { [Datetime]::Parse($_.LastModified) } }

... could also be that:

Select-PSFObject -TypeName D365FO.TOOLS.Azure.Blob "name", 'Length as Size to PSFSize', @{Name = "LastModified"; Expression = { [Datetime]::Parse($_.LastModified) } }

And possibly even this

Select-PSFObject -TypeName D365FO.TOOLS.Azure.Blob "name", 'Length as Size to PSFSize', 'LastModified as DateTime'

More Awesome things Select-PSFObject can do

Enabling Stop-PSFFunction

Example Location

You are currently using Stop-PSFFunction mostly as an enhanced Write-Warning.
Not a problem, but here's what else it can do (and what I had in mind for it):

function Invoke-Division {
    [CmdletBinding()]
    param (
        [int]
        $Number,

        [int]
        $DivideBy,

        [switch]
        $EnableException
    )

    Write-PSFMessage -Level Host -Message "Starting Math"
    try { $Number / $DivideBy }
    catch {
        Stop-PSFFunction -Message "Math Failed" -ErrorRecord $_ -EnableException $EnableException
        return
    }
    Write-PSFMessage -Level Host -Message "Done with Math"
}

The fun part is now:
When a user actually does want a red error (e.g. for a try/catch), they can enable it on demand:

# Just the warning:
Invoke-Division -Number 2 -DivideBy 0 

# Kill it with red fire
Invoke-Division -Number 2 -DivideBy 0 -EnableException

Documentation

You may also find this other command quite helpful when it comes to executing code that might fail.

Parameter Type for Paths

Example Location

Sometimes you want to accept paths as parameter.
Which then means we usually take a string and then later test, whether it exists, etc..
Example:

[string] $OutputPath = "c:\temp\d365fo.tools\WebConfigDecrypted"

With PSFramework, you can add path resolution (relative, wildcards, etc.) and validation to the module:

[PsfDirectory] $OutputPath = "c:\temp\d365fo.tools\WebConfigDecrypted"

Currently it is limited to allowing multiple paths (as if it were a string-array), the next version will have single-item variants.

Documentation

Hashtable Cloning made easy

Example Location

You are doing hashtable cloning to simplify splatting using Get-DeepClone:

$Params = Get-DeepClone $PSBoundParameters
if($Params.ContainsKey("ComputerName")){$null = $Params.Remove("ComputerName")}
if($Params.ContainsKey("OutputServiceDetailsOnly")){$null = $Params.Remove("OutputServiceDetailsOnly")}
if($Params.ContainsKey("OnlyStartTypeAutomatic")){$null = $Params.Remove("OnlyStartTypeAutomatic")}

PSFramework can help with that:

$Params = $PSBoundParameters | ConvertTo-PSFHashtable -ReferenceCommand Get-ServiceList

Not a PSFramework thing, but while I'm at it ... IsNullOrEmpty

Example Location

In pretty much all cases, you can replace [string]::IsNullorEmpty with -not:

if (([string]::IsNullOrEmpty($AccountId) -eq $true) -or
    ([string]::IsNullOrEmpty($Container)) -or
    (([string]::IsNullOrEmpty($AccessToken)) -and ([string]::IsNullOrEmpty($SAS)))) {
}

Could be:

if (
    (-not $AccountId) -or
    (-not $Container) -or
    (-not $AccessToken) -and (-not $SAS)
) {
}
@FH-Inway
Copy link
Owner

FH-Inway commented Dec 7, 2024

Moved this to d365collaborative#870

@FH-Inway FH-Inway closed this as completed Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants