Update a DataGroup using REST and Powershell
I have a scenario where I need the "data" too, not just the "name". This script does what I want but I need to add/update the "data", which is the "Value:" textbox in the F5 admin portal for iRules Data Groups. When I run the following I get the "code":400,"message":"one or more configuration identifiers must be provided" error:
$Records += @{name=$AllowedIPAddrName}
$Records += @{data=$AllowedIPAddrData}
The first line above is your entry and the second one is mine after I created the variable to hold the value for "data".
The data is correct as the DGJSONBody contains the records with the correct "data", it gives me the error when it is put in the Invoke-RestMethod.
Here is my code for the Add new entry only:
## Create a policy to trust all Certs
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
##Variables
$LoadBalancer1 = "xx.xx.xxx.xxx"
$DG = "Scott_Test"
$DGURL1 = "https://$LoadBalancer1/mgmt/tm/ltm/data-group/internal/~Common~$DG"
$Partition = "Common"
##Functions
function BackupDG {
Param(
$FileName = "Scott_Test_BackupDG",
$RESTURL
)
$Date = Get-Date -Format MMddyy-hh_mm_ss
Get-ChildItem BackupDG* | where{-not $_.PsIsContainer} | sort CreationTime -desc| select -Skip 20 | Remove-Item -Force
$DGJSONBody = @{name=$DG;partition=$Partition;records=@()}
$Records = @()
## Lookup Virtual Server and get Data Group
$DataGroupLookup = Invoke-RestMethod $RESTURL -Credential $Credential
foreach ($DataGroup in $DataGroupLookup)
{
foreach ($AllowedIPAddr in $DataGroup.Records)
{
$AllowedIPAddrName = $AllowedIPAddr.name
$AllowedIPAddrData = $AllowedIPAddr.data
$Records += @{name=$AllowedIPAddrName}
$Records += @{data=$AllowedIPAddrData}
}
}
$DGJSONBody.Records = $Records
$DGJSONBody = $DGJSONBody | ConvertTo-Json
$DGJSONBody | Out-File "C:/F5/$FileName-$Date.json"
$RecordCount = ($DataGroup.Records).Count
Write-Host "Successfully Backed Up $RecordCount Records"
}
##Add New IP address and append $Records variable
Write-Host "1. Add"
#Write-Host "2. Remove"
#Write-Host "3. Backup"
#Write-Host "4. Restore"
$DGMaint = Read-Host "Selection"
if ($DGMaint -eq "1"){
#$Credential = Get-Credential
$AllowedCustomerIP = Read-Host "Customer IP Address"
$AllowedCustomerDataNew = Read-Host "Description & JSD Number - NO spaces, Alphanumeric and dashes/underscores only"
BackupDG -RESTURL $DGURL1 -FileName "BackupDGLB1"
$DGJSONBody = @{name=$DG;partition=$Partition;records=@()}
$Records = @()
$Records += @{name="$AllowedCustomerIP/32"}
$Records += @{data=$AllowedCustomerDataNew}
## Lookup Virtual Servers
$DataGroupLookup = Invoke-RestMethod $DGURL1 -Credential $Credential
foreach ($DataGroup in $DataGroupLookup)
{
foreach ($AllowedIPAddr in $DataGroup.Records)
{
$AllowedIPAddrName = $AllowedIPAddr.name
$AllowedCustomerData = $AllowedIPAddr.data
$Records += @{name=$AllowedIPAddrName}
$Records += @{data=$AllowedCustomerData}
}
}
$DGJSONBody.Records = $Records
$DGJSONBody = $DGJSONBody | ConvertTo-Json
#$DGJSONBody
Write-Host "Adding Customer IP: $AllowedCustomerIP to the SMTP allow list."
$InvokeAdd1 = Invoke-RestMethod $DGURL1 -Credential $Credential -Method Put -Body $DGJSONBody
}
You'll see I hacked it up pretty good! No other error occurs prior to sending at line 85. The error is so vague, no pointers to anything, and a Google search is no joy either, maybe a string/integer issue with how the "data" field is populated in the Record?
Any information would be greatly appreciated, thanks!