我在 test1.txt 中有以下行
bpdppa301p.corpads.local 86929680 JC_UNIX_FS01_INCR JC_FS UNIX_FS01_INCR incr 02/24/2022 03/30/2022 03/30/2022 disk 1645678800 backup 84 MB /dbwsrp01/dbwsrp01 JCBDD1300P.corpads.local
bpdppa302p.corpads.local 82578060 MN_DB_ORA manual 02/24/2022 04/21/2022 03/31/2022 disk 1645678854 80 MB RMAN:PFDPRE01_jf0mjj9l_1_1 MNBDD3302P.corpads.local
bpdppa301p.corpads.local 86929880 JC_UNIX_FS01_INCR JC_FS UNIX_FS01_INCR incr 02/24/2022 03/30/2022 03/30/2022 disk 1645678800 backup 84 MB /dbwsrp01/dbwsrp01 JCBDD1300P.corpads.local
我喜欢在数字之后但在单词(incr 或 manual)之前获取唯一名称。所以我使用了下面的代码。
$backupLogLines = @(Get-Content C:\scripts\test1.txt) -match "incr|full|manual"
$planTable = @{}
foreach ($line in $backupLogLines)
{
$metadataParts = $line -csplit "incr|full|manual"
# first string is host name,
# second string is discarded,
# ... and the rest are backup plans
$clientHostName,$null,$backupPlans = -split $metadataParts[0]
if(-not $planTable.Contains($clientHostName)){
# Create new array containing the backup plans for the current
$planTable[$clientHostName] = @($backupPlans)
}
else {
$planTable[$clientHostName] += $backupPlans
}
}
但 $backupPlans 也可能有重复项。如何避免这样 hashTable 只有独特的计划?
您可以重写现有代码以使用嵌套哈希表,如下所示:
$backupLogLines = @(Get-Content C:\scripts\test1.txt) -match "incr|full|manual"
$planTable = @{}
foreach ($line in $backupLogLines)
{
$metadataParts = $line -csplit "incr|full|manual"
# first string is host name,
# second string is discarded,
# ... and the rest are backup plans
$clientHostName,$null,$backupPlans = -split $metadataParts[0]
if(-not $planTable.Contains($clientHostName)){
# Create new inner hashtable to hold observed backup plans
$planTable[$clientHostName] = @{}
}
foreach($plan in $backupPlans){
$planTable[$clientHostName][$plan] = $true
}
}
如果对同$plan
一个客户端多次观察到相同的情况,脚本现在将简单地更新/设置已经存在的同一个表条目。
最后将内部值转换为数组:
foreach($key in @($planTable.PSBase.Keys)){
$planTable[$key] = @($planTable[$key].PSBase.Keys)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句