#1 10 Oct 2016 17:10

Axachi
Member
Registered: 10 Oct 2016
Posts: 1

Need help on a script. Get args from a file then use them in a foreach

Here is what I want it to do.
First of Great ob you guys are doing helping out.

I am fairly new to PS..

I have a script to get all the Address Objects in a firewall out of a saved Config from a Firewall. I have cleaned the file and removed stale enteries and we are now build up a new firewall from scratch to not Export import is not working as the FW are different model's.. sad
The File is built like this.
addrObjId_1=SomeNameA
adrrObjType_1=8
adrrObjZone_1=ZONE_A
addrObjIP_1=0.0.0.0
addrObjIP2_1=0.0.0.0
addrObjId_1=SomeNameB
adrrObjType_1=8
adrrObjZone_1=ZONE_B
addrObjIP_1=0.0.0.0
addrObjIP2_1=0.0.0.0
paramGmsMode=0
gmsSyslogToDrs=off
gmsDrsIpName=
gmsDrsPort=514
syslogFormat=0
logPrefs_logEmailFreq=2

for now I am only interested in getting the addrXX objects in the file.

This is the script I use so far..

ForEach ($addrId in GC 'C:\temp\Scripts\Sonicwall.txt' | Where-Object {$_ -like '*addrObjId_*=*' -or $_ -like '*addrObjType_*=*' -or $_ -like '*addrObjZone_*=*' -or $_ -like '*addrObjIP1_*=*' -or $_ -like '*addrObjIP2_*=*'}) {

       $addrId.split('=')[1] | out-file -Append 'C:\temp\Scripts\Sonicwall-AddressObjects.txt'
       

    }

After I get this data out I have cleaned it up and and removed many entries I only took out addrObjId. AS It was easier to filter true and get rid of old crap...
I now need to Generate a new File but now with the rest of the data addrObjType etc. So is there a way to read the file with only the names and then do a for loop and get the rest of the data..ending up with a file with commands I can import back into my Firewall tongue

Thx.

Offline

#2 12 Oct 2016 18:32

XPlantefeve
New Member
Registered: 12 Oct 2016
Posts: 1

Re: Need help on a script. Get args from a file then use them in a foreach

I gave it a quick look, here's how I would approach it:

$file = Get-Content .\test.txt -Raw
$file -split 'addrObjId_1' | % {
    if ($_.Length) {
        $record = 'addrObjId_1' + $_
        $objectrecord = New-Object -TypeName psobject -Property (ConvertFrom-StringData $record) | select addrObjId_1,adrrObjType_1,addrObjIP2_1,addrObjIP_1,adrrObjZone_1
        $objectrecord
    }
}

I use Get-Content with the -raw argument so as to load the file in a multi-line string (rather than the default array of strings). I then split this multi-line string at every 'addrObjId_1'. Each resultant string will look like:

=SomeNameA
adrrObjType_1=8
adrrObjZone_1=ZONE_A
addrObjIP_1=0.0.0.0
addrObjIP2_1=0.0.0.0

I test $_.Length because splitting the string will give a first item that is empty (what was before the first addrObjId_1, IE nothing). For each other item, I add back the 'addrObjId_1' (removed by the split, as considered as the separator). Then I create a new object from it (convertfrom-stringdata will make an array out of it, new-object creates an object from that array) and I select only what I want from it (as the last record will get all the last lines that don't interest you).

You end up with each record turned into an object that you can use as you wish.

Offline

Board footer

Powered by FluxBB