Call a function in another script when executing using 'Run With PowerShell'

前端 未结 2 1007
攒了一身酷
攒了一身酷 2021-02-03 17:05

I have functions in a \'library\' file to be called from my \'worker\' script.

Library File

function ShowMessage($AValue)
{
  $a = new-         


        
相关标签:
2条回答
  • 2021-02-03 17:47

    In your worker file, dot-source the library file, this will load all content (functions, variables, etc) to the global scope, and then you'll be able to call functions from the library file.

    =================== Worker file ==========================
    # dot-source library script
    # notice that you need to have a space 
    # between the dot and the path of the script
    . c:\library.ps1
    
    ShowMessage -AValue Hello
    =================== End Worker file ======================
    
    0 讨论(0)
  • 2021-02-03 17:48

    In the worker file change to this:

    . "c:\scratch\b.ps1"
    
    ShowMessage "Hello"
    

    As @RoiDanton mentioned below:

    Attention when using relative pathing: Don't forget to prepend a dot before the path . ".\b.ps1".

    The first dot is an operator used to modify the scope and in that context it has nothing to do with paths. See Dot Source Notation.

    0 讨论(0)
提交回复
热议问题