You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从TXT读取服务器列表检查CrowdStrike状态并导出CSV的脚本问题

脚本问题分析与修正

原脚本存在几个关键问题,导致运行出错或结果无效:

  • 未指定远程计算机:Get-Service默认仅查询本地服务,循环中的$Computer变量完全没用到,等于反复查本机状态
  • 无服务器标识:导出的CSV只有Status列,无法对应到具体服务器
  • 缺失错误处理:远程访问服务器时,遇到权限不足、机器离线等情况会直接报错,且不会记录失败信息

修正后的PowerShell脚本

# 读取服务器列表
$Computers = Get-Content -Path C:\temp\servers.txt
# 初始化结果集合
$results = @()

foreach ($Computer in $Computers) {
    try {
        # 查询远程服务器的CrowdStrike服务状态
        $service = Get-Service -DisplayName "Crowdstrike Falcon Sensor Service" -ComputerName $Computer -ErrorAction Stop
        # 构造结果对象,包含服务器名和状态
        $results += [PSCustomObject]@{
            ComputerName = $Computer
            ServiceStatus = $service.Status
            Status = "Success"
        }
    }
    catch {
        # 记录访问失败的服务器及错误原因
        $results += [PSCustomObject]@{
            ComputerName = $Computer
            ServiceStatus = $null
            Status = "Failed"
            ErrorMessage = $_.Exception.Message
        }
    }
}

# 将所有结果导出到CSV
$results | Export-Csv -Path C:\temp\results.csv -NoTypeInformation

关键改进点说明

  • 指定远程目标:通过-ComputerName $Computer参数明确要查询的远程服务器
  • 保留服务器标识:结果中加入ComputerName列,确保每一条状态都能对应到具体机器
  • 错误捕获:用try/catch捕获远程访问异常,记录失败原因,避免脚本中断
  • 批量导出:先将所有结果存入集合,最后一次性导出到CSV,比循环中用-Append更高效且避免文件锁问题

内容的提问来源于stack exchange,提问作者Andeng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 16:13:16