0

I am getting IP and Name data from infoblox API like below

my_redacted_screenshot

As you can see there are certain entries which are blank or having like "gateway"/"reserved s"

I need to eliminate such entries and write rest of the data in csv. currently I am writing all of the data using below code

$url = "https://this.infoblox.fqdn.local/wapi/v2.10.5/ipv4address?network=$subnet&status=USED&_return_as_object=1"
    

$lapw = Get-Content "E:\EncryptedPassword\pw.txt" | ConvertTo-SecureString -Key (Get-Content E:\EncryptedPassword\aes.key)
$creds = New-Object System.Management.Automation.PsCredential("USER",$lapw)
$Result_IPAddress = Invoke-RestMethod -Uri $url -Method Get -Credential $creds -Headers $headers -ContentType 'application/json'


$Result_IPAddress.Result |
Select-Object @{Name='IPAddress';Expression='IP_Address'},                
              @{Name='IPName';Expression={$_.names -join ' ;'}}  | Export-Csv -Path "E:\Used_IP_Details.csv" -NoTypeInformation -Append -Force

Please let me know how can I filter the data. as of now I got only these 3 types which I have to ignore writing but later there might be more such keywords which I have to ignore and filter out the data.

I tried giving like this

@{Name='IPName';Expression={$_.names -join ' ;'} -and {$_.names -match 'reserved s'} -or {$_.names -match 'gateway'}}

but problem is I am not getting how to give condition for Blank and there are certain entries which starts with "reserved" but have followed hostname in it so I have to keep them. So have to have strict matching.


With respect to @MathiasR.Jessen answer, tried below

$addressesAndNames = $Result_IPAddress.Result |
Select-Object @{Name='IPAddress';Expression='IP_Address'},                
              @{Name='IPName';Expression={$_.names -join ' ;'}}  

$addressesAndNames | Where-Object $_.IPName -notlike 'reserved *' | Where-Object $_.IPName -notlike 'gateway' | Where-Object $_.IPName -ne '' | Export-Csv -Path "E:\ComparisonFiles\InfoBlox_Used_IP_Details.csv" -NoTypeInformation -Append -Force

but getting error like

Where-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command 
again.
At E:\InfoBlox_CMDB_Comparison_Report.ps1:101 char:35
+ $addressesAndNames | Where-Object $_.IPName -notlike 'reserved *' | W ...
+                                   ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Where-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.WhereObjectCommand
1
  • Filter the object with Where-Object and pipe the result to Select-Object? Commented Feb 8, 2022 at 12:29

1 Answer 1

3

Select-Object is not for filtering. For that, you'll want to use Where-Object:

$addressesAndNames = $Result_IPAddress.Result | Select-Object @{Name='IPAddress';Expression='IP_Address'},@{Name='IPName';Expression={$_.names -join ' ;'}}

# use `Where-Object` to filter the output from `Select-Object`
$addressesAndNames |Where-Object IPName -notlike 'reserved *' |Export-Csv ...

You can chain as many excluding Where-Object commands as you like:

$addressesAndNames |Where-Object IPName -notlike 'reserved *' |Where-Object IPName -notlike '*gateway*' |Export-Csv ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.