← 返回
开发者工具 中文

PowerShell Safe Chain

Chain PowerShell commands safely without &&. Use try/catch, ErrorAction, and proper sequencing for reliable Windows execution.
安全链式执行 PowerShell 命令,避免使用 &&。利用 try/catch、ErrorAction 及合理的顺序,确保 Windows 执行的可靠性。
dalomeve
开发者工具 clawhub v1.0.0 1 版本 99848 Key: 无需
★ 0
Stars
📥 657
下载
💾 80
安装
1
版本
#latest

概述

PowerShell Safe Chain

Chain commands reliably on Windows PowerShell. No && anti-patterns.

Problem

PowerShell differs from bash:

  • && does NOT work for command chaining
  • Parameter parsing is case-insensitive but strict
  • Errors continue by default (no fail-fast)
  • Path separators vary (\ vs /)

Workflow

1. Safe Chaining Pattern

Wrong:

mkdir test && cd test && echo done

Right:

$ErrorActionPreference = 'Stop'
try {
    New-Item -ItemType Directory -Path test -Force
    Set-Location test
    Write-Host 'done'
} catch {
    Write-Error "Failed at step: $_"
    exit 1
}

2. Conditional Chaining

# If-then pattern
if (Test-Path $file) {
    Remove-Item $file
    Write-Host "Deleted"
} else {
    Write-Warning "File not found"
}

# Pipeline with error handling
Get-Process | Where-Object CPU -GT 100 | Stop-Process -WhatIf

3. Splatting for Complex Commands

$params = @{
    Path = $filePath
    Encoding = 'UTF8'
    Force = $true
}
Set-Content @params

Executable Completion Criteria

CriteriaVerification
-----------------------
No && in scriptsSelect-String '&&' *.ps1 returns nothing
ErrorAction setSelect-String 'ErrorAction' *.ps1 matches
try/catch present`Select-String 'trycatch' *.ps1` matches
Paths use Join-PathSelect-String 'Join-Path' *.ps1 matches

Privacy/Safety

  • No hardcoded credentials
  • Use [SecureString] for passwords
  • Environment variables via $env:VAR

Self-Use Trigger

Use when:

  • Writing any PowerShell script
  • Chaining 2+ commands
  • Executing file operations

Chain safely. Fail explicitly.

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-30 02:25 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

Gog

steipete
Google Workspace 命令行工具,支持 Gmail、日历、云端硬盘、通讯录、表格和文档。
★ 921 📥 185,924
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 68 📥 180,462
developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 672 📥 324,514