I am trying to write script to extract some data from json and then append them to .txt file for my next Use-Case.
When I execute code below:
$columns = "ID_Report","Name","Created_by","Last_mod_by"
$file = Get-Content -Path "W:PersonalUlrichVA-Scrapingreports.json" -Raw | ConvertFrom-Json
$key_first = "items"
Write-Output "$($columns[0]);$($columns[1]);$($columns[2]);$($columns[3]);$($columns[4])" > xxx.txt
foreach ($record in $file){
if ($record | Select-Object -ExpandProperty $key_first -ErrorAction SilentlyContinue){
foreach ($item in $record.items){
"$($item.id);$($item.name);$($item.createdBy);$($item.modifiedBy)" | Add-Content "W:PersonalUlrichSkriptikyxxx.txt"
}
}
}
I get many errors resulting in truncation of exported data:
Add-Content : The process cannot access the file 'W:PersonalUlrichSkriptikyxxx.txt' bec
ause it is being used by another process.
At line:12 char:82
+ ... tem.modifiedBy)" | Add-Content "W:PersonalUlrichSkriptikyxxx.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (W:PersonalUlrichSkriptikyxxx.txt:String) [A
dd-Content], IOException
+ FullyQualifiedErrorId : GetContentWriterIOError,Microsoft.PowerShell.Commands.AddCon
tentCommand