简体   繁体   English

在PowerShell中将标志从一个脚本移动到另一个脚本

[英]Move flag from one script to another in PowerShell

I have 2 powershell scripts script1.ps1 and script2.ps1 . 我有2个powershell脚本script1.ps1script2.ps1 They both contain the same parameter flag and integer parameter [int]$num, [switch]$silent = $false . 它们都包含相同的参数标志和整数参数[int]$num, [switch]$silent = $false

Now I call script.ps1 with parameters script.ps1 222 -silent . 现在,我使用参数script.ps1 222 -silent调用script.ps1 I want to call script2.ps1 from script1.ps1 with that flag and another integer parameter. 我想使用该标志和另一个整数参数从script1.ps1调用script2.ps1 But the only decision I found is 但是我发现的唯一决定是

param([int]$num, [switch]$silent = $false)

if($silent) {
    .\script2.ps1 333 -silent 
} else {
    .\script2.ps1 333
}

Is there more brief and convenient way to send parameter flag from one script to another ? 有没有更简单,方便的方法将参数标志从一个脚本发送到另一个脚本?

Switch parameters accept boolean values, so you could do something like this: 开关参数接受布尔值,因此您可以执行以下操作:

Param(
    [int]$num,
    [Switch]$silent = $false
)

.\script2.ps1 333 -silent:$silent.IsPresent

Note that this doesn't behave exactly like a passthru. 请注意,这并不完全像passthru。 The second script in this case will always be passed the parameter -silent , just with an explicit value of $true or `$false. 在这种情况下,第二个脚本将始终传递参数-silent ,只是其显式值为$true或`$ false。

If you want to not pass the parameter when your first script wasn't called with it you can't get around actually making that distinction. 如果您不希望在未调用第一个脚本时不传递参数,则无法绕开该区别。 In that case you need something like this: 在这种情况下,您需要以下内容:

Param(
    [int]$num,
    [Switch]$silent = $false
)

$extraParams = @{}
if ($PSBoundParameters.ContainsKey('silent')) {
    $extraParams['silent'] = $silent.IsPresent
}

.\script2.ps1 333 @extraParams

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell 脚本将文件从一个位置移动到另一个位置,然后将消息框显示到我们网络上的另一台计算机 - Powershell script to move file from one location to another then display message box to a different computer on our network 如何创建powershell脚本以将数千个日志文件从一个位置移动到另一个位置? - How can i create a powershell script to move thousands of log files from one location to another? 读取文件,然后根据文件内容名称,使用 Powershell 脚本将文件从一个文件夹移动到另一个文件夹 - Read a file and then based on file content names, move the file from one folder to another using Powershell script 读取文件,然后使用 Powershell 脚本将内容从一个文件夹移动到另一个文件夹 - Read a file and then move the content from one folder to another using Powershell script PowerShell脚本将文件和文件夹(包括子文件夹)从一个位置移动到x天以外的另一个位置 - PowerShell script to move files and folders including subfolders from one location to another older than x days 将参数从另一个PowerShell脚本传递给一个PowerShell脚本 - To pass argument to one powershell script from another powershell script 通过传递参数从另一个PowerShell脚本调用一个PowerShell脚本 - Invoke one PowerShell script from Another PowerShell script with passing arguments 从第一个脚本调用另一个Powershell脚本 - Calling another powershell script from first one 从另一个运行 PowerShell 脚本 - Run a PowerShell script from another one 将参数从一个Powershell脚本传递到另一个 - Pass arguments from one powershell script to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM