在 powershell 中打印参数

2023-12-13

我想知道,为什么下面的 powershell 脚本中没有打印字符串参数?

function Get-Name ( [string] $Username ) {
    echo "user : $Username"
}


PS C:\> .\Get-Name.ps1 -username "test"
PS C:\>

脚本文件Get-Name.ps1 only defines功能Get-Name,它不执行它。

使用点源运算符 (.) 使其在调用范围中定义函数,然后您可以执行该函数本身:

PS C:\> . .\Get-Name.ps1
PS C:\> Get-Name -Username test
user : test

或者,删除function Get-Name { and }脚本的一部分正如 Lee_Dailey 所指出的,此时脚本文件本身就变成了参数化函数,然后您可以执行以下操作:

PS C:\> .\Get-Name.ps1 -Username test
user : test

请参阅about_Scripts帮助文件,尤其是关于脚本范围和点来源

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 powershell 中打印参数 的相关文章

随机推荐