Why do I need to have my functions written first in my PowerShell script?

前端 未结 6 962
忘掉有多难
忘掉有多难 2020-12-29 01:06

I have a script that I am utilizing functions to wrap parts of the code that allow me to move through the sections at a specified point. What I have found is that I have to

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 01:32

    You can also source your function definitions from a separate file:

    Steps-Lib.ps1

    # Since this is just function definitions it is safe to source
    function Step1 { 
        'Step 1' 
        Step2 
    } 
    function Step2 { 
        'Step 2' 
        Step3 
    } 
    function Step3 { 
        'Step 3' 
        'Done!' 
    }
    

    Steps.ps1

    # This sources the Steps-Lib.ps1 so that the functions are available
    . "./Steps-Lib.ps1"
    
    $stepChoice = read-host 'Where would you like to start.'
    
    switch($stepChoice)
    {
        1{Step1}
        2{Step2}
        3{Step3}
    }
    

提交回复
热议问题