SuperAPI AI 工具集

powershell-windows

通用与其它

查看项目

更新于 10-16 07:24

技能介绍

用途:为在 Windows 上编写 PowerShell 脚本提供关键模式与易错点参考,帮助避免语法与运行时问题。

主要能力:

  1. 运算符语法:使用逻辑运算符时,每个 cmdlet 调用必须加括号,例如 if ((Test-Path "a") -or (Test-Path "b"))。
  2. 字符限制:脚本中只用 ASCII,避免 Unicode/Emoji,用 [OK]、[!]、[*]、[i]、[...] 等替代符号。
  3. 空值检查:访问属性前先判空,如 $array -and $array.Count -gt 0、if ($text) { $text.Length }。
  4. 字符串插值:复杂表达式先存入变量再输出,避免直接内嵌多层属性。
  5. 错误处理:ErrorActionPreference 可选 Stop(开发快速失败)、Continue(生产脚本)、SilentlyContinue(预期错误);try/catch 中不要在 try 内 return,用 finally 清理,在 try/catch 之后返回。
  6. 文件路径:用 Join-Path 拼接,如 Join-Path $env:USERPROFILE "file.txt"、Join-Path $ScriptDir "data"。
  7. 数组操作:空数组 $array = @(),添加项 $array += $item,ArrayList 用 $list.Add($item)。
  8. JSON:ConvertTo-Json 始终指定 -Depth,如 -Depth 10;读取用 Get-Content "file.json" -Raw。
  9. 常见错误对照:缺括号报 parameter 'or';Unicode 报 Unexpected token;空对象报 Cannot find property;类型不匹配报 Cannot convert,可用 .ToString()。
  10. 脚本模板:Set-StrictMode -Version Latest、设置 $ErrorActionPreference、用 Split-Path -Parent $MyInvocation.MyCommand.Path 取脚本目录,主逻辑放 try 中,catch 用 Write-Warning 并 exit 1。

使用方式:作为编写或审查 PowerShell 脚本时的检查清单与模板参考。

适用场景:Windows 自动化脚本、运维任务、批处理与配置管理脚本开发。

限制:资料未提供具体版本兼容范围、安装方式或额外工具依赖,暂未提供。