0

Hi I need some help getting this to work :) I have a file with names in a list. FILE A.

SomeNameA
SomeNameB
SomeNameC
etc

Now I need to get a set of lines from FILE B:

ddrObjId_1=SomeNameA
adrrObjType_1=8
adrrObjZone_1=ZONE_A
addrObjIP_1=0.0.0.0
addrObjIP2_1=0.0.0.0
addrObjId_2=SomeNameB
adrrObjType_2=8
adrrObjZone_2=ZONE_B
addrObjIP_2=0.0.0.0
addrObjIP2_2=0.0.0.0

starting with the line with a value which matches one of the names in FILE A, and all subsequent lines until either another match is found of the end of the file. Hope this explains it better :)

So when I'm finished I need a new FILE C to be generated with the data:

address-object SomeNameA
type 1
host 0.0.0.0
mask 0.0.0.0
zone ZONE_A    
address-object SomeNameB
type 8
host 0.0.0.0
mask 0.0.0.0
zone ZONE_B
etc

So I have been trying some different scripts found her on StackO, and I can now scan the file for a word from my file and it gets the data, so i need it to get the values and then make a new file witch holds the commands. etc.. Here is the code not Working but it's a start. Hope someone can make sens of things for me..

Clear-Host
$array = @('SomeNameA')


$found = @{}
Get-Content 'D:\Scripts\FileB.txt'| % {
  $line = $_
  foreach ($item in $array) {
    if ($line.Split('=')[1] -like $item) { $found[$item] = $true }
        foreach ($item in $line | Where-Object{$_ -like 'addrObjId_*=*' -or $_ -like 'addrObjType_*=*'}) {
        write-host $item
        }
  }
}

$found.Keys | Out-File "C:\results.txt"

2 Answers2

0

this should do the job:

$names = cat '.\fileA.txt'
$data =  cat '.\fileB.txt'
$outfile = '.\fileC.txt'

rm -Force $outfile

foreach($d in ([string]$data -split 'addrObjId_\d+='))
{
    if ($d -eq "") { continue }

    $d = $d -split ' '

    $obj = $d[0].Trim()
    $type = ($d[1] -split "=")[1]
    $zone = ($d[2] -split "=")[1]
    $hostip = ($d[3] -split "=")[1]
    $maskip = ($d[4] -split "=")[1]   

    if ($names.Contains($obj))
    {
        "address-object $obj" | Out-File -Append -FilePath $outfile
        "type $type" | Out-File -Append -FilePath $outfile
        "host $hostip" | Out-File -Append -FilePath $outfile
        "mask $maskip" | Out-File -Append -FilePath $outfile
        "zone $zone" | Out-File -Append -FilePath $outfile
    }
}
Maximilian Etti
  • 230
  • 1
  • 9
0

Her is the code I used in the end :) Thx

metix

Clear-Host
$names = cat 'FileA.txt'
$data =  cat 'FileB.txt'
$outfile = 'C:\Temp\output.txt'
if(test-path $outfile){Remove-Item $outfile}

foreach($d in ([string]$data -split 'addrObjId_\d+='))
{
    if ($d -eq "") { continue }

    $d = $d -split ' '

    $obj = $d[0].Trim()
    $type = ($d[2] -split "=")[1]
    $zone = ($d[3] -split "=")[1]
    $hostip1 = ($d[5] -split "=")[1]
    $hostip2 = ($d[6] -split "=")[1]     

    if ($names.Contains($obj))
    {
        "address-object ipv4 $obj" | Out-File -Append -FilePath $outfile
        if ($type -eq '4'){"network $hostip1 $hostip2" | Out-File -Append -FilePath $outfile}elseif($type -eq '1'){"host $hostip1" | Out-File -Append -FilePath $outfile}elseif($type -eq '2'){"range $hostip1 $hostip2" | Out-File -Append -FilePath $outfile}
        "zone $zone" | Out-File -Append -FilePath $outfile
    }
}