在编写 powershell 脚本时,我必须 运行 两次才能使我所做的任何更改对函数内部的代码生效。有什么建议么?

When writing powershell script, I have to run it twice for any changes I made to take effect to code that's inside a function. Any suggestions?

如标题所示,当我在 Visual Studio 2013 或 Powershell ISE 中编写 powershell 脚本时。如果我对脚本进行更改,当我 运行 它时,我所做的更改不会应用,直到我第二次 运行 它。不仅是变量,还有整行代码。

这是产生问题的示例代码:

Write-Output "Code Updates";
Test;

Function Test{      
    Write-Output "Code Doesn't Update";
}

如果您更改两个 Write-Output 命令中的文本,只有函数外部的文本会在第一个 运行 上更新。

对发生这种情况的原因有什么建议吗?

Powershell 脚本只能从上到下读取。我必须先声明函数然后调用它。

是的,您在它有时间重新声明函数并获取更改之前调用函数。

测试时,您可以突出显示函数并按 F8(在 ISE 中)重新声明函数,而无需重新运行 整个脚本。

最佳实践表明,您应该在任何逻辑之前将功能置于顶部。将函数放在模块文件中,并在第一行导入模块。

问候,Edwoli。

您不能使用不存在的代码。您说您正在使用 Visual Studio 2013,这意味着您是开发人员类型或正在尝试成为开发人员类型。函数就像 Visual Studio 引用/库。除非先加载并实例化,否则不能使用引用/库。

脚本语言自上而下读取并加载操作自上而下。

以后与其他代码块相关的操作要使用的任何代码,例如函数或模块及其公开的函数和方法,都必须首先加载。

如果您想使用任何 PowerShell cmdlet,则必须加载其模块及其关联方法、函数、类 等(首先在内存中)

您编写的任何自定义函数都必须是脚本首先加载/读取的内容。

请参阅下文以获取更多指导。

Design Your PowerShell Functions to Be Reusable

Here are a few things I try to think about as I am writing a function that will be reusable:

• Functions should be short and sweet. Functions should do one thing and do it very well. If your function is implementing multiple layers of processing and logic, you should probably break it down into smaller functions. Excluding parameters and comments, I would suggest that the main body of a function should rarely be more than 10 or 12 lines of code. If it is longer than that, ask yourself if the steps can be broken down further.

• Functions should be pipeline friendly. Functions should take input objects from the pipeline and also write objects to the pipeline. A reusable function should never use Write-Host, unless you are writing a function specifically to format some special format. Remember that something that is output with Write-Host is not put in the pipeline.

• Use Parameters wisely. Parameters should generally always have default values. For example, a Get- function should always return a value. Parameters can be used to allow a user to filter the output from the cmdlet. Think about how Get-Process works. You can specify a particular process, but if you do not, they are all returned. As you are writing your function and you declare a variable, you should ask yourself if this is something the end user may want to change. If so, provide a good default value that most people will use, but allow the end user to change it if they want to.

• Functions should be named properly. Make sure that you use an accepted verb and well thought out noun for your function. If you adhere to these standards, it will be easier for others to incorporate your functions into their code. Also, it will make your function more discoverable and the end users will have a good idea of what to expect when they see your function’s name.

https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/20/design-your-powershell-functions-to-be-reusable

Windows PowerShell: Build a Better Function https://technet.microsoft.com/en-us/library/hh360993.aspx