Hi i have the followign PS script but the output is not how i would like to arrange it, can anyone pelase help get it right please.
Its pollign a txt file for line with Name and Expire and i would like to dispaly this into a CSV with column heading of Name and Expire and have each name and expiry date int hat line to be stored :
ID Name Expire
ID1 Name1 01/11/2024
ID2 Name2 05/12/2025
ID3 Name3 04/12/2024
My current PS script :
$txtFilePath = "C:Reportskey.txt"
$pattern1 = "id"
$pattern2 = "names"
$pattern3 = "expires"
$extractedData = @()
$lines = Get-Content -Path $txtFilePath
foreach ($line in $lines) {
if ($line -match $pattern1) {
$id = $line -replace '.*id*', ''
$extractedData += [pscustomobject]@{
'Field' = '$line'
'Value' = $id
}
}
elseif ($line -match $pattern2) {
$expires = $line -replace '.*name*', ''
$extractedData += [namepscustomobject]@{
'Field' = 'name'
'Value' = $name
}
}
elseif ($line -match $pattern3) {
$Expiry = $line -replace '.*expires*', ''
$extractedData += [pscustomobject]@{
'Field' = 'Expiry'
'Value' = $Expiry
}
}
}
$extractedData | Format-Table -AutoSize
$extractedData | Export-Csv -Path "C:ReportsResultsoutput.csv" -NoTypeInformation
Example of Key.txt:
{
"attributes": {
"created": "2019-07-03T14:55:51+00:00",
"enabled": true,
"expire": "2299-12-25T07:28:38+00:00",
"notBefore": null,
"recoverableDays": 90,
"recoveryLevel": "Recoverable",
"updated": "2022-09-26T20:32:13+00:00"
},
"contentType": "",
"id": "https://prod-ukb2c-keyvault-
ne.vault.azure.net/secrets/azure-log-analytics-workspace-
customer-id",
"managed": null,
"name": "azure-log-analytics-workspace-customer-id",
"tags": {}
},
{
Here is an extract from key.txt :
},
"contentType": "text/plain; tfmanaged=true",
"id": "https://prodtctkvne.vault.azure.net/secrets/prod-helios",
"managed": null,
"name": "prod-helios-ado-app-secret",
"tags": {}
},
{
"attributes": {
"created": "2023-12-07T09:52:56+00:00",
"enabled": true,
"expires": null,
"notBefore": null,
"recoverableDays": 90,
"recoveryLevel": "Recoverable",
"updated": "2023-12-07T09:52:56+00:00"
},
"contentType": "text/plain; tfmanaged=true",
"id": "https://prodtctkvne.vault.azure.net/secrets/prod-helios",
"managed": null,
"name": "prod-helios-databricks-app-id",
"tags": {}
},
5
Your formatting is terrible but assuming the input is like this without the incorrect line breaks in the middle of the id strings
{
"attributes": {
"created": "2019-07-03T14:55:51+00:00",
"enabled": true,
"expire": "2299-12-25T07:28:38+00:00",
"notBefore": null,
"recoverableDays": 90,
"recoveryLevel": "Recoverable",
"updated": "2022-09-26T20:32:13+00:00"
},
"contentType": "",
"id": "https://prod-ukb2c-keyvault-ne.vault.azure.net/secrets/azure-log-analytics-workspace-customer-id",
"managed": null,
"name": "azure-log-analytics-workspace-customer-id",
"tags": {}
},
{
// More similar objects
}
then the file is actually kind of json with a list of objects without a root element. You can simply use ConvertFrom-Json
to operate on it after adding a valid root array object []
like this
PS C:> "[$(Get-Content C:Reportskey.txt)]" | ConvertFrom-Json | `
Select-Object -Property name, id, `
@{Name="attributes.expire"; Expression={($_.attributes.expire -as [datetime]).ToString("MM-dd-yyyy")}}
name id attributes.expire
---- -- -----------------
name1 id1 12-25-2299
name2 id2 12-25-2123
@{Name="name"; Expression={...}}
is used to create a user-defined property
0