PoshCode Archive  Artifact [5683b75ad8]

Artifact 5683b75ad8a4b005af69e7a8eba4f2c10ec88b8785907359bdd92ba2685d07d2:

  • File Require-Function.ps1 — part of check-in [418ce9f4cf] at 2018-06-10 12:59:14 on branch trunk — 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 (user: unknown size: 1748)

# encoding: ascii
# api: powershell
# title:  Require-Function
# description: 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
# version: 0.1
# type: function
# license: CC0
# function: Require-Function
# x-poshcode-id: 1617
# x-archived: 2010-02-03T16:13:31
#
#
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"
    }
}