批量远程卸载CrowdStrike遇双重问题求助
优化方案与问题排查
一、解决重复输入凭据的问题
将凭据获取逻辑移至循环外部,仅需输入一次即可复用:
# 仅在脚本启动时获取一次管理员凭据 $credential = Get-Credential $servers = Get-Content 'C:\Users\PP\Desktop\CrowdStrike_Automation\servers.txt' $maintenance_token = Get-Content 'C:\Users\PP\Desktop\CrowdStrike_Automation\maintenance_key.txt' foreach ($server in $servers) { Invoke-Command -ComputerName $server -Credential $credential -ScriptBlock { & "C:\Temp\CrowdStrike_Automation\Setup\CsUninstallTool.exe" MAINTENANCE_TOKEN=$using:maintenance_token /quiet } }
二、优化批量执行效率(复用远程会话)
使用New-PSSession创建持久化远程会话,避免重复建立连接,提升批量执行的稳定性与效率:
$credential = Get-Credential # 建议用CSV存储服务器与对应专属维护码(主机专属维护码需一一对应) $serverTokenMap = Import-Csv 'C:\Users\PP\Desktop\CrowdStrike_Automation\server_maintenance_tokens.csv' # 批量创建远程会话 $sessions = New-PSSession -ComputerName $serverTokenMap.ServerName -Credential $credential foreach ($entry in $serverTokenMap) { $targetSession = Get-PSSession -ComputerName $entry.ServerName Invoke-Command -Session $targetSession -ScriptBlock { & "C:\Temp\CrowdStrike_Automation\Setup\CsUninstallTool.exe" MAINTENANCE_TOKEN=$using:entry.MaintenanceToken /quiet } } # 执行完成后清理会话 Remove-PSSession $sessions
三、批量卸载失败的关键排查点
- 维护码不匹配:你提到维护码是主机专属的,原脚本所有服务器共用同一个维护码会导致卸载失败。需用CSV建立服务器与维护码的一一对应关系,示例CSV格式:
ServerName,MaintenanceToken Server01,XYZ-123-ABC Server02,DEF-456-GHI - 远程工具路径不存在:
Invoke-Command在远程机器执行命令,需确保远程机器的C:\Temp\CrowdStrike_Automation\Setup\路径下存在CsUninstallTool.exe。若不存在,可提前批量复制:$localToolPath = 'C:\Users\PP\Desktop\CrowdStrike_Automation\Setup\CsUninstallTool.exe' foreach ($server in $servers) { $remoteDir = "\\$server\C$\Temp\CrowdStrike_Automation\Setup\" if (-not (Test-Path $remoteDir)) { New-Item $remoteDir -ItemType Directory -Force } Copy-Item $localToolPath -Destination $remoteDir -Force } - 权限与执行策略:确保你的账号在所有远程服务器拥有管理员权限,同时远程机器的PowerShell执行策略需允许运行脚本(可通过
Invoke-Command远程设置:Set-ExecutionPolicy RemoteSigned -Scope LocalMachine -Force)。 - 静默参数有效性:验证
CsUninstallTool.exe的静默卸载参数,部分工具可能支持/silent而非/quiet,可在单台机器测试确认。
内容的提问来源于stack exchange,提问作者PPL
相关产品推荐
相关产品推荐

