Private/Out-JsonFile.ps1
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 |
function Out-JsonFile { [CmdletBinding()] param( [Parameter( Mandatory = $true, ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true )] [object]$Object, [Parameter( Mandatory = $true)] [string]$Path, [Parameter( Mandatory = $true)] [string]$FileName ) try { if (-not (Test-Path -Path $Path)) { [void](New-Item -Path $Path -ItemType Directory -Force) } $destinationPath = Join-Path -Path $Path -ChildPath $FileName $Object | ConvertTo-Json -Depth 10 | Out-File -FilePath $destinationPath -Encoding utf8 -Force } catch { throw $PSItem } } |