Response object Regex help needed

I have been working on a powershell script that calculates an expiration date for files stored on a server, authenticates to that server, runs the commands to show the files that need to be deleted, and then deletes the files. The server responds with a JSON object that I have been able to read, but the server requires the file IDs in order to delete a file(s). I am having great difficulty in getting the file name out of the response field. The File ID is the first item in the object, but I have not been able to get to pull out the file ID value.

I know each file id would need to be captured into a variable and then execute a foreach loop to run a delete command, but getting the file ID out has been my difficulty.

my script:


$findFiles = Invoke-RestMethod -Uri $url2 -Method Get -Body $body2 -ContentType 'application/json' -Headers $hdrs2 -WebSession $session |ConvertTo-Json

Write-host "Find Files value is " $findFiles

#get file ID and delete individual files, First separate out the meta response ----------------------

$psdata=ConvertFrom-Json-InputObject $findFiles

$psdata | Get-Member -Name response

Write-host"PSData Value is "$psdata

#Separate out the Response response ------------------------

$response=$psdata.response

$response | Get-Member -Name files

Write-host"Response Value is "$response.files

server response:
PSData Value is @{meta=; response=}
Response Value is @{id=5cfff5e6fa13bc0219563289; name=6001 06-11-2019 02:41:39 Facility Visitation.pdf; processed=True; created_at=06/11/2019 14:41:42; updated_at=06/11/2019 14:41:45; url=https://s3-us-west-1.amazonaws.com/storage-platform.cloud.appcelerator.com/detCyr5jIe2ZSjVaAo1RmJxJZiSNcQXP/files/b6/50/5cfff5e6fa13bc0219563289/20190611_14_41?AWSAccessKeyId=ASIAVHFI3UVCJCDCYGES&Expires=1560280697&Signature=HlV9OvQq62TqG%2Bpa5jVj6WxWJSQ%3D&x-amz-security-token=AgoJb3JpZ2luX2VjEIP%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLXdlc3QtMiJIMEYCIQCG64ar5fWofRgze%2B%2Bglt0FjMy7vLegh4GKaI5SQ1%2BH3gIhAIN9UBbqr3AUcoCXg0b6DGljyFe51OrWDvIBKbOxOQO8KuMDCKz%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEQABoMMzU4OTgzOTAyNTMyIgyRRKYc89DuKXftNIkqtwPuyKWq7Mg77mjYTccuUXVv3g5ep%2Bs%2F5usKP%2BdUNcO5gmTPxRGUEteSSz5pj1DD5Togf0j1K6hbvDCWNxHgEFIzOCiYactqfC4C9FF0DeQXesNofg5wqQrGEJmg7%2Fmtx1WcKDiFoGpqjWVdrtqibDVr7ChGvTWvFcH93GcNaBNinSSZbDEIMpNCcmL%2FsWxUgdl9Wb%2FbUOQHz0ZhdfvZFhBxHgBGN3Xi9wASqAThL5Ib22SZX33JdDGRQlkwkLs43NsAJOguF4r8g%2BCYS4HEsn%2Fd29cn83bboq7Gople45Aak0b8tEz%2BdRz%2FGJbWC5hfGn3k5hZXY9nXYCnggPVG85PiGCCFydXwzcLAZiqWZjj%2BpPROMPRQ85DJ8iX7XCQYfIbFKUn7Cc2TPO9aUSsRok2SVipslCkjxY5vYa%2BIEvuBNsHUQxBBH8SnKSQVWrpCqbU39Nu04KKf7LtFG8%2BYNQsqUlMRJwSWZPgLy%2FDRJ0e90GJf41%2B9emKXTWglcGYDR9tgkEjyGQj2EoW3mnOyAW0kDR6h8vQ4KqaU0v8xxuYtoYFoxTQPr8k79nkFZWsk9kO3b3UcKSiLMLXu%2F%2BcFOrMBdsD5h9fumw6%2BePXJdgc25zWmoGjlT2YB3WnQIRr5tXiqrqDDuO98azhOyvkQ%2Blobx0%2BmWUyeKQ3No0UMqIxpQJtN8Tw%2BBL9z7QmyzcQtEitwT2McVWR%2B9kcE33WG%2FYw113y7nJXDhYIHYWGQIi%2BSmXMgfqB47nu94CE6Dh068k0RznqgXsmYHCmaPYfWTK396EG7K3OPBD9OLJoKrfHgni%2FIgEkTaPR0NXmIXtxPdsk4%2BFA%3D; s3_acl=private; user_id=5c5214c77230c8024595646e; custom_fields=} @{id=5cfff4caa850b3021957f7d7; name=6001 06-11-2019 02:36:55 Monthly Inspection.pdf; processed=True; created_at=06/11/2019 14:36:58; updated_at=06/11/2019 14:37:02; url=https://s3-us-west-1.amazonaws.com/storage-platform.cloud.appcelerator.com/detCyr5jIe2ZSjVaAo1RmJxJZiSNcQXP/files/1a/8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blobx0%2BmWUyeKQ3No0UMqIxpQJtN8Tw%2BBL9z7QmyzcQtEitwT2McVWR%2B9kcE33WG%2FYw113y7nJXDhYIHYWGQIi%2BSmXMgfqB47nu94CE6Dh068k0RznqgXsmYHCmaPYfWTK396EG7K3OPBD9OLJoKrfHgni%2FIgEkTaPR0NXmIXtxPdsk4%2BFA%3D; s3_acl=private; user_id=5c5214c77230c8024595646e; custom_fields=}
 TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
response NoteProperty System.Management.Automation.PSCustomObject response=@{files=System.Object[]}
files NoteProperty Object[] files=System.Object[]
All my attempts to write a regex to pull out the file id (id=5cfff5e6fa13bc0219563289;) have not been successful - is there a better way? What is the regex to just get the id?

So, here’s what it looks like properly formatted:

$findFiles = Invoke-RestMethod -Uri $url2 -Method Get -Body $body2 -ContentType 'application/json' -Headers $hdrs2 -WebSession $session | ConvertTo-Json
Write-Host "Find Files value is " $findFiles

#get file ID and delete individual files, First separate out the meta response
$psdata = ConvertFrom-Json-InputObject $findFiles
$psdata | Get-Member -Name response
Write-Host "PSData Value is "$psdata

#Separate out the Response response
$response = $psdata.response
$response | Get-Member -Name files
Write-Host "Response Value is "$response.files

I’m guessing you’re coming from a Linux background. Regex doesn’t have quite as much application in PowerShell, because the data that comes from command results isn’t just text. In this case, it looks like $response.files contains the id value, so you should be able to get what you with just:

$response.files.id

But also, your Get-Member statements (lines 6 and 11) aren’t actually doing anything because you haven’t stored the output from those commands into anything. You can improve the efficiency and remove the intermediate steps:

$findFiles = Invoke-RestMethod -Uri $url2 -Method Get -Body $body2 -ContentType 'application/json' -Headers $hdrs2 -WebSession $session | ConvertTo-Json
Write-Host "Find Files value is $findFiles"

#get file ID and delete individual files, First separate out the meta response
$psdata = ConvertFrom-Json -InputObject $findFiles
Write-Host "Response Value is $($psdata.response.files.id)"

THANK YOU Grokkit!

Unfortunately when I run Write-Host “Response Value is $($psdata.response.files.id)”
The response from powershell is

Response Value is

(nothing, it shows nothing.)

In my earlier attempts, it showed that files was :

 TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
response NoteProperty System.Management.Automation.PSCustomObject response=@{files=System.Object[]}
files NoteProperty Object[] files=System.Object[]

The Files object is an array.
So I haven’t been able to access the id field in files, which is was I was told to use regex to pull out the id that I need.

I updated my script since yesterday to break out each file in the array, but I still just want the id, not the all the information about the file…

My current script breaks out the files into individual objects, but I still cannot access “id=xxxx” any help is greatly appreciated!

$filesArray = ConvertFrom-Json $findFiles

$fuFileId=$filesArray.response.files

write-host “array size is” $fuFileId.Count

$numEmployees = $fuFileId.Count

$i = 0

while ($i -lt $numEmployees) {

$empObj=$fuFileId[$i]

$newObj=ConvertTo-Json$empObj|Out-String

Write-Host"my id is "$newObj.

$i=$i+1

}


and it results in:

15 days ago from today's date is 5/28/2019 11:39:21 AM
array size is 9
my id is "@{id=5ced50e1960304022139ae50; name=31 05-28-2019 11:16:47 Facility Visitation.pdf; processed=True; created_at=05/28/2019 11:16:49; updated_at=05/28/2019 11:16:51; url=https://s3-us-west-1.amazonaws.com/storage-platform.cloud.appcelerator.com/detCyr5jIe2ZSjVaAo1RmJxJZiSNcQXP/files/22/a2/5ced50e1960304022139ae50/20190528_11_16?AWSAccessKeyId=ASIAVHFI3UVCMVJAREM2&Expires=1560354262&Signature=dJ%2B2KFkSWq45zxeIm0p3bgGse0s%3D&x-amz-security-token=AgoJb3JpZ2luX2VjEJf%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLXdlc3QtMiJHMEUCIQCxbhJGC51WUORxqxoL1Sb%2BzHRqT%2F5Xc50JhXogbm3wXwIgYXkrvRccGrmkt8D%2FN3qXDpJd3P3rkfIAugp6s82aKzcq4wMIwP%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAAGgwzNTg5ODM5MDI1MzIiDAESriKvZSVVzjHZQSq3A%2FkDuBZYdRBTXG%2FL1XFraKkoASyLkihx3jqlzz1R1hmrYDIGcVJg7%2FbsKPJa92K6Mpd3UxWZJ4jO543eZxosQ2NKaLZTkfoD6qiq9AmKapsIX8aMVztjjiJ2ZcFgRl07SkoDnOly8%2BZFKu3Kwu8tnNvl9acEwn30LlOTSeCeQybK%2B1enboHZHlk7l79CLXLuAnbfaa%2FqWlaDUfchvKRZqw7JiKmppV2ywjsDLFMRoNWrXrrOWTYoi4KhGUP%2F%2BVxWkkfYBmW59mcNdd4wjuzYYDA4qLNhAyCtXhZ5FRcXe8578vXtD1ZehxTQrLGP47rjjOwx2AdQDn1WTfOXK712yT99Bow3WwrDNg2OvVToKfBGL6bhf79PMgUH4CICgx8IGdIxxzyZ57dcU%2Bom0Wj0ol6RehzIxccNgUguufZzuZLyXOTAvLiP7d4fI8lX7CEPeNqs9JQwk3hlKMQOVa54GnDi5cpYox7KK27yWla8F0t7fZ4tokf%2FqccK03JhpLBTQvi2JYb72KB%2FEmWkxkDR0mhZUUDdrf3LeRv6KdyrGUcbfwGwkqNgP5w1%2BqZ8ysUGgrLa%2B5t2hzkwmqWE6AU6tAHvlaibNWNpuKW4dK%2BJAWBQ3wKUTonQ8RTZmhjHCAk5fmySWZSs4YFCGqmlapThmtLZ1HwY7CsiIiCZrwJdOZlPkUPKpfW7W9LIK49BHRjncmE1bazuNqtcEQq767Rd20pB5tG5twJtxavchvcKNbXWyEfdlSO0Zx12PZX2k4KPBscc1yMBVtDmR55lD4OyfN0qzmrVf5Bvofzzc4S%2FBObzZ7aFxQa6gqDFhc8pRTAqAtsdwaE%3D; s3_acl=private; user_id=5b2d3bc493fb030218184c8f; custom_fields=}"
.
my id is "@{id=5ced50595686f70220394f21; name=31 05-28-2019 11:14:31 Monthly Inspection.pdf; processed=True; created_at=05/28/2019 11:14:33; updated_at=05/28/2019 11:14:37; url=https://s3-us-west-1.amazonaws.com/storage-platform.cloud.appcelerator.com/detCyr5jIe2ZSjVaAo1RmJxJZiSNcQXP/files/ea/68/5ced50595686f70220394f21/20190528_11_14?AWSAccessKeyId=ASIAVHFI3UVCM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twJtxavchvcKNbXWyEfdlSO0Zx12PZX2k4KPBscc1yMBVtDmR55lD4OyfN0qzmrVf5Bvofzzc4S%2FBObzZ7aFxQa6gqDFhc8pRTAqAtsdwaE%3D; s3_acl=private; user_id=5b2d3bc493fb030218184c8f; custom_fields=}"

Hmm, what happens if you change line 20 to

Write-Host "my id is " $newObj.id

When I modify line 20 to your suggestion, the response from powershell terminal is:

array size is 10
my id is
my id is
my id is
my id is
my id is
my id is
my id is
my id is
my id is
my id is

 

Oh, you converted the data to JSON on line 18. You won’t be able to return the individual property values of a JSON object in PowerShell. It would be easiest to do that before doing ConvertTo-JSON, maybe with $empObj.

You should also try using Set-PSDebug -Trace 1 to examine the script’s progression, and consider echoing the contents of the various objects as you go to confirm that they actually contain what you expect. If you

$fuFileId | Get-Member
does it actually contain an “id” property?