如何在哈希表中的数组中创建唯一条目

how to make unique entry in array in hashtabe

我在 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 只有唯一的计划??

您可以重写现有代码以使用嵌套哈希table,如下所示:

$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,脚本现在将简单地 update/set 相同的 table 已经存在的条目。

要在最后将内部值转换为数组:

foreach($key in @($planTable.PSBase.Keys)){
    $planTable[$key] = @($planTable[$key].PSBase.Keys)
}

我的完整代码在这里

$out=Get-Content C:\anil\networker\mminfo_0324_2022.txt
$ht = @{}
$arr = @()
$today = Get-Date
$outFile = $(Join-Path -Path $PSScriptRoot -ChildPath "Networker_fetb_$(get-date -UFormat '%Y-%m-%d-%H-%M-%S').csv")
"Server Nam:Overall Size in MB:DB2 Size in MB:RMAN Size in MB:File Size in MB:Groups Name:Backup Type" | Out-File -FilePath $outFile
foreach ( $line in $out){
              $arr=$line.Split(" ")
              if ( $arr[0] -ne "nwsppl300p.corpads.local"){
              $mn=$line -csplit "incr|full|manual" ### split on case sensitive on incr or full or manual
              $md=$mn[1] -split "\s{1,}"   ### split on more than one white space
              if ($line -match  '.*( backup |Clone_DR ).*') {$btype=$md[9]} else {$btype=$md[8]}
              $clientHostName,$null,$backupPlans = -split $mn[0] ### split mn[0] in three peice
              if ($line -cmatch  "incr" -and $line -cmatch  "DB2") {$bt="DB2incr"}
              if ($line -cmatch  "manual" -and $line -cmatch  "DB2") {$bt="DB2manual"}
              if ($line -cmatch  "full" -and $line -cmatch  "DB2") {$bt="DB2full"} 
              if ($line -cmatch  "incr" -and $line -cmatch  "RMAN") {$bt="RMANincr"}
              if ($line -cmatch  "manual" -and $line -cmatch  "RMAN") {$bt="RMANmanual"}
              if ($line -cmatch  "full" -and $line -cmatch  "RMAN") {$bt="RMANfull"} 
              if ($line -cmatch  "incr" -and $line -notmatch  "DB2" -and $line -notmatch  "RMAN") {$bt="Fileincr"}
              if ($line -cmatch  "manual" -and $line -notmatch  "DB2" -and $line -notmatch  "RMAN") {$bt="Filemanual"}
              if ($line -cmatch  "full" -and $line -notmatch  "DB2" -and $line -notmatch  "RMAN") {$bt="Filefull"} 
              $date =$mn[1].split(" ")[2]  #### get retention ########
              $bdate =$mn[1].split(" ")[1]  #### get backup date ########
              
              $date1str="02/23/2022"   #### start date
              $date2str="03/05/2022"   #### end date
              $date1=[Datetime]::ParseExact($date1str, 'MM/dd/yyyy', $null)   ### convert string to date format
              $date2=[Datetime]::ParseExact($date2str, 'MM/dd/yyyy', $null)   ### convert string to date format
              $newdate=[Datetime]::ParseExact($date, 'MM/dd/yyyy', $null)     ### convert string to date format
              $diff = New-TimeSpan -Start $today -end $newdate
              
              if ( $diff.Days -lt 400 ) {      #### look for one year only and between date1 and date2 ########
                   if ( $arr[12] -ne "Clone_DR") {
                        if ($arr[0] -notin $ht.keys){
                                                 $ht[$arr[0]] = @{}
                                                                                                  
                                                 if ($btype -match "DB2") {  
                                                 $ht[$arr[0]]['Db2size'] = [int64]$arr[1]   ### convert string to integer format
                                                 $ht[$arr[0]]['groups'] = @($backupPlans)  ### adding to array
                                                 $ht[$arr[0]]['type'] = @($bt)              ### adding to array
                                                  }
                                                 if ($btype -match "RMAN") { 
                                                 $ht[$arr[0]]['RMANsize'] =[int64] $arr[1]
                                                 $ht[$arr[0]]['groups'] = @($backupPlans)
                                                 $ht[$arr[0]]['type'] = @($bt)
                                                 } 
                                                 if  ($btype -notmatch "RMAN" -and $btype -notmatch "DB2" ){
                                                 $ht[$arr[0]]['Filesize'] =[int64] $arr[1]
                                                 $ht[$arr[0]]['groups'] = @($backupPlans)
                                                 $ht[$arr[0]]['type'] = @($bt)
                                                 }
                                                 
                                                 } else {
                                                         
                                                if ($btype -match "DB2" -and $arr[1] -gt $ht[$arr[0]]['Db2size'] ) {
                                                $ht[$arr[0]]['Db2size'] += [int64]$arr[1]
                                                if ($ht[$arr[0]]['groups'] -notcontains $backupplans) { 
                                                $ht[$arr[0]]['groups'] += $backupPlans
                                                $ht[$arr[0]]['type'] += $bt
                                                }
                                                }                                                                      
                                                if ($btype -match "RMAN" -and $arr[1] -gt $ht[$arr[0]]['RMANsize']) {
                                                
                                                $ht[$arr[0]]['RMANsize'] += [int64]$arr[1]
                                                if ($ht[$arr[0]]['groups'] -notcontains $backupplans ) { 
                                                $ht[$arr[0]]['groups'] += $backupPlans
                                                $ht[$arr[0]]['type'] += $bt
                                                }
                                                  } 
                                                if  ($btype -notmatch "RMAN" -and $btype -notmatch "DB2" -and $arr[1] -gt $ht[$arr[0]]['Filesize']){ 
                                                 $ht[$arr[0]]['Filesize'] += [int64]$arr[1]
                                                 if ($ht[$arr[0]]['groups'] -notcontains $backupplans) { 
                                                 $ht[$arr[0]]['groups'] += $backupPlans
                                                 $ht[$arr[0]]['type'] += $bt
                                                 }
                                                 }

                                                    }
                                                    }  ###clone_dr
                                                    }   ###less than 400
                          } ### chcking for networker server
                          } #### looping thru file
write-host "=================================In MB =============================================================================================================================="
write-host "===ServerName==============OverAllsize======DB2size===========RMANsize========FileSize========Groups=============================================BackupType=========="
write-host "====================================================================================================================================================================="

 $ht.GetEnumerator()| ForEach-Object  {
 $total = $_.value.Db2size/1024/1024 + $_.value.RMANsize/1024/1024 + $_.value.Filesize/1024/1024
 "{0}:{1}:{2}:{3}:{4}:{5}:{6}" -f $_.name,$total,$($_.value.Db2size/1024/1024),$($_.value.RMANsize/1024/1024),$($_.value.Filesize/1024/1024),$(($_.Value.groups |Sort-Object -Unique) -join ','),$(($_.Value.type |Sort-Object -Unique) -join ',') | out-file -FilePath $outfile -Append
 "{0,-25}     {1:n2}          {2:n2}            {3:n2}            {4:n2}          {5,-35}   {6,-15}" -f $_.name,$total,$($_.value.Db2size/1024/1024),$($_.value.RMANsize/1024/1024),$($_.value.Filesize/1024/1024),$(($_.Value.groups |Sort-Object -Unique) -join ','),$(($_.Value.type |Sort-Object -Unique) -join ',')}