powershell-windows
掌握 PowerShell Windows 脚本模式
避免导致脚本失败的常见 PowerShell 陷阱。学习正确的运算符语法、错误处理和 Windows 特定模式,实现可靠的自动化。
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“powershell-windows”。 检查文件或目录是否存在
预期结果:
使用:if ((Test-Path "file.txt") -or (Test-Path "folder")) - 使用逻辑运算符时,每个 cmdlet 必须用括号包裹
正在使用“powershell-windows”。 安全获取数组长度
预期结果:
使用:if ($array -and $array.Count -gt 0) - 访问属性前始终验证数组是否存在
正在使用“powershell-windows”。 将嵌套对象转换为 JSON
预期结果:
使用:$data | ConvertTo-Json -Depth 10 - 转换嵌套对象时始终指定 depth 参数以避免截断
安全审计
安全Static analysis detected 34 patterns but all are false positives. The SKILL.md file is documentation only, containing PowerShell syntax examples and best practices. No executable code or security risks present. The skill is a reference guide for proper PowerShell scripting patterns on Windows.
风险因素
质量评分
你能构建什么
DevOps 工程师脚本编写
为 CI/CD 流水线创建具有适当错误处理和空值安全的可靠 Windows 自动化脚本
系统管理员参考
维护遗留脚本时的 PowerShell 语法规则和常见陷阱快速参考
开发者学习资源
学习 PowerShell 最佳实践,在将 Windows 脚本添加到您的工具箱时避免常见错误
试试这些提示
检查文件是否存在 OR 其他条件为真的正确 PowerShell 语法是什么?
展示需要清理资源的 PowerShell 脚本的正确 try-catch-finally 模式
在 PowerShell 中访问数组的 Count 属性之前,如何安全地检查数组是否有元素?
在 PowerShell 中读写包含嵌套对象的 JSON 文件的正确方法是什么?
最佳实践
- 使用 -or 和 -and 等逻辑运算符时,将每个 cmdlet 包裹在括号中
- 访问对象属性或方法前始终检查空值
- 使用 Join-Path 构建文件路径而不是字符串拼接
- 根据您的环境适当设置 ErrorActionPreference(开发环境用 Stop,生产环境用 Continue)
- 转换嵌套对象为 JSON 时指定 -Depth 参数
避免
- 在 PowerShell 脚本中使用 unicode 或表情符号 - 使用 ASCII 标记如 [OK] 代替
- 访问属性时不进行空值检查 - 会在空值上导致运行时错误
- 使用 ConvertTo-Json 时不带 -Depth 参数 - 嵌套对象会被截断
- 在 try 块中放置 return 语句 - 使用 finally 进行清理并在之后 return