Require-Function by Bernd Kriszio 24 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1617"></script>download | new post
This is a bit similar to Python import module. If a function is present, nothing happens, otherwise Path is searched for a file with the functionname and the extension .ps1 and that file is dot sourced
- function Require-Function
- {
- <#
- .SYNOPSIS
- Load function when not already loaded
- .DESCRIPTION
- When a function is not loaded and there is a file <functionname>.ps1 in one of the directories listed
- in $env:Path it is dot sourced.
- To get the function in your environment you must dot source the Require-Function too.
- .NOTES
- File Name : Require-Function.ps1
- Author : Bernd Kriszio - http://pauerschell.blogspot.com/
- .PARAMETER FunctionName
- The name of the function you want to import
- .EXAMPLE
- . Require-Function <any_function_script_in_your_path>
- .EXAMPLE
- . Require-Function New-ISEFile -verbose
- .EXAMPLE
- . Require-Function New-ISEFile
- .EXAMPLE
- . Require-Function unbekanntn -verbose
- #>
- [cmdletbinding()]
- param (
- [string] $FunctionName
- )
- if (! (Test-Path function:$FunctionName))
- {
- try{
- Write-Verbose "Function $FunctionName not loaded"
- $cmd = "$($FunctionName).ps1"
- #"$cmd"
- . $cmd
- }
- catch
- {
- "$cmd could not be loaded"
- break;
- }
- Write-Verbose "Function $FunctionName is loaded"
- }
- else
- {
- Write-Verbose "Function $FunctionName was loaded"
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository