33 lines
1011 B
PowerShell
33 lines
1011 B
PowerShell
function Export-PowerShellDataFile {
|
|
[CmdletBinding()]
|
|
Param(
|
|
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
|
|
[System.Collections.IDictionary]
|
|
$InputObject,
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
[string]
|
|
$Path,
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
[switch]
|
|
$Force
|
|
)
|
|
|
|
process {
|
|
$ResolvedPath = Repair-ConfigurationDataPathEncoding -Path $Path
|
|
|
|
if((Test-Path -LiteralPath $ResolvedPath -PathType Leaf) -and (-not $Force)){
|
|
throw "File [$ResolvedPath] already exists. Use -Force to overwrite it."
|
|
}
|
|
|
|
$Parent = Split-Path -Path $ResolvedPath -Parent
|
|
if(-not [string]::IsNullOrWhiteSpace($Parent) -and -not (Test-Path -LiteralPath $Parent)){
|
|
New-Item -Path $Parent -ItemType Directory -Force | Out-Null
|
|
}
|
|
|
|
$Text = ConvertTo-PowerShellDataFileText -InputObject $InputObject
|
|
Set-Content -LiteralPath $ResolvedPath -Value $Text -Encoding UTF8
|
|
}
|
|
}
|