powershell - 提取这行文本的有效方法
问题描述
我开始了学习 power shell 的旅程,在其他人的慷慨反馈下,我终于几乎完成了我的第一个有用的脚本并根据自己的喜好对其进行了定制,我只需要一些帮助来克服最后一个障碍,我已经尝试了几种方法,似乎无法得到我想要的结果
目的
获取包含以下内容的文本文件
Set WN = CreateObject("WScript.Network")
Set Ws = CreateObject("WScript.shell")
US=WN.USERNAME
D="q,\\abc-filesvr-03\shared$,g,\\abc-ngfs-01\pingate$,s,\\abc-ngfs-01\mdis-com$,i,\\abc-civica-01\icondata$"
A=SPLIT(D,",")
DO UNTIL B>UBOUND(A)
MAPDRIVE A(B),A(B+1)
B=B+2
LOOP
Sub MapDrive(DriveLetter, UNCPath)
on error resume next
WN.RemoveNetworkDrive DriveLetter&":",1,1
on error resume next
WN.MapNetworkDrive DriveLetter&":",UNCPath
if err<>0 then
'ws.EXEC "msg.exe %username% Message from LOGON script - Error connecting drive " & DriveLetter & " to " & UNCPath & " - user is " & US& " - Further message is - " & err.description
'ER " Message from LOGON script - Error connecting drive " & DriveLetter & " to " & UNCPath & " - user is " & US& " - Further message is - " & err.description
end if
End Sub
sub er(m)
m = m & wn.computername
PF = "\\abc-ngfs-01\logons$\ERRORS"
PT = PF & "\" & US
SET OBJFSO = CREATEOBJECT("scripting.filesystemobject")
if OBJFSO.FOLDEREXISTS(PF) THEN
F = 1
DO UNTIL NOT OBJFSO.FILEEXISTS(PT & F)
F = F + 1
LOOP
OBJFSO.CREATETEXTFILE(PT & F)
set fl = OBJFSO.opentextfile(PT & F,8)
fl.write(DATE & "[]" & TIME & "[]" & us & "[]" & m & "[][][]")
fl.close
set fl = nothing
END IF
end sub
提取包含驱动器的部分(如下),并将其存储在变量中以备后用
D="q,\\abc-filesvr-03\shared$,g,\\abc-ngfs-01\pingate$,s,\\abc-ngfs-01\mdis-com$,i,\\abc-civica-01\icondata$"
根据使用的文本文件,驱动器名称会改变,所以我不能预先指定这个
我已经尝试过尝试使用文件过滤器,它有一个要排除的单词或符号列表 - 似乎没有得到任何结果
例如
$inputfile = "C:\users\admin\desktop\inputfile.txt"
$outputfile = "C:\users\admin\desktop\outputfile.txt"
$filterFile = "C:\users\admin\desktop\filterFile.txt"
$filters = Get-Content $filterFile
Get-Content $inputFile | Select-String -pattern $filters -notMatch | Out-File $outputFile
只是想知道是否有人能想到一种有效的方法来使用,因为我觉得我已经过度复杂化了?
解决方案
对于单个输入文件,您可以使用:
$inputfile = "C:\users\admin\desktop\inputfile.txt"
$drives = (Select-String -Path $inputfile -Pattern '(D=.+)').Line
结果
$drives --> D="q,\\abc-filesvr-03\shared$,g,\\abc-ngfs-01\pingate$,s,\\abc-ngfs-01\mdis-com$,i,\\abc-civica-01\icondata$"
如果您希望将其拆分为具有 Driveletter 和 Path 属性的对象:
$items = $drives -replace '^D=|"' -split ','
for ($i = 0; $i -le $items.Count - 1; $i +=2) {
[PsCustomObject]@{DriveLetter = $items[$i]; Path = $items[$i + 1]}
}
结果
DriveLetter Path ----------- ---- q \\abc-filesvr-03\shared$ g \\abc-ngfs-01\pingate$ s \\abc-ngfs-01\mdis-com$ i \\abc-civica-01\icondata$
在文件夹中的一系列文件上使用它时:
$inputPath = 'C:\users\admin\desktop'
Get-ChildItem -Path $inputPath -Filter '*.txt' -File | ForEach-Object {
$drives = ($_ | Select-String -Pattern '(D=.+)').Line
if ($drives) {
$items = $drives.Trim() -replace '^D=|"' -split ','
for ($i = 0; $i -le $items.Count - 1; $i +=2) {
# add the file where this was found in the output
[PsCustomObject]@{DriveLetter = $items[$i]; Path = $items[$i + 1]; SourceFile = $_.FullName}
}
}
}
结果:
DriveLetter Path SourceFile ----------- ---- ---------- q \\abc-filesvr-03\shared$ C:\users\admin\desktop\inputfile.txt g \\abc-ngfs-01\pingate$ C:\users\admin\desktop\inputfile.txt s \\abc-ngfs-01\mdis-com$ C:\users\admin\desktop\inputfile.txt i \\abc-civica-01\icondata$ C:\users\admin\desktop\inputfile.txt q \\abc-filesvr-03\shared$ C:\users\admin\desktop\anotherfile.txt g \\abc-ngfs-01\pingate$ C:\users\admin\desktop\anotherfile.txt s \\abc-ngfs-01\mdis-com$ C:\users\admin\desktop\anotherfile.txt i \\abc-civica-01\icondata$ C:\users\admin\desktop\anotherfile.txt
如果要将其保存到新的输出 csv 文件,只需添加
| Export-Csv -Path 'C:\users\admin\desktop\outputfile.csv' -NoTypeInformation -UseCulture
在最后一个大括号之后}
希望有帮助
推荐阅读
- python - 比较主数据框和子数据框并仅基于两列值提取新行
- python - 包括前 N 行过滤器匹配
- java - startActivity(intent) 方法调用预期错误
- r - 基于R中预先存在的变量的值将值分配给新变量的迭代方法
- sql - 如何将查询的返回结果合并为一行
- git - 为什么 git fetch origin master:master 快进本地分支?
- python - 为什么 Python 程序运行次数越多越快?
- swift - 如何在按钮上设置 IBAction 操作
- c++ - 如何将无效操作数修复为二进制表达式
- scala - 运行使用 nohup 编写在文件中的 spark 结构化流应用程序 scala 代码