Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
相当于Bash';Powershell中的s${myvar:-default}?_Powershell - Fatal编程技术网

相当于Bash';Powershell中的s${myvar:-default}?

相当于Bash';Powershell中的s${myvar:-default}?,powershell,Powershell,在Bash中,我可以执行以下操作: ${VAR1:-default} …这意味着如果VAR1不存在,则返回“default” 这些类型的操作员有很多: 使用Get Variable的例子似乎很多,但没有什么特别简洁的。在Powershell中是否有我遗漏的等价物 如果在处理脚本或函数的参数时需要默认值,则可以提供: param ( [string]$price = 100, [string]$ComputerName = $env:computername,

在Bash中,我可以执行以下操作:

${VAR1:-default}

…这意味着如果VAR1不存在,则返回“default”

这些类型的操作员有很多:


使用
Get Variable
的例子似乎很多,但没有什么特别简洁的。在Powershell中是否有我遗漏的等价物

如果在处理脚本或函数的参数时需要默认值,则可以提供:

param (
    [string]$price = 100, 
    [string]$ComputerName = $env:computername,    
    [string]$username = $(throw "-username is required."),
    [string]$password = $( Read-Host -asSecureString "Input password" )
    [switch]$SaveData = $false
)

但是,如果在将现有变量替换为表达式时表示默认值,则必须编写一个表达式来提供所需的值。

AFAIK PowerShell不支持这种参数扩展。你想达到什么目标?对于函数参数还是一般的变量,您需要它吗?FWIW如果它在用作模板的字符串中,您可以创建自己的模板处理函数,其行为类似于Bash。