带输入参数的PowerShell脚本,带有打包器(.json)
我有这个Ps1脚本带输入参数的PowerShell脚本,带有打包器(.json),powershell,Powershell,我有这个Ps1脚本 param ([string] $userpassword, [string] $UserName) Write-host "Start.." (Get-date) Write-host "User Name is "$UserName Write-host "User password is "$userpassword if ($userpassword.length -lt 0) { Write-host "Please enter password!
param ([string] $userpassword, [string] $UserName)
Write-host "Start.." (Get-date)
Write-host "User Name is "$UserName Write-host "User password is
"$userpassword
if ($userpassword.length -lt 0) {
Write-host "Please enter password!"$UserName }
Write-host "End.." (Get-date)
打包程序(.json)如下所示:
"provisioners": [
{
"type": "powershell",
"environment_vars":
[
"userpassword=********",
"UserName=ABC"
],
"scripts":
[
"test.ps1"
]
} ]
.Ps1无法从packer(.json)读取输入参数。与Unix shell脚本不同,Powershell变量(和参数)不是环境变量 Powershell中用于读取环境变量的语法是
$env:variablename
您可以将参数默认值设置为环境变量:
param(
[string] $userpassword = $env:userpassword,
[string] $UserName = $env:username
)
Write-host "Start.." (Get-date)
[...]
或废弃参数块,直接分配变量:
[string] $userpassword = $env:userpassword
[string] $UserName = $env:username
Write-host "Start.." (Get-date)
[...]
虽然问题不是很清楚,但我从标题中推测,您希望在packer下运行PowerShell脚本,并向其传递参数 Packer确实支持这一点,但只支持内联脚本。 参数是环境变量还是直接输入并不重要。 关键是确保脚本从构建机器运行 这是一个两步的过程
“供应者”:[
{
“类型”:“文件”,
“源”:“c:\myscripts\MyPowerShellScript.ps1”,
“目的地”:“c:\temp\MyPowerShellScript.ps1”,
“方向”:“上传”
},
{
“类型”:“外壳”,
“内联”:[
“c:\temp\MyPowerShellScript.ps1 Param1{{user`Param2_from_an_env_var`}”
]
},
我建议澄清这个问题,以明确您想做什么,但我认为基于标题,我的回答是正确的