首页 > 解决方案 > Powershell 路径名变量

问题描述

在完成了一些在线课程之后,我对 Powershell 脚本非常陌生,并且我已经设置了一项任务来清除模板备份文件夹,并将现有模板移动到备份文件夹中。

我创建的脚本本身没有问题(我希望如此),但是,我们希望这个脚本适用于我们的 PROD 和 DEVTEST 环境——文件路径会有所不同。

如何为 systemfiles 文件夹的文件路径创建变量?

我可以有两个单独的变量,但希望将它作为一个变量。

任何帮助,将不胜感激。谢谢

$ErrorActionPreference = "Stop"
. "$PSScriptRoot\functions.ps1"

$consul_address =       $env:bamboo_consul_address
$our_environment =      $env:bamboo_our_environment
$location =             $env:bamboo_location

$systemfilespath =     
"\\oursite.com\shares\APPSystemFiles\${our_environment}" # DEVTEST env locations

"\\oursite\applications\APPLICATIONNAME\Systemfiles\${our_environment}" # PROD env locations

{
Write-Host "Clearing the content on Printing Template Backups from ${our_environment}"

Remove-Item "${systemfilespath}\PrintingRoot\PrintingTemplateBackup/*" 

(Move-Item -Path "${systemfilespath}\PrintingRoot\PrintingTemplate/*") 
| ForEach-Object
Write-Host "Backing up Printing Template Backup file with Printing Template 
file from ${our_environment}" 
| Set-Content -Path 
"${systemfilespath}\PrintingRoot\PrintingTemplateBackup/*" 
}

标签: powershellvariables

解决方案


如果您需要从中选择一定数量的选项,并且脚本将被手动触发以启动,您可以提示您想要影响的位置。

$systemfilespath = Read-Host -Prompt "Choose a Location"
        Switch ($systemfilespath){
                1 {CLS ; Write-Host -ForegroundColor Green "\\oursite.com\shares\APPSystemFiles\${our_environment}"} # or a desc
                2 {CLS ; Write-Host -ForegroundColor Green "\\oursite\applications\APPLICATIONNAME\Systemfiles\${our_environment}"} # or a desc
                }

推荐阅读