You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本添加站点选择功能的实现咨询

实现站点选择功能的PowerShell代码示例

方法1:基础输入验证式选择

直接提示用户输入站点标识,同时做输入验证,确保用户输入合法选项:

# 预定义文件源路径
$sourceGB = '\\machineGB\5400\630G8'
$sourceAP = '\\machineAP\5400\630G8'

# 循环提示用户选择站点,直到输入合法
do {
    $siteChoice = Read-Host "请选择文件源站点(输入GB选择广州站点,输入AP选择亚太站点)"
    # 统一转为大写,避免大小写问题
    $siteChoice = $siteChoice.ToUpper()
    if ($siteChoice -notin @('GB', 'AP')) {
        Write-Host "输入无效,请输入GB或AP" -ForegroundColor Red
    }
} while ($siteChoice -notin @('GB', 'AP'))

# 根据选择赋值对应的文件源
switch ($siteChoice) {
    'GB' { $selectedSource = $sourceGB }
    'AP' { $selectedSource = $sourceAP }
}

# 验证结果,后续补丁部署逻辑直接用$selectedSource即可
Write-Host "已选择文件源:$selectedSource" -ForegroundColor Green

方法2:交互式菜单选择(更直观)

如果想让用户通过数字选择,体验更友好:

# 预定义文件源路径
$sourceGB = '\\machineGB\5400\630G8'
$sourceAP = '\\machineAP\5400\630G8'

# 展示选择菜单
Write-Host "=== 请选择文件源站点 ==="
Write-Host "1. 广州站点(GB)"
Write-Host "2. 亚太站点(AP)"

# 循环验证输入
do {
    $menuChoice = Read-Host "请输入对应数字(1/2)"
    if ($menuChoice -notin @('1', '2')) {
        Write-Host "输入无效,请输入1或2" -ForegroundColor Red
    }
} while ($menuChoice -notin @('1', '2'))

# 匹配选择的文件源
$selectedSource = if ($menuChoice -eq '1') { $sourceGB } else { $sourceAP }

# 输出结果
Write-Host "已选择文件源:$selectedSource" -ForegroundColor Green

关键说明

  • 两种方法都加入了输入验证循环,避免用户输入无效值导致脚本出错
  • 统一转换输入为大写(方法1)或限定数字输入(方法2),提升容错性
  • 最终通过$selectedSource变量传递给后续的补丁部署逻辑,无需修改原有核心代码,直接替换原来的文件源路径变量即可

内容的提问来源于stack exchange,提问作者Jeremy Patrick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 21:12:45