为什么我需要在PowerShell脚本中首先编写我的函数? [英] Why do I need to have my functions written first in my PowerShell script?

查看:134
本文介绍了为什么我需要在PowerShell脚本中首先编写我的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我使用函数来包装代码的部分,让我在指定点移动的部分。



不工作的例子



  $ stepChoice = read-host'你想从哪里开始。

switch($ stepChoice)
{
1 {Step1}
2 {Step2}
3 {Step3}

}

#Steps.ps1
function Step1 {
'Step 1'
Step2
}
function Step2 {
'Step 2'
Step3
}
function Step3 {
'Step 3'
'Done!'
}



错误



这会给我以下错误:


术语Step1不被识别为小命令,功能,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包括路径,请验证路径是否正确,然后重试。

 在C:\Tools\Scripts\functiontest.ps1:7 char:12 
+ 1 { Step1<<<< }
+ CategoryInfo:ObjectNotFound:(Step1:String)[],CommandNotFoundException
+ FullyQualifiedErrorId:CommandNotFoundException *




工作示例



如果我更改其周围的顺序工作正常:

 #Steps.ps1 
function Step1 {
'Step 1'
Step2
}
Step2 {
'Step 2'
Step3
}
function Step3 {
'Step 3'
'Done!'
}

#steps
$ stepChoice = read-host'你想从哪里开始。

switch($ stepChoice)
{
1 {Step1}
2 {Step2}
3 {Step3}

}



为什么?



我猜这是因为PS没有加载函数。


解决方案

重新排序您的脚本



我猜想PowerShell是一个解释器,只是从顶部到底部线性地执行一个脚本(在对脚本进行标记化之后),然后一路评估脚本。如果它还没有得到一个函数的定义,你已经尝试调用该函数,PowerShell将会错误。



这里简单的解决方案是

转发声明



即使一些编译语言的行为是这样的 - 最引人注目的是C / C ++ - 并且需要 forward declarations 来解决这个问题。



其他语言(如C#)在编译期间对代码执行多次传递,因此不需要前向递减。


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 have the functions listed first in the script for it to run correctly.

Non-working example

$stepChoice = read-host 'Where would you like to start.'

switch($stepChoice)
{
    1{Step1}
    2{Step2}
    3{Step3}

}

# Steps.ps1 
function Step1 { 
    'Step 1' 
    Step2 
} 
function Step2 { 
    'Step 2' 
    Step3 
} 
function Step3 { 
    'Step 3' 
    'Done!' 
}

Error

This give me the following error:

The term 'Step1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

 At C:\Tools\Scripts\functiontest.ps1:7 char:12
  +     1{Step1 <<<< }
  + CategoryInfo          : ObjectNotFound: (Step1:String) [], CommandNotFoundException
  + FullyQualifiedErrorId : CommandNotFoundException*

Working example

If I change the order around it works fine:

# Steps.ps1 
function Step1 { 
    'Step 1' 
    Step2 
} 
function Step2 { 
    'Step 2' 
    Step3 
} 
function Step3 { 
    'Step 3' 
    'Done!' 
}

#steps
$stepChoice = read-host 'Where would you like to start.'

switch($stepChoice)
{
    1{Step1}
    2{Step2}
    3{Step3}

}

Why?

I am guessing that it is because PS is not loading the functions.

Why is this and is there a better way to lay out this code structure?

解决方案

Reorder your script

I'm guessing that PowerShell, being an interpreter, just goes through a script linearly top to bottom, (after tokenizing the script) and evaluates the script along the way. If it hasn't gotten to the definition of a function yet and you're already attempting to invoke that function, PowerShell will error out.

The simple solution here is to move the function definitions before the switch statement -- as you've discovered.

Forward declarations

Even some compiled languages behave this way -- most notably C/C++ -- and require forward declarations to work around this issue.

Other languages like C# do multiple passes over the code during compilation so that forward decls aren't required.

这篇关于为什么我需要在PowerShell脚本中首先编写我的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆