如何将所需参数传递给 Powershell ISE 中的脚本?
参见标题。
我在脚本的头部指定了所需的参数:
param ($G_ARCHIVE = $(throw "Need file to upload!"),
$G_LOGFILE = $(throw "Need logfile!"))
当我想使用 Powershell ISE 调试脚本时:如何填写这些参数?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用命令窗格。在 ISE 编辑器中打开脚本文件,设置断点 (F9)。然后在命令窗格中键入使用所需参数调用此脚本的命令。我认为 ISE 中没有其他(内置)方法可以做到这一点。
Use the command pane. Open the script file in the ISE editor, set the breakpoints (F9). Then in the command pane type a command invoking this script with required parameters. I do not think there is another (built-in) way of doing this in ISE.
还有另一种方法。您可以使用 $PSDefaultParameterValues 自动变量(自 v3 起)为 cmdlet 和高级函数(不适用于普通函数)提供新的默认参数。然而,它确实适用于脚本,即使在 ISE 中调试时也是如此。您必须像高级函数一样声明
[CmdletBinding()]
或[Parameter()]
。因此,对于您的示例,
您将在 ISE 提示符上执行类似的操作:
您还可以将参数值设置为将在运行时自动执行的脚本块:
该变量是一个哈希表,并且所有标准语法都适用,除了键必须包含脚本(或高级函数或cmdlet)的名称,后跟冒号,然后是参数名称。您可以为多个脚本或命令设置默认值,并为每个脚本或命令设置多个参数(每个参数都是一个新的表条目)。
通过这样做,您只需按 F5 即可像平常一样运行脚本。参数将从变量中获取,因此您无需输入任何内容。
$PSDefaultParameterValues
的其他用例可能是自定义,例如让 Get-History 仅获取最后 10 个条目,除非您在命令中指定-Count
参数。由于条目仅在当前会话中保留,因此您需要将自定义添加到 个人资料。您可以通过在提示符下键入Get-Help about_Parameters_Default_Values
来了解更多信息,或者在 TechNet。There is another way. You can use the
$PSDefaultParameterValues
automatic variable, which exists (since v3) to provide new default arguments to cmdlets and advanced functions (doesn't work with normal functions). However, it does work for scripts, even when debugging in ISE. You have to declare[CmdletBinding()]
or[Parameter()]
like you would for an advanced function.So for your example,
you would execute something like this on the ISE Prompt:
You could also set the parameter value to a script block which will auto-execute at run-time:
The variable is a hashtable and all the standard syntax applies, except the key must have the name of the script (or advanced function or cmdlet) followed by a colon then the parameter name. You can set defaults for multiple scripts or commands, and multiple parameters for each (each parameter is a new table entry).
By doing it this way, you can just hit F5 to run your script like normal. The parameters will be taken from the variable, so you don't have to type anything in.
Other use cases for
$PSDefaultParameterValues
might be customizations, like have the Get-History get only the last 10 entries, unless you specify the-Count
parameter in the command. Because entries only persist for the current session, you would want to add customizations to your profile. You can read more by typingGet-Help about_Parameters_Default_Values
at the prompt or view the same information on TechNet.在 ISE 中设置所需参数有一种更简单的方法:
在 ISE 中按 F5 之前,设置所需的参数。我通常注释我需要的参数,例如:
# $G_ARCHIVE = "C:\Temp\TestFile_001.txt"
我选择“#”之后的所有内容,然后按 F8。下次我使用 F5 调试脚本时,参数将设置为我正在测试的值,无需通过命令行传递参数。
There is a much simpler way to set needed Parameters in ISE:
Before pressing F5 in ISE, set the Parameter you need. I usually comment the Parameter I need, example:
# $G_ARCHIVE = "C:\Temp\TestFile_001.txt"
I select everything after "#" and press F8. Next time I debug the script with F5, the Parameter is set to the value I am testing with, no need to pass the Parameters through the command line.
至少在 Powershell 5.1 ISE 中,当您按 F5 运行参数化脚本时,系统会要求您一一输入参数值。
当使用
$PSDefaultParameterValues
填充变量时,您可以通过$psISE
变量引用加载的脚本,例如At least in
Powershell 5.1 ISE
when you pressF5
to run a parameterized script, you will be asked to enter values for the parameters one by one.When using the
$PSDefaultParameterValues
to populate the variables, you can reference the loaded script through the$psISE
variable like我今天遇到了这个问题。经过一番折腾后,我发现下部面板正在调试 Eg Prompt '[Dbg]: PS'
中运行
我停止了调试并使用参数重新发出了我的脚本。
示例: ./myScript.ps1 -ForceBuild $true
我的结果是脚本启动并且断点触发并允许我按预期进行调试。
I had this problem today. After messing around I discovered that the lower panel was running in Debug E.g. Prompt '[Dbg]: PS'
I stopped the debugging and re-issued my script with paramaeters.
Example: ./myScript.ps1 -ForceBuild $true
My results were that the script started and the breakpoints fired and allowed me to debug as expected.