如何将所需参数传递给 Powershell ISE 中的脚本?

发布于 2024-10-12 10:24:47 字数 207 浏览 1 评论 0 原文

参见标题。

我在脚本的头部指定了所需的参数:

param ($G_ARCHIVE = $(throw "Need file to upload!"),
       $G_LOGFILE = $(throw "Need logfile!"))

当我想使用 Powershell ISE 调试脚本时:如何填写这些参数?

See Title.

I specified needed parameters in the head of a script:

param ($G_ARCHIVE = $(throw "Need file to upload!"),
       $G_LOGFILE = $(throw "Need logfile!"))

When I want to debug the script with Powershell ISE: how can I fill these parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夏日浅笑〃 2024-10-19 10:24:47

使用命令窗格。在 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.

﹂绝世的画 2024-10-19 10:24:47
  1. 在 Windows Powershell ISE 中打开脚本 (myscript.ps1)
  2. 在要检查(调试)的变量处按 F9。例如,下面示例中的第二行,其中 $outputText 变量被分配
  3. 在 shell 窗口中提供脚本的相对路径以及参数值。例如: .\myscript.ps1 "my value"
  4. 按 Enter 键(无需按 F5
  5. 您将能够看到以黄色突出显示的调试断点。将光标置于所需变量以检查当前值。

显示使用 ISE 和命令参数进行 PowerShell 调试的示例

  1. Open the script (myscript.ps1) in Windows Powershell ISE
  2. Press F9 at the variable you want to inspect (debug). For instance 2nd line in the sample below where the $outputText variable is being assigned
  3. In the shell window provide the relative path of the script along with the param value. For instance: .\myscript.ps1 "my value"
  4. Hit enter (you don't need to hit F5)
  5. You'll be able to see the debugging breakpoints in highlighted with yellow color. Place your cursor to the desired variable to inspect the current value.

A sample showing PowerShell debugging with ISE and command parameter

遗忘曾经 2024-10-19 10:24:47

还有另一种方法。您可以使用 $PSDefaultParameterValues 自动变量(自 v3 起)为 cmdlet 和高级函数(不适用于普通函数)提供新的默认参数。然而,它确实适用于脚本,即使在 ISE 中调试时也是如此。您必须像高级函数一样声明 [CmdletBinding()][Parameter()]

因此,对于您的示例,

[CmdletBinding()]
param ($G_ARCHIVE = $(throw "Need file to upload!"),
$G_LOGFILE = $(throw "Need logfile!"))

您将在 ISE 提示符上执行类似的操作:

$PSDefaultParameterValues.add("ExampleScript.ps1:G_ARCHIVE","File-to-upload.txt")
$PSDefaultParameterValues.add("ExampleScript.ps1:G_LOGFILE","Example.log")

您还可以将参数值设置为将在运行时自动执行的脚本块:

$PSDefaultParameterValues["ExampleScript.ps1:G_LOGFILE"]={
  "Example-{0:yyMMddHHmm}.log" -f [datetime]::Now
}

该变量是一个哈希表,并且所有标准语法都适用,除了必须包含脚本(或高级函数或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,

[CmdletBinding()]
param ($G_ARCHIVE = $(throw "Need file to upload!"),
$G_LOGFILE = $(throw "Need logfile!"))

you would execute something like this on the ISE Prompt:

$PSDefaultParameterValues.add("ExampleScript.ps1:G_ARCHIVE","File-to-upload.txt")
$PSDefaultParameterValues.add("ExampleScript.ps1:G_LOGFILE","Example.log")

You could also set the parameter value to a script block which will auto-execute at run-time:

$PSDefaultParameterValues["ExampleScript.ps1:G_LOGFILE"]={
  "Example-{0:yyMMddHHmm}.log" -f [datetime]::Now
}

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 typing Get-Help about_Parameters_Default_Values at the prompt or view the same information on TechNet.

夜无邪 2024-10-19 10:24:47

在 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.

没︽人懂的悲伤 2024-10-19 10:24:47

至少在 Powershell 5.1 ISE 中,当您按 F5 运行参数化脚本时,系统会要求您一一输入参数值。

当使用 $PSDefaultParameterValues 填充变量时,您可以通过 $psISE 变量引用加载的脚本,例如

$PSDefaultParameterValues.add("$($psISE.CurrentFile.DisplayName):G_ARCHIVE","test")

At least in Powershell 5.1 ISE when you press F5 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

$PSDefaultParameterValues.add("$($psISE.CurrentFile.DisplayName):G_ARCHIVE","test")
以为你会在 2024-10-19 10:24:47

我今天遇到了这个问题。经过一番折腾后,我发现下部面板正在调试 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文