Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Created May 8, 2024 20:13
Show Gist options
  • Save jdhitsolutions/d5685f6b7be4cda7123d9dd8a107119e to your computer and use it in GitHub Desktop.
Save jdhitsolutions/d5685f6b7be4cda7123d9dd8a107119e to your computer and use it in GitHub Desktop.
A demonstration PowerShell script using the Crescendo commands to create a PowerShell module.
#requires -version 7.4
#requires -Module Microsoft.PowerShell.Crescendo
#this demo assumes you have copied the du.exe utility from Sysinternals to a folder in your path
#the name of the module to create
$ModuleName = "FolderUsage"
#the parent folder for the module
$ModulePath = "C:\temp"
$commands=@()
$cmd = New-CrescendoCommand -Verb Get -Noun FolderUsage -OriginalName du.exe
$cmd.Platform = @('Windows')
$cmd.Aliases = @('psdu')
$cmd.OriginalCommandElements = @('-c', '-nobanner')
$cmd.OriginalText = $(du -? | Out-String)
$cmd.usage = New-UsageInfo -usage "Get folder usage data"
$cmd.Description = 'Get folder usage statistics using the du.exe utility from Sysinternals. It is assumed du.exe exists in your path.'
$example = @{
Command = 'Get-FolderUsage -Path D:\ -NoRecurse'
Description = 'Get root folder usage from D:\'
OriginalCommand = 'du -c -nobanner d:\'
}
$cmd.Examples += New-ExampleInfo @example
$example = @{
Command = 'Get-FolderUsage -Path C:\work -Level 1'
Description = 'Get top-level folder usage for the root C:\work'
OriginalCommand = 'du -c -nobanner -l 1 c:\work'
}
$cmd.Examples += New-ExampleInfo @example
#add parameters
$param = New-ParameterInfo -Name Path -OriginalName ''
$param.ParameterType = 'string'
$param.DefaultValue = "$env:Temp"
$param.AdditionalParameterAttributes='[ValidateScript({Test-Path $_})]'
$param.Description = 'Specify the top-level folder path like C:\Work or a drive like D:\'
$param.Position = 0
$param.ValueFromPipeline = $true
$param.OriginalPosition = 1
$cmd.Parameters += $param
$param = New-ParameterInfo -Name Levels -OriginalName '-l'
$param.ParameterType = 'int'
#the position after any defaults
$param.OriginalPosition = 0
$param.Description = 'Specify the subdirectory depth.'
$cmd.Parameters += $param
#handlers
$OutputHandlers = @()
$handler = New-OutputHandler
#use the same handler for all parameter sets
$handler.ParameterSetName = "Default"
# $handler | Select- *
$handler.StreamOutput = $true
#This needs to be a literal string
#you can also use an external function as the handler
$handler.handler = '$input | ConvertFrom-CSV | Foreach-Object {$_.PSObject.TypeNames.insert(0,"PSDUObject"); $_}'
$OutputHandlers += $handler
$cmd.OutputHandlers = $OutputHandlers
#add the command even though this module will only have a single command
$commands += $cmd
#Exports
Export-CrescendoCommand -command $commands -targetDirectory $env:TEMP
#create the module
$ModuleLocation = Join-Path -Path $ModulePath -ChildPath $ModuleName
if (-not (Test-Path -Path $ModuleLocation)) {
New-Item -Path $ModulePath -Name $ModuleName -ItemType Directory
}
$splatExport = @{
ModuleName = Join-Path -Path $ModuleLocation -ChildPath $ModuleName
ConfigurationFile = Get-ChildItem $env:TEMP -Filter *.crescendo.json
Force = $True
PassThru = $True
}
Export-CrescendoModule @splatExport | Out-Null
Set-Location $ModuleLocation
Get-ChildItem
Write-Host "`nCrescendo module created. You can update the manifest and add other module features like custom formatting and help documentation." -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment