[英]How to load variables in powershell dynamically based on a selection
I am creating a powershell script that is going to be run on different environments. 我正在创建一个将在不同环境中运行的powershell脚本。 Therefore I am creating seperate variable files for each environment so that I can have one main script, and then as many environment scripts as required.
因此,我为每个环境创建了单独的变量文件,以便可以拥有一个主脚本,然后根据需要具有多个环境脚本。
However I have been unable to get powershell to dynamically load my specific environment file 但是我无法获得Powershell动态加载我的特定环境文件
For testing purposes I have two files 为了测试目的,我有两个文件
env-CI.ps1 env-QA.ps1 env-CI.ps1 env-QA.ps1
both files only contain one variable [string] $envName = "CI" and [string] $envName = "QA" respectively. 这两个文件分别只包含一个变量[string] $ envName =“ CI”和[string] $ envName =“ QA”。
I am using a promptForChoice to specify what environment I with to work with, but I just cannot get the scripts to load. 我正在使用hintForForChoice来指定要使用的环境,但是我只是无法加载脚本。
I have read about dot but this does not appear to load the files 我已经阅读过有关点的信息,但这似乎无法加载文件
This is my script file 这是我的脚本文件
function whatEnvironment {
[string] $answer = ""
[string] $title = ""
[string] $message = "What environment are you updating"
$ci = New-Object System.Management.Automation.Host.ChoiceDescription "&CI", `
"Update the CI environment."
$qa = New-Object System.Management.Automation.Host.ChoiceDescription "&QA", `
"Update the QA Environment."
$uat = New-Object System.Management.Automation.Host.ChoiceDescription "&UAT", `
"Update the UAT environment."
$production = New-Object System.Management.Automation.Host.ChoiceDescription "&Production", `
"Update the Production Environment."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($ci, $qa, $uat, $production)
:OuterLoop do
{
$answer = $host.ui.PromptForChoice($title, $message, $options, -1)
switch ($answer)
{
0 {
. .\environment-CI.ps1
break OuterLoop
}
1 {
. .\environment-QA.ps1
break OuterLoop
}
2 {break OuterLoop}
3 {break OuterLoop}
}
}
while ($y -ne 100)
return $answer
}
cls
$environment = whatEnvironment
write-host $environment
write-host $envName
No error is thrown, but the file containing the environment variables is not loaded. 不会引发任何错误,但是不会加载包含环境变量的文件。
Do one thing at a time. 一次做一件事。 No weird breaking out of loops and stuff.
没有怪异的循环和东西。 Prompt for the user's decision first, then load the environment file after the user has made a valid choice.
首先提示用户做出决定,然后在用户做出有效选择后加载环境文件。
function whatEnvironment {
[string] $answer = ""
[string] $title = ""
[string] $message = "What environment are you updating"
$ci = New-Object Management.Automation.Host.ChoiceDescription "&CI", "Update the CI environment."
$qa = New-Object Management.Automation.Host.ChoiceDescription "&QA", "Update the QA Environment."
$uat = New-Object Management.Automation.Host.ChoiceDescription "&UAT", "Update the UAT environment."
$production = New-Object Management.Automation.Host.ChoiceDescription "&Production", "Update the Production Environment."
$options = [Management.Automation.Host.ChoiceDescription[]]($ci, $qa, $uat, $production)
do {
$answer = $Host.UI.PromptForChoice($title, $message, $options, -1)
} until ($answer -in 1..4)
switch ($answer) {
0 { ". .\environment-CI.ps1" }
1 { ". .\environment-QA.ps1" }
}
return $answer
}
$environment = whatEnvironment
Write-Host $environment
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.