PowerShell script with input parameters with packer(.json)
I have this Ps1 script,
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)
packer(.json) as follows:
"provisioners": [
{
"type": "powershell",
"environment_vars":
[
"userpassword=********",
"UserName=ABC"
],
"scripts":
[
"test.ps1"
]
} ]
.Ps1 can not read input para from packer(.json)...
powershell
add a comment |
I have this Ps1 script,
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)
packer(.json) as follows:
"provisioners": [
{
"type": "powershell",
"environment_vars":
[
"userpassword=********",
"UserName=ABC"
],
"scripts":
[
"test.ps1"
]
} ]
.Ps1 can not read input para from packer(.json)...
powershell
add a comment |
I have this Ps1 script,
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)
packer(.json) as follows:
"provisioners": [
{
"type": "powershell",
"environment_vars":
[
"userpassword=********",
"UserName=ABC"
],
"scripts":
[
"test.ps1"
]
} ]
.Ps1 can not read input para from packer(.json)...
powershell
I have this Ps1 script,
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)
packer(.json) as follows:
"provisioners": [
{
"type": "powershell",
"environment_vars":
[
"userpassword=********",
"UserName=ABC"
],
"scripts":
[
"test.ps1"
]
} ]
.Ps1 can not read input para from packer(.json)...
powershell
powershell
edited Nov 23 '18 at 10:51
Paxz
2,302826
2,302826
asked Nov 23 '18 at 10:43
dhaneenjadhaneenja
1
1
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.
The syntax in Powershell for reading an environment variable is $env:variablename
.
You can either set the param default values to be the env var:
param(
[string] $userpassword = $env:userpassword,
[string] $UserName = $env:username
)
Write-host "Start.." (Get-date)
[...]
Or scrap the param block and just assign the variables directly:
[string] $userpassword = $env:userpassword
[string] $UserName = $env:username
Write-host "Start.." (Get-date)
[...]
add a comment |
Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.
Packer does support this but only for inline scripts.
It does not matter if the parameter is an environment variable or direct entry.
the key thing is to ensure that the script runs from the building machine.
It's a 2 step process.
- Copy the file up to the machine.
- Run the file inline passing params.
Eg
"provisioners": [
{
"type": "file",
"source": "c:myscriptsMyPowerShellScript.ps1",
"destination": "c:tempMyPowerShellScript.ps1",
"direction": "upload"
},
{
"type": "shell",
"inline": [
"c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
]
},
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445151%2fpowershell-script-with-input-parameters-with-packer-json%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.
The syntax in Powershell for reading an environment variable is $env:variablename
.
You can either set the param default values to be the env var:
param(
[string] $userpassword = $env:userpassword,
[string] $UserName = $env:username
)
Write-host "Start.." (Get-date)
[...]
Or scrap the param block and just assign the variables directly:
[string] $userpassword = $env:userpassword
[string] $UserName = $env:username
Write-host "Start.." (Get-date)
[...]
add a comment |
Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.
The syntax in Powershell for reading an environment variable is $env:variablename
.
You can either set the param default values to be the env var:
param(
[string] $userpassword = $env:userpassword,
[string] $UserName = $env:username
)
Write-host "Start.." (Get-date)
[...]
Or scrap the param block and just assign the variables directly:
[string] $userpassword = $env:userpassword
[string] $UserName = $env:username
Write-host "Start.." (Get-date)
[...]
add a comment |
Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.
The syntax in Powershell for reading an environment variable is $env:variablename
.
You can either set the param default values to be the env var:
param(
[string] $userpassword = $env:userpassword,
[string] $UserName = $env:username
)
Write-host "Start.." (Get-date)
[...]
Or scrap the param block and just assign the variables directly:
[string] $userpassword = $env:userpassword
[string] $UserName = $env:username
Write-host "Start.." (Get-date)
[...]
Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.
The syntax in Powershell for reading an environment variable is $env:variablename
.
You can either set the param default values to be the env var:
param(
[string] $userpassword = $env:userpassword,
[string] $UserName = $env:username
)
Write-host "Start.." (Get-date)
[...]
Or scrap the param block and just assign the variables directly:
[string] $userpassword = $env:userpassword
[string] $UserName = $env:username
Write-host "Start.." (Get-date)
[...]
edited Nov 23 '18 at 11:13
answered Nov 23 '18 at 11:02
James C.James C.
8,39222030
8,39222030
add a comment |
add a comment |
Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.
Packer does support this but only for inline scripts.
It does not matter if the parameter is an environment variable or direct entry.
the key thing is to ensure that the script runs from the building machine.
It's a 2 step process.
- Copy the file up to the machine.
- Run the file inline passing params.
Eg
"provisioners": [
{
"type": "file",
"source": "c:myscriptsMyPowerShellScript.ps1",
"destination": "c:tempMyPowerShellScript.ps1",
"direction": "upload"
},
{
"type": "shell",
"inline": [
"c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
]
},
add a comment |
Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.
Packer does support this but only for inline scripts.
It does not matter if the parameter is an environment variable or direct entry.
the key thing is to ensure that the script runs from the building machine.
It's a 2 step process.
- Copy the file up to the machine.
- Run the file inline passing params.
Eg
"provisioners": [
{
"type": "file",
"source": "c:myscriptsMyPowerShellScript.ps1",
"destination": "c:tempMyPowerShellScript.ps1",
"direction": "upload"
},
{
"type": "shell",
"inline": [
"c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
]
},
add a comment |
Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.
Packer does support this but only for inline scripts.
It does not matter if the parameter is an environment variable or direct entry.
the key thing is to ensure that the script runs from the building machine.
It's a 2 step process.
- Copy the file up to the machine.
- Run the file inline passing params.
Eg
"provisioners": [
{
"type": "file",
"source": "c:myscriptsMyPowerShellScript.ps1",
"destination": "c:tempMyPowerShellScript.ps1",
"direction": "upload"
},
{
"type": "shell",
"inline": [
"c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
]
},
Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.
Packer does support this but only for inline scripts.
It does not matter if the parameter is an environment variable or direct entry.
the key thing is to ensure that the script runs from the building machine.
It's a 2 step process.
- Copy the file up to the machine.
- Run the file inline passing params.
Eg
"provisioners": [
{
"type": "file",
"source": "c:myscriptsMyPowerShellScript.ps1",
"destination": "c:tempMyPowerShellScript.ps1",
"direction": "upload"
},
{
"type": "shell",
"inline": [
"c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
]
},
"provisioners": [
{
"type": "file",
"source": "c:myscriptsMyPowerShellScript.ps1",
"destination": "c:tempMyPowerShellScript.ps1",
"direction": "upload"
},
{
"type": "shell",
"inline": [
"c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
]
},
"provisioners": [
{
"type": "file",
"source": "c:myscriptsMyPowerShellScript.ps1",
"destination": "c:tempMyPowerShellScript.ps1",
"direction": "upload"
},
{
"type": "shell",
"inline": [
"c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
]
},
answered Jan 6 at 14:58
Marcus AdamsMarcus Adams
114
114
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445151%2fpowershell-script-with-input-parameters-with-packer-json%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown