-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigToCsv.ps1
More file actions
61 lines (55 loc) · 2.23 KB
/
Copy pathConfigToCsv.ps1
File metadata and controls
61 lines (55 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[CmdletBinding]
function DataFromJsonToCsv {
param (
[Parameter(Mandatory=$true)]$jsonfilepath,
[Parameter(Mandatory=$true)][string]$csvFolderPath,
[Parameter(Mandatory=$true)]$ActionObj
)
$outputTable = @()
$baseName = $jsonfilepath.BaseName
#$currenTime = (get-date((get-date).ToUniversalTime()) -Format o)
$currenTime = (get-date((get-date).ToUniversalTime()) -Format yyyyMMddhhmmss)
try {
$jsonFile = Get-Content $_.FullName | convertfrom-json
foreach($action in $actions){
Write-Host $action -ForegroundColor Green
$jconfigs = $jsonFile.$action
foreach($jconfig in $jconfigs){
$id = $jconfig.'$id'
if($jconfig.source.'$description'){
$descr=$jconfig.source.'$description'
}
elseif ($jconfig.target.'$description') {
$descr=$jconfig.target.'$description'
}
else{$descr=$null}
Write-Host "$($action)-$($jconfig.name)-$($jconfig.action)-$($descr)" -ForegroundColor Blue
#write to csv
$jconfig | ConvertTo-Csv | Out-File "$csvFolderPath\$($currenTime)_$($baseName)_$($action)_$($id).csv"
}
}
return 0
}
catch {
$errorMessage = $_.Exception.Message
Write-Host $errorMessage -ForegroundColor Red
return 11
}
}
### this might be conveted in input parameters
#Jspn folder Full Path
$jsonPath = "<Full Path to folder with json files>"
#csv output folder full path
$csvFolder = "<Full Path to folder that will contains csv files>"
##############################################
$actions = @('created','deleted','failed','skipped','unchanged','updated')
$jsonFiles = Get-ChildItem $jsonPath "*.json"
$jsonFiles | ForEach-Object{
if ((DataFromJsonToCsv -jsonfilepath $_ -csvFolderPath $csvFolder -ActionObj $actions) -eq 0){
Write-Host "$($_.Name) Processed successfully"
}
else {
Write-Host "$($_.Name) Processed with error"
Write-Host $errorMessage -ForegroundColor Red
}
}