我设计了一个脚本来放置与特定字符串匹配的数据。文本文件中有数十万个机器名。现在我已经拥有了所有的数据,我需要查看这个列表,并以某种方式设置一些类似于Idex=LastIndex+6的东西。如果两行等于6,我想要导出计算机名,并将它们放在两个不同的csv/excel文件中的两列中。如果这两个数字之间的差不是6,那么忽略它。
这是我现在收集信息的代码。我只是不知道怎么算。
$Servers = Get-Content c:\temp\dad\servers.txt
foreach ($Server in $servers)
{
Get-ChildItem -Path \\$server\image_store$\Win764\DADLOGS -recurse -Filter ADReport.ini | Get-Content | Select-String -CaseSensitive -pattern "\bName=\b" |
Export-Csv c:\scripts\test.csv -Force -Append -NoTypeInformation
$intRow = $intRow + 1
}下面是我得到的结果:一些在文件中的下一个数字之前有一个6的计数,而另一些则不会。
110名称=042LOAN-TPL-Y
161 Name=052 052SHARE TPL-K
212 Name=052 052SHARE TPL-ZZ
258 Name=NYCMTENTESTING
38559 Name=gert34t-TPD-Z
38604 Name=njytyety-XPU
38610 Name=jnrthe-TPL-AD
38655 Name=dger54-XPU
38661 Name=ertydffj-TPL-AE
38706 Name=5
38712 Name=nfhjtre-TPL-AG
发布于 2014-06-14 02:19:22
如果我正确理解这个问题,我认为这至少是解决方案的一部分:
$Data =
(@'
110 Name=042LOAN-TPL-Y
161 Name=052SHARE-TPL-K
212 Name=052SHARE-TPL-ZZ
258 Name=NYCMTENTESTING
38559 Name=gert34t-TPD-Z
38604 Name=njytyety-XPU
38610 Name=jnrthe-TPL-AD
38655 Name=dger54-XPU
38661 Name=ertydffj-TPL-AE
38706 Name=5erteefg-XPU
38712 Name=nfhjtre-TPL-AG
'@).split("`n") |
foreach {$_.trim()}
$Last = '0'
$Keep =
$Data |
foreach {
if ( 6 -eq $_.split()[0] - $last.split()[0] )
{
,($Last.split()[1],$_.split()[1] -replace 'Name=')
$last ='0'
}
else { $last = $_ }
}$keep将是一个数组,每个二级数组中有两个计算机名.
同样的基本想法,适用于进口/出口csv (未经测试):
$InputFile = 'c:\somedir\somefile.csv'
$OutputFile = 'c:\somedir\clean.csv'
$NullRecord = '' | Select Index,ComputerName
$Last = $NullRecord.psobject.copy()
Import-Csv $InputFile |
foreach {
if ( 6 -eq $_.Index - $last.Index )
{
New-Object PSObject -Property @{
OldMachine = $Last.ComputerName -replace 'Name='
NewMachine = $_.ComputerName -replace 'Name='
}
$Last = $NullRecord.psobject.copy()
}
else { $Last = $_.psobject.copy() }
} | Export-Csv $OutputFile -NoTypeInformation发布于 2014-06-14 01:42:02
我认为这会满足你的要求,但它可能不能给你最好的表现。
$Servers = Get-Content c:\temp\dad\servers.txt
$indexes = @()
$matches = @()
$Servers | % {
$currentIndex = $_.Split(" ")[0]
if ($currentIndex-6 -in $indexes) {
$matches += $currentIndex-6
$matches += $currentIndex
}
$indexes += $currentIndex
}
"`"Index`", `"Name`"" | Out-File c:\scripts\test.csv -Force
$Servers | % {
$currentIndex = $_.Split(" ")[0]
if ($currentIndex -in $matches) {
"$currentIndex, `"$name`"" | Out-File c:\scripts\test.csv -Force -Append
}
}这是假设您希望同时记录新旧计算机,但是您可以很容易地通过删除$matches += ...行来修改它。
https://stackoverflow.com/questions/24214438
复制相似问题