以下是关于如何使用 PowerShell 动态授权不同 Salesforce 环境的详细指南。我们将通过代码示例和说明,帮助您构建一个安全且可扩展的授权脚本。
1. 背景与目标
Salesforce CLI 提供了多种授权方式(如 Web 登录、JWT 认证等)。本文将展示如何通过 PowerShell 脚本动态选择不同的 Salesforce 环境(Dev、UAT、Prod 等),并自动调用 Salesforce CLI 完成授权。
目标:
- 动态选择 Salesforce 环境
- 自动读取环境配置(含测试敏感信息)
- 调用 Salesforce CLI 完成授权
- 替换敏感信息为测试占位符(如
YOUR_CONSUMER_KEY
)
2. 环境准备
2.1 安装 Salesforce CLI
确保已安装 Salesforce CLI 并添加到系统路径中。可通过以下命令验证:
css
sf --version
2.2 示例环境配置
定义不同 Salesforce 环境的配置表(敏感信息替换为测试占位符):
ini
$environmentConfig = @{
"TransPoCDev" = @{
ConsumerKey = "YOUR_CONSUMER_KEY_1" # 替换为测试值
ClientSecret = "YOUR_CLIENT_SECRET_1" # 替换为测试值
Url = "https://test.sfcrmproducts.cn"
UserName = "test.user@example.com.transpoc"
}
"UATCN" = @{
ConsumerKey = "YOUR_CONSUMER_KEY_2" # 替换为测试值
ClientSecret = "YOUR_CLIENT_SECRET_2" # 替换为测试值
Url = "https://uat.sfcrmproducts.cn"
UserName = "test.user@example.com.uatcn"
}
"LocalDevCN" = @{
ConsumerKey = "YOUR_CONSUMER_KEY_3" # 替换为测试值
ClientSecret = "YOUR_CLIENT_SECRET_3" # 替换为测试值
Url = "https://local.sfcrmproducts.cn"
UserName = "test.user@example.com.localdev"
}
}
3. 核心脚本逻辑
3.1 检查 Salesforce CLI
确保 Salesforce CLI 可用:
sql
function Check-SalesforceCLI {
if (-not (Get-Command "sf" -ErrorAction SilentlyContinue)) {
Write-Host "Salesforce CLI 未安装或未添加到 PATH。请先下载并安装。" -ForegroundColor Red
exit 1
}
}
3.2 动态选择环境
允许用户输入环境名称,并验证是否存在:
bash
Check-SalesforceCLI
# 显示可用环境
$availableEnvironments = $environmentConfig.Keys -join ", "
Write-Host "可用环境: $availableEnvironments" -ForegroundColor Cyan
# 获取用户输入
$environmentName = Read-Host "请输入要授权的环境名称"
# 验证环境是否存在
if (-not $environmentConfig.ContainsKey($environmentName)) {
Write-Host "未找到环境: $environmentName。请检查拼写或配置新环境。" -ForegroundColor Red
exit 1
}
3.3 读取环境配置
根据用户输入的环境名,获取对应的配置信息:
ini
$config = $environmentConfig[$environmentName]
$consumerKey = $config["ConsumerKey"]
$clientSecret = $config["ClientSecret"]
$url = $config["Url"]
$username = $config["UserName"]
4. 调用 Salesforce CLI 授权
4.1 Web 登录(无需 ConsumerKey/ClientSecret)
通过浏览器完成授权(推荐用于交互式场景):
bash
# 构造授权命令
$command = "sf org login web -r $url -a $environmentName -i $username"
Write-Host "正在执行命令: $command" -ForegroundColor Green
# 调用 CLI
Invoke-Expression $command
4.2 JWT 登录(需要 ConsumerKey/OPEN SSL Private Key)
通过 JWT 令牌完成无交互授权(适用于自动化脚本):
bash
$command = "sf org login jwt -r $url -a $environmentName --client-id $consumerKey -u $username --jwt-key-file /path/to/server.key"
Write-Host "正在执行命令: $command" -ForegroundColor Green
Invoke-Expression $command
5. 安全性建议
5.1 敏感信息管理
-
避免硬编码 :将敏感信息(如
ConsumerKey
、ClientSecret
)通过环境变量或加密文件提供。 -
示例:通过环境变量读取敏感信息:
ini$consumerKey = $env:SF_CONSUMER_KEY $clientSecret = $env:SF_CLIENT_SECRET
5.2 加密配置文件
将敏感配置存储在加密文件中(需 PowerShell 7+):
ini
# 加密配置
$secureConfig = ConvertTo-SecureString -String (ConvertFrom-Json (Get-Content "config.json")) -AsPlainText -Force
Set-Content -Path "config.enc" -Value (ConvertFrom-SecureString $secureConfig)
# 解密配置
$secureData = Get-Content "config.enc" | ConvertTo-SecureString
$config = ConvertFrom-SecureString -SecureString $secureData
6. 完整脚本示例
ini
# sf-authorize.ps1
# 设置环境变量(禁用 DNS 检查)
$env:SF_DISABLE_DNS_CHECK = "true"
$env:NODE_TLS_REJECT_UNAUTHORIZED = "0"
$env:SF_DOMAIN_RETRY = "0"
# 检查 Salesforce CLI
function Check-SalesforceCLI {
if (-not (Get-Command "sf" -ErrorAction SilentlyContinue)) {
Write-Host "Salesforce CLI 未安装或未添加到 PATH。请先下载并安装。" -ForegroundColor Red
exit 1
}
}
# 定义测试环境配置
$environmentConfig = @{
"TransPoCDev" = @{
ConsumerKey = "YOUR_CONSUMER_KEY_1"
ClientSecret = "YOUR_CLIENT_SECRET_1"
Url = "https://test.sfcrmproducts.cn"
UserName = "test.user@example.com.transpoc"
}
"UATCN" = @{
ConsumerKey = "YOUR_CONSUMER_KEY_2"
ClientSecret = "YOUR_CLIENT_SECRET_2"
Url = "https://uat.sfcrmproducts.cn"
UserName = "test.user@example.com.uatcn"
}
"LocalDevCN" = @{
ConsumerKey = "YOUR_CONSUMER_KEY_3"
ClientSecret = "YOUR_CLIENT_SECRET_3"
Url = "https://local.sfcrmproducts.cn"
UserName = "test.user@example.com.localdev"
}
}
# 主逻辑
Check-SalesforceCLI
# 显示可用环境
$availableEnvironments = $environmentConfig.Keys -join ", "
Write-Host "可用环境: $availableEnvironments" -ForegroundColor Cyan
# 获取用户输入
$environmentName = Read-Host "请输入要授权的环境名称"
# 验证环境是否存在
if (-not $environmentConfig.ContainsKey($environmentName)) {
Write-Host "未找到环境: $environmentName。请检查拼写或配置新环境。" -ForegroundColor Red
exit 1
}
# 读取环境配置
$config = $environmentConfig[$environmentName]
$consumerKey = $config["ConsumerKey"]
$clientSecret = $config["ClientSecret"]
$url = $config["Url"]
$username = $config["UserName"]
# 选择授权方式
$authMethod = Read-Host "Pls choose auth type - (web/jwt)"
Write-Host "Here is the user info you may need: $username" -ForegroundColor Cyan
Write-Host "Here is the secret info you may need: $clientSecret" -ForegroundColor Cyan
if ($authMethod -eq "web") {
$command = "sf org login web -r $url -a $environmentName -i $consumerKey"
} elseif ($authMethod -eq "jwt") {
$command = "sf org login jwt -r $url -a $environmentName --client-id $consumerKey -u $username --jwt-key-file /path/to/server.key"
} else {
Write-Host "Invalid auth type" -ForegroundColor Red
exit 1
}
# 执行命令
Write-Host "Executing Commend: $command" -ForegroundColor Green
Invoke-Expression $command
7. 测试与验证
- 将上述脚本保存为
sf-authorize.ps1
。 - 替换所有测试占位符为实际值(例如
YOUR_CONSUMER_KEY_1
)。 - 运行脚本并输入环境名称及授权方式,观察是否成功授权。
8. 总结
通过 PowerShell 脚本动态授权 Salesforce 环境,可以显著提升开发效率和安全性。关键点包括:
- 环境配置管理:通过哈希表灵活管理多环境配置。
- 用户交互 :通过
Read-Host
提供交互式输入。 - 安全性:避免敏感信息硬编码,推荐使用环境变量或加密文件。
- 扩展性:支持 Web 登录和 JWT 登录两种模式,适应不同场景需求。
如需进一步优化,可结合 CI/CD 工具(如 GitHub Actions)实现自动化授权流程。