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

PowerShell自动化脚本:用户输入与相对路径压缩配置问题

解决PowerShell脚本的输入验证与相对路径问题

核心改进方向

  • 强化输入校验:循环确保版本号格式合规,验证路径有效性、输入非空
  • 替换硬编码路径:基于用户输入动态拼接路径,原生支持相对路径
  • 关联用户指定的ZIP路径:将压缩目标直接指向用户输入的$zipFile

改进后的完整脚本

# 版本号验证:循环直到输入7位长度内容
do {
    $versionNumber = Read-Host -Prompt "Version Number (must be 7 characters)"
    if ($versionNumber.Length -ne 7) {
        Write-Host "Error: Version number must be exactly 7 characters. Please try again." -ForegroundColor Red
    }
} while ($versionNumber.Length -ne 7)

# 包名验证:确保输入非空
do {
    $packageName = Read-Host -Prompt "Package Name"
    if ([string]::IsNullOrWhiteSpace($packageName)) {
        Write-Host "Error: Package name cannot be empty. Please try again." -ForegroundColor Red
    }
} while ([string]::IsNullOrWhiteSpace($packageName))

$newPackage = "$packageName.$versionNumber"

# 验证新目录路径存在
do {
    $newDirectory = Read-Host -Prompt "Path to new directory (supports relative paths)"
    if (-not (Test-Path -Path $newDirectory -PathType Container)) {
        Write-Host "Error: Directory does not exist. Please enter a valid path." -ForegroundColor Red
    }
} while (-not (Test-Path -Path $newDirectory -PathType Container))

# 验证Deploy.ps1文件存在
do {
    $deployPath = Read-Host -Prompt "Path to Deploy.ps1 file (supports relative paths)"
    if (-not (Test-Path -Path $deployPath -PathType Leaf)) {
        Write-Host "Error: Deploy.ps1 file not found. Please enter a valid file path." -ForegroundColor Red
    }
} while (-not (Test-Path -Path $deployPath -PathType Leaf))

# 验证ZIP路径的父目录存在(避免压缩时路径错误)
do {
    $zipFile = Read-Host -Prompt "Path for new zip file (supports relative paths, e.g., ./output.zip)"
    $zipParentDir = Split-Path -Path $zipFile -Parent
    if ([string]::IsNullOrWhiteSpace($zipParentDir) -or (Test-Path -Path $zipParentDir -PathType Container)) {
        break
    } else {
        Write-Host "Error: Parent directory for ZIP file does not exist. Please enter a valid path." -ForegroundColor Red
    }
} while ($true)

# 拼接新包目录路径(自动兼容相对路径)
$newItemPath = Join-Path -Path $newDirectory -ChildPath $newPackage

# 创建新目录(若不存在)
if (-not (Test-Path -Path $newItemPath -PathType Container)) {
    New-Item -Path $newItemPath -ItemType Directory | Out-Null
    Write-Host "Created directory: $newItemPath" -ForegroundColor Green
}

# 复制Deploy.ps1到新包目录
Copy-Item -Path $deployPath -Destination $newItemPath -Force
Write-Host "Copied Deploy.ps1 to: $newItemPath" -ForegroundColor Green

# 使用用户指定路径压缩目录,支持相对路径
Microsoft.PowerShell.Archive\Compress-Archive -Path $newItemPath -DestinationPath $zipFile -Force
Write-Host "Successfully created ZIP file: $zipFile" -ForegroundColor Green

关键细节说明

  • 输入校验:所有输入加入循环校验,避免格式错误或无效路径导致后续执行异常
  • 相对路径支持:借助Join-Path拼接路径,PowerShell会自动关联当前工作目录,无需硬编码绝对路径
  • ZIP路径处理:提前校验ZIP文件父目录的存在性,直接使用用户输入的$zipFile作为压缩目标
  • 路径安全:用Test-Path区分目录/文件类型,避免因输入错误路径类型引发报错

内容的提问来源于stack exchange,提问作者Sabrina Grosse

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 14:10:18