Providers/FileSystemProvider.ps1
# # FileSystemProvider.ps1 # #region Parameters [CmdletBinding()] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string] $OutputPath ); #endregion #region FUNCTIONS FUNCTION Get-Registry { [CmdletBinding()] Param () IF(![string]::IsNullOrEmpty($OutputPath)) { IF(![System.IO.Path]::HasExtension($OutputPath)) { $OutputPath = [string]::Concat($OutputPath, '.json'); } if(Test-Path $OutputPath) { Write-Verbose "Read json file from $OutputPath" $Model = ConvertFrom-Json -InputObject $([System.IO.File]::ReadAllText($OutputPath)); } } RETURN $Model; } FUNCTION Set-Registry { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [psobject] $Model ) Try { IF(![string]::IsNullOrEmpty($OutputPath)) { IF(![System.IO.Path]::HasExtension($OutputPath)) { $OutputPath = [string]::Concat($OutputPath, '.json'); } $FileInfo = [System.IO.FileInfo]::new($OutputPath); IF(!$FileInfo.Directory.Exists) { $FileInfo.Directory.Create(); } If ((-not (Test-Path $OutputPath)) -or ((Get-Content $OutputPath) -eq $Null)) { $Array = [System.Collections.ArrayList]::new(); $Array.Add($Model) | Out-Null ; $ModelAsJson = ConvertTo-Json -InputObject $Array; [System.IO.File]::WriteAllText($OutputPath, $ModelAsJson); } ELSE { [System.Collections.ArrayList]$Array = $(ConvertFrom-Json -InputObject $([System.IO.File]::ReadAllText($OutputPath));); $Array.Add($Model) | Out-Null ; $ModelAsJson = ConvertTo-Json -InputObject $Array; [System.IO.File]::WriteAllText($OutputPath, $ModelAsJson); } } } Catch { Write-Host "Error: $_.Exception" -ForegroundColor Red; } } #endregion |