Chain PowerShell commands safely without &&. Use try/catch, ErrorAction, and proper sequencing for reliable Windows execution.
技能名称:powershell-safe-chain
详细描述:
在 Windows PowerShell 中可靠地链式执行命令。避免使用 && 反模式。
PowerShell 与 bash 不同:
错误示例:
powershell
mkdir test && cd test && echo done
正确示例:
powershell
$ErrorActionPreference = Stop
try {
New-Item -ItemType Directory -Path test -Force
Set-Location test
Write-Host done
} catch {
Write-Error 在步骤处失败: $_
exit 1
}
powershell
powershell
$params = @{
Path = $filePath
Encoding = UTF8
Force = $true
}
Set-Content @params
| 标准 | 验证方法 |
|----------|-------------|
| 脚本中无 && | Select-String && *.ps1 返回空 |
| 设置 ErrorAction | Select-String ErrorAction *.ps1 匹配 |
| 包含 try/catch | Select-String try|catch *.ps1 匹配 |
| 路径使用 Join-Path | Select-String Join-Path *.ps1 匹配 |
在以下情况下使用:
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 powershell-safe-chain-1776287513 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 powershell-safe-chain-1776287513 技能
skillhub install powershell-safe-chain-1776287513
文件大小: 1.64 KB | 发布时间: 2026-4-16 17:47