pf-loader.ps1
function Get-ScriptFolder { $callStack = Get-PSCallStack | Where-Object ScriptName $scriptPath = $callStack[1] $scriptPath = Split-Path $scriptPath.ScriptName -Parent return $scriptPath } function Get-Module_Metadata ($path) { $content = Get-Content -Path $path -Raw -ErrorAction SilentlyContinue if (-not $content) { return @() } $data = $content | ConvertFrom-Pson return $data } function Register-PSRepository_Local { $global:PSRepositoryLocal = 'LocalRepo' if (-not (Get-PSRepository -Name $PSRepositoryLocal -ErrorAction SilentlyContinue)) { $PSRepo = 'C:\code\PSRepository' mkdir $PSRepo -Force Register-PSRepository -Name $PSRepositoryLocal -SourceLocation $PSRepo ` -PublishLocation $PSRepo -InstallationPolicy Trusted } } function Get-ScriptList($path) { if (-not $path) { $path = Get-ScriptFolder } $result = Get-ChildItem -Path $path -Recurse -Include "*.ps1" | Sort-Object FullName return $result } function Uninstall-Module_WhenDuplicated { [CmdletBinding()] param( [Parameter(ValueFromPipeline=$true)] $Module, [switch]$PassThru ) process { $mm = Get-module $Module.Name -ListAvailable if ( $mm -and -not $mm.Path.StartsWith($Module.DirectoryName)) { Uninstall-Module $Module.Name -Force -Verbose } if ($PassThru) { return $Module } } } function Import-Module_Reload { [CmdletBinding()] param( [Parameter(ValueFromPipeline=$true)] $Module ) process { if ($Module) { if (get-module $Module.Name ) { Write-Host "Removing $($Module.Name)" remove-module $Module.Name -Force } $modulePath = Join-Path -Path $Module.DirectoryName -ChildPath ($Module.Name + ".psm1") if (Test-Path $modulePath) { $modulePsd1 = Join-Path -Path $Module.DirectoryName -ChildPath ($Module.Name + ".psd1") $psd1 = Import-PowerShellDataFile -Path $modulePsd1 -ErrorAction SilentlyContinue if ($psd1.PrivateData.PSData.ExternalModuleDependencies) { $psd1.PrivateData.PSData.ExternalModuleDependencies | ForEach-Object { Import-Module_Smart $_ } } Import-Module $Module.DirectoryName -Global -Force } } } } function Import-Module_Smart { [CmdletBinding()] param( [Parameter(ValueFromPipeline=$true)] $Module ) process { if (get-module $module -ErrorAction SilentlyContinue ) { return } $moduleRoot = Get-ChildItem -Filter "$module.psm1" -Recurse | Select-Object -First 1 if ( $moduleRoot ) { $moduleFolder = Split-Path $moduleRoot -Parent Import-Module $moduleFolder -Global -Force return } if (-not (Get-Module -name $name -ListAvailable )) { Set-PSRepository -name PsGallery -InstallationPolicy Trusted $mm = Install-Module -Name $name -Repository PsGallery -PassThru $mm | Import-Module } } } function Get-Modules_Local { $folders = Get-ChildItem -file -Recurse -Filter *.* -include @('*.psd1', '*.psm1', "*.ps1") | Where-Object { $_.FullName -notlike "*pending*" } | Where-Object { $_.BaseName -eq $_.Directory.BaseName } | Select-Object DirectoryName -Unique $folders.DirectoryName | ForEach-Object { [PSCustomObject]@{ Name = (Split-Path $_ -LeafBase) DirectoryName = $_ } } } function Import-Script_AsModule { [CmdletBinding()] param ( $ScriptName, [string[]]$WhenNotDefined, $RequiredVersion, [Switch]$Global, [Switch]$PassThru ) if ($WhenNotDefined) { foreach ($item in $WhenNotDefined) { $cmd = Get-Command -Name $item -ErrorAction SilentlyContinue if ( $cmd ){ if ($PassThru) { return $cmd.Module } return } } } $installResult = Install-Script -Name $ScriptName -PassThru -Force -RequiredVersion $RequiredVersion $scriptPath = Join-Path $installResult.InstalledLocation -ChildPath "$ScriptName.ps1" $scriptContent = Get-Content -Raw -Path $scriptPath $sb = [scriptblock]::Create($scriptContent) $mm = New-Module -ScriptBlock $sb -Name "ImportScript_$ScriptName" $mm | Import-Module -Global:($Global.IsPresent) -Force -PassThru:($PassThru.IsPresent) } function Import-PSSnapin ($name) { if (-not (Get-PSSnapin | Where-Object Name -eq $name)) { if ( Get-PSSnapin -Registered | Where-Object Name -eq $name ) { Add-PsSnapin $name } else { Write-Warning "SNAPIN not available : '$name'" } } } function Import-PSSnapin:::Example { Import-PSSnapin Microsoft.SharePoint.PowerShell } |