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

提取文本文件/Findstr结果最后一行最后一个单词的脚本需求

Got it, let's work through these two scripting tasks—they're totally doable with either Batch or PowerShell, depending on what you're comfortable with in a Windows environment.

1. 提取文本文件最后一行的最后一个单词

Batch 实现

If you prefer using Batch scripts, here's a straightforward approach:

@echo off
:: 替换成你的目标文本文件路径
set "target_file=C:\path\to\your\file.txt"

:: 先获取文件的最后一行
for /f "delims=" %%a in ('type "%target_file%" ^| findstr /v /n "^" ^| findstr /r "^[0-9]*:$"') do set "last_line=%%a"

:: 拆分最后一行的内容,取最后一个单词
for %%b in (%last_line%) do set "last_word=%%b"

echo 提取到的最后一个单词: %last_word%

Quick note: This assumes words are separated by spaces. If your file uses a different delimiter (like commas), you'll need to tweak the second for loop with the delims parameter.

PowerShell 实现

For a cleaner, more readable option, PowerShell is great here:

# 替换成你的目标文本文件路径
$filePath = "C:\path\to\your\file.txt"

# 读取文件最后一行,按空格拆分后取最后一个元素
$lastWord = (Get-Content $filePath | Select-Object -Last 1) -split '\s+' | Select-Object -Last 1

Write-Host "提取到的最后一个单词: $lastWord"

The \s+ regex handles multiple spaces between words, so it works even if there are gaps.

2. 提取findstr结果最后一行的最后一个单词并执行SCCM RemoteControl

This is similar to the first task, but we're capturing output from findstr instead of a static file. Let's cover both scripting languages again:

Batch 实现

@echo off
:: 替换成你的findstr搜索条件和源文件
set "search_term=your_target_string"
set "source_file=C:\path\to\source\file.txt"

:: 遍历findstr的每一行,最后一次循环会保留最后一行内容
for /f "delims=" %%a in ('findstr "%search_term%" "%source_file%"') do set "last_result_line=%%a"

:: 拆分最后一行,取末尾的计算机名
for %%b in (%last_result_line%) do set "computername=%%b"

:: 执行SCCM远程控制命令
echo 正在启动对 %computername% 的远程控制...
SCCM RemoteControl %computername%

If your findstr output uses a non-space delimiter (like a colon or hyphen) for the computer name, adjust the second for loop with delims—for example, for /f "tokens=* delims=:" %%b in ("%last_result_line%") do set "computername=%%b" if the name comes after a colon.

PowerShell 实现

# 替换成你的findstr搜索条件和源文件
$searchTerm = "your_target_string"
$sourceFile = "C:\path\to\source\file.txt"

# 获取findstr输出的最后一行,拆分后取末尾的计算机名
$computername = (findstr $searchTerm $sourceFile | Select-Object -Last 1) -split '\s+' | Select-Object -Last 1

# 执行SCCM远程控制命令
Write-Host "正在启动对 $computername 的远程控制..."
& "SCCM RemoteControl" $computername

This will reliably grab that trailing computer name (like B0K9VY1) and pass it straight to the SCCM command.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:09:17