ShredOS+Powershell 处理格式化日志
ShredOS
需求: 格式化旧硬盘并整理文件统计数量 Github link
一款开源的硬盘格式化处理软件,可以用来对 PC/Laptop/服务器等带硬盘的机器进行格式化处理,
但目前无法对 HP 服务器进行很好的兼容,无法打开镜像,详情见 github issue 页面
镜像制作过程可以参考下 github 说明,此处不作赘述
TODO: 补充对硬盘的处理,具体怎么将配置固定
v0.37 版本新增了对默认方式的修改,具体可以修改镜像中的 grub.cf 文件,参数说明
参考如下
|
|
Powershell
CMD 调用 Powershell 文件
powershell -ExecutionPolicy Bypass -File xx.ps1
思路:
- 打开 ShredOS 格式化处理后的 TXT 文件
- 按照固定行列索引的方式进行定位,找到长度为 14 的硬盘序列号,接着找到 PC 的 S/N 号码
- 在目录下查找到包含硬盘序列号的 PDF 文件
- 重命名本 TXT 文件以及找到的 PDF 文件,加上 PC 的 S/N 号码
展开代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 设置要查找的目录路径
$directoryPath = "Path"
# 获取所有 TXT 文件
$txtFiles = Get-ChildItem -Path $directoryPath -Filter *.txt
# 循环遍历每个文件
foreach ($file in $txtFiles) {
# 输出文件名和完整路径
Write-Output "文件名: $($file.Name)"
#Write-Output "完整路径: $($file.FullName)"
# 读取文件内容
$content = Get-Content -Path $file.FullName
# 检查文件是否包含至少 17 行
if ($content.Length -ge 17) {
# 获取第 17 行
$line = $content[16] # 索引从 0 开始
# 检查行长度是否足够
if ($line.Length -ge 116) { # 103 列是索引 102
# 复制从第 103 列开始的 14 位字符
$result = $line.Substring(102, 14)
Write-Output $result
} else {
Write-Output "第 17 行字符长度不足。"
}
} else {
Write-Output "文件行数不足 17 行。"
}
# 检查文件是否包含至少 25 行
if ($content.Length -ge 25) {
# 获取第 25 行
$line = $content[24] # 索引从 0 开始
# 检查行长度是否足够
if ($line.Length -ge 62) { # 63 列是索引 62
# 复制从第 55 列开始的 8 位字符
$result2 = $line.Substring(54, 8)
Write-Output $result2
} else {
Write-Output "第 25 行字符长度不足。"
}
} else {
Write-Output "文件行数不足 25 行。"
}
# 查找文件名包含指定字符串的文件
$matchingFiles = Get-ChildItem -Path $directoryPath -File | Where-Object { $_.Name -like "*$result*" }
# 输出匹配的文件
if ($matchingFiles) {
foreach ($file in $matchingFiles) {
Write-Output "找到文件: $($file.FullName)"
}
} else {
Write-Output "未找到"
}
$newName = $result2 -join "_" -join $matchingFiles
$newName2 = $result2 -join "_" -join $file.Name
Write-Output $newName
Write-Output $newName2
#Rename-Item $matchingFiles $newName
#Rename-Item $file.Name $newName2
}
-
将 ps1 文件放在文件夹同个目录下进行获取当前路径
-
将固定行列序号改为正则匹配,规则为“S/N=”后换行符前的字符串以及“system-serial-number =”后的 8 位字符
-
用 array 对 textfile 进行追加统计无法找到的 pdf 数量
目前存在问题:展开代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81# 设置要查找的目录路径 $directoryPath = Get-Location # 获取所有 TXT 文件 $txtFiles = Get-ChildItem -Path $directoryPath -Filter *.txt #$regex = [regex] "S/N=(.*?)(\r?\n)" $regex = 'S/N=([^ \n]+)' $regex2 = [regex] "system-serial-number = (\S{8})" $array = @("以下txt文件找不到对于PDF:") # 1.循环遍历每个文件 foreach ($file in $txtFiles) { Write-Output "---------------------------------------------------------" # 输出文件名和完整路径 #Write-Output "file.Name: $($file.Name)" if ($file.Name){ $textfile = $file.Name #Write-Output $textfile } # 2.读取文件内容 $content = Get-Content -Path $file.FullName -ErrorAction SilentlyContinue # 3.查找硬盘号 #$match = $regex.Match($content) $matches = [regex]::Matches($content, $regex) if ($matches.Count -ge 2) { # 获取第二个匹配项 $diskNumber = $matches[1].Groups[1].Value.Trim() #Write-Output "第二个 S/N: $diskNumber" } else { Write-Output "未找到第二个 S/N" } # 3.查找所有包含主机名 $match2 = $regex2.Match($content) if ($match2.Success) { $pcNumber = $match2.Groups[1].Value #Write-Output "pcNumber: $pcNumber" } # 4.查找文件名包含指定字符串的文件 $matchingFiles = Get-ChildItem -Path $directoryPath -File | Where-Object { $_.Name -like "*$diskNumber*" } #Write-Output $matchingFiles # 5.输出匹配的文件 if ($matchingFiles) { foreach ($file in $matchingFiles) { try{ #Write-Output "file.FullName: $($file.FullName)" $newName = $pcNumber + "_"+ $matchingFiles #Write-Output $newName Rename-Item $matchingFiles $newName -ErrorAction Stop #Write-Output "textName: $($textfile)" $newName2 = $pcNumber+ "_" + $textfile #Write-Output $newName2 Rename-Item $textfile $newName2 -ErrorAction Stop Write-Output "rename" Write-Output "---------------------------------------------------------" }catch { Write-Output "$pcNumber 重命名文件时出错: $_" } } }else { Write-Output "can not found PDF" $array += $textfile Write-Output "---------------------------------------------------------" } } Write-Output $array -
当文件出现重复名称的时候无法进行下一步处理
-
无法统计改名失败的文件
-
更多的容错判断