SuperAPI AI 工具集

powershell-windows

通用与其它

查看项目

更新于 10-15 22:09

技能介绍

用途:提供 Windows PowerShell 关键模式与陷阱规则,减少脚本因语法约定导致的问题。

主要能力与要点:

  1. 运算符语法:使用逻辑运算符时每个 cmdlet 调用需加括号,例如 if ((Test-Path "a") -or (Test-Path "b"))。
  2. 字符限制:脚本中只用 ASCII 字符,避免 Unicode/Emoji;用 [OK]、[!]、[WARN]、[i] 等替代符号。
  3. 空值检查:访问属性前先判断,如 $array -and $array.Count -gt 0、if ($text) { $text.Length }。
  4. 字符串插值:复杂表达式先存入变量再输出,如 $value = $obj.prop.sub; Write-Output "Value: $value"。
  5. 错误处理:$ErrorActionPreference 可选 Stop、Continue、SilentlyContinue;采用 Try/Catch,不在 try 中 return,用 finally 清理,在 try/catch 后返回。
  6. 文件路径:使用 Join-Path 提升跨平台安全性。
  7. 数组操作:空数组 $array = @();添加 $array += $item;$list.Add($item) | Out-Null。
  8. JSON 操作:ConvertTo-Json 须指定 -Depth,如 ConvertTo-Json -Depth 10;读取用 Get-Content "file.json" -Raw | ConvertFrom-Json,写入用 $data | ConvertTo-Json -Depth 10 | Out-File "file.json" -Encoding UTF8。
  9. 常见错误:缺少括号、含 Unicode、空对象取属性、类型不匹配等对应修复方式。
  10. 脚本模板:Set-StrictMode -Version Latest 起手,设置 $ErrorActionPreference = "Continue",用 $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path 取脚本目录,主逻辑放 try,成功 Write-Output "[OK] Done" 后 exit 0,catch 中 Write-Warning "Error: $_" 后 exit 1。

限制:资料仅列出上述模式与规则,未提供安装命令、依赖、版本兼容范围或价格信息。