Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active December 23, 2017 07:46
Show Gist options
  • Save turboBasic/2abc24f6fc4e392a7a6e4782fc920618 to your computer and use it in GitHub Desktop.
Save turboBasic/2abc24f6fc4e392a7a6e4782fc920618 to your computer and use it in GitHub Desktop.
[New-AdhocModule.ps1] Creating Module based on all .ps1 files in specified directories. No manifest required, one command and whole bunch of functions are encapsulated into module #powershell
function New-AdhocModule {
#
# .SYNOPSIS
# Creating Module based on all .ps1 files in specified directories
#
# .EXAMPLE
# New-AdhocModule -Path c:\ProgramData\Chocolatey\helpers\functions -Module ChocoTools
#
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'Low'
)]
Param(
[Parameter(
Mandatory,
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName
)]
[ValidateScript({
(Test-Path $_)
})]
[Object[]]
$Path,
[Parameter( Mandatory = $False, Position = 1 )]
[String]
$Module = 'AdhocModule'
)
PROCESS {
if ( $psCmdlet.ShouldProcess( $Path ) )
{
$code = [ScriptBlock]::Create(
'Resolve-Path {0} | Get-ChildItem -Filter *.ps1 | ForEach-Object {{ . $_.FullName }}' -f $Path
)
$functions = @()
Resolve-Path $Path | Get-ChildItem -Filter *.ps1 |
ForEach-Object {
$functions += $_.BaseName
}
New-Module -ScriptBlock $code -Function $functions -Name $Module | Import-Module
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment