使用带有PowerShell脚本的答案文件

Ale*_*gas 2 powershell

我有一个PowerShell脚本,开头有很多'params':

param(
    [switch] $whatif,
    [string] $importPath = $(Read-Host "Full path to import tool"),
    [string] $siteUrl = $(Read-Host "Enter URL to create or update"),
    [int] $importCount = $(Read-Host "Import number")
)
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以针对答案文件运行此操作以避免每次都输入参数值?

man*_*lds 5

我没有理由提出这个问题.所有你需要做的就是调用你的脚本:

.\script.ps1 -whatif -importPath import_path -siteUrl google.com -importCount 1
Run Code Online (Sandbox Code Playgroud)

Read-Host在那里为默认值,将被执行(然后读取和分配值的参数)只有当你不指定值.只要您拥有上述命令(保存在文件中以便您可以复制并粘贴到控制台或从其他脚本或其他任何内容运行),您就不必一次又一次地输入值.