Save-Rules-A10.ps1

<#PSScriptInfo
   
.VERSION 1.0.0
   
.GUID 8e38f4d8-70a4-479e-90d9-8e87fe160c09
   
.AUTHOR Felipe Fuentes Milosavljevic - ffuentes3003@gmail.com
   
.COMPANYNAME Felipe Fuentes
   
.COPYRIGHT (c) 2020 Felipe Fuentes. All rights reserved.
   
.TAGS Get Rules A10, A10, Axapi/v3, Virtual Server, Service Group, Members
  
#>


<#
   
.DESCRIPTION
 Get Rules From A10 Network axapi V3
.EXAMPLE
 
Enter Ip For A10 To Connect: IP Address A10 Network
Enter Username For A10 IPAddresA10 : UserName For A10 Login
Enter Password For A10 IPAddresA10 - Username ffuentes : Password for Username Login A10
Enter Name File For Export Data: Only Name for File Csv Export
 
 
#>
 
Clear-Host
Add-Type @"
    using System;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    public class ServerCertificateValidationCallback
    {
        public static void Ignore()
        {
            ServicePointManager.ServerCertificateValidationCallback +=
                delegate
                (
                    Object obj,
                    X509Certificate certificate,
                    X509Chain chain,
                    SslPolicyErrors errors
                )
                {
                    return true;
                };
        }
    }
"@

 
[ServerCertificateValidationCallback]::Ignore();

#force TLS1.2 (necessary for the management interface)
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;    


 #$CredA10 = Get-Credential -Message "Enter Credential For A10"
$device = Read-Host -Prompt "Enter Ip For A10 To Connect"
$username = Read-Host -Prompt "Enter Username For A10 $device"
$password = Read-Host -Prompt "Enter Password For A10 $device - Username $username "
$filename = Read-Host -Prompt "Enter Name File For Export Data"
$exportCSV = "$PSScriptRoot\$filename.csv"

$prefix = "https:" #Prefix Https
$base = "axapi/v3" #Base Uri
$apiauth = "axapi/v3/auth" #Uri Authenticate API
$apisrv = "axapi/v3/slb/virtual-server" #Uri Get VirtualServer
$apisgs = "axapi/v3/slb/service-group" #Uri Get ServiceGroup

#Credential Json
$jsoncreds = @"
{"credentials": {"username": "$username", "password": "$password"}}
"@


#Obtain Token Connection
$request = Invoke-RestMethod -Method Post -Uri "$prefix//$device/$apiauth" -Body $jsoncreds -ContentType application/json -ErrorVariable lostconnection | Select -ExpandProperty authresponse
$signature = $request.Signature

#Header
$head = @{ Authorization= "A10 $signature" }

function Get-A10Rules {

    param($vs1, $IP1, $vipProtocol, $portVS, $SNAT, $sg, $profileSSL, $member, $PortMember, $MemberState)
    $objError = New-Object System.Object
    $objError | Add-Member -type NoteProperty -name VirtualServer -value $($vs1)
    $objError | Add-Member -type NoteProperty -name IPVip -value $($IP1)
    $objError | Add-Member -type NoteProperty -name PortVip -value $($portVS)
    $objError | Add-Member -type NoteProperty -name ProtocolVip -value $($portVS)
    $objError | Add-Member -type NoteProperty -name SNAT -value $($SNAT)
    $objError | Add-Member -type NoteProperty -name ServiceGroup -value $($sg)
    $objError | Add-Member -type NoteProperty -name ProfileSSL -value $($profileSSL)
    $objError | Add-Member -type NoteProperty -name Member -value $($member)
    $objError | Add-Member -type NoteProperty -name PortMember -value $($PortMember)
    $objError | Add-Member -type NoteProperty -name MemberState -value $($MemberState)
    $objError
}

#Write-Host "$prefix//$device/$apisrv/$fullvs"

$teste = Invoke-RestMethod -Method Default -Uri "$prefix//$device/$base/slb/?format=json" -Headers $head -ContentType application/json | Select -ExpandProperty slb 
foreach($allin in $teste)
{
       $data = @()
       $data += $allin

      foreach($fullvs in $data.'virtual-server-list'.name){

        $vsdata = Invoke-RestMethod -Method Default -Uri "$prefix//$device/$apisrv/$fullvs" -Headers $head -ContentType application/json
      
        $nameVS = $vsdata.'virtual-server'.name
        $IpAddressVIP = $vsdata.'virtual-server'.'ip-address'
        $portVip = $vsdata.'virtual-server'.'port-list'.'port-number'
        $vsprotocol = $vsdata.'virtual-server'.'port-list'.protocol
        $vsSnat = $vsdata.'virtual-server'.'port-list'.pool
        $sg = $vsdata.'virtual-server'.'port-list'.'service-group'
        $profileSSL = $vsdata.'virtual-server'.'port-list'.'template-client-ssl'

        $searchSG = Invoke-RestMethod -Method Default -Uri "$prefix//$device/$apisgs/$sg" -Headers $head -ContentType application/json
        foreach($member in $searchSG){
            
            $mem = $member.'service-group'.'member-list'.name
            $memPort = $member.'service-group'.'member-list'.port
            $memState = $member.'service-group'.'member-list'.'member-state'

            Get-A10Rules -vs1 $nameVS -IP1 $IpAddressVIP -portVS $($portVip -join ",") -vipProtocol $($vsprotocol -join ",") -SNAT $($vsSnat -join ",") -sg $($sg -join ",") -profileSSL $($profileSSL -join ",") -member $($mem -join ",") -PortMember $($memPort -join ",") -MemberState $($memState -join ",") | Export-Csv -Path $exportCSV -Delimiter "," -NoTypeInformation -Append
        
        }
            

      
      }
           
       

}

Write-Host "The File Export Path is $exportCSV"