public/Out-FileNoBom.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


function Out-FileNoBom () {
    [CmdletBinding(SupportsShouldProcess)]
    Param(
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Object] $InputObject,

        [Alias("o", "dest")]
        [ValidateNotNullOrEmpty]
        [Parameter(Position = 0, ValueFromPipelineByPropertyName = $true)]
        [String] $Destination,

        [Switch] $Append,

        [Switch] $NoClobber,

        [Switch] $Force 
    )

    Process {
        
        $Path = [IO.Path]::GetFullPath($Destination)
        $dir = Split-Path $Path 
        if(!(Test-Path $dir)) {
            if($Force.ToBool()) {
                New-Item -ItemType Directory $dir 
            } else {
                throw [System.IO.DirectoryNotFoundException] $dir 
            }
        }


        $content = $InputObject.ToString()

        if ($PSCmdlet.ShouldProcess("InputObject", "Writing JSON to $Path")) {
           
            if($Append.ToBool()) {
                [IO.File]::AppendAllText($Path, $content)
                return;
            }

            if($NoClobber.ToBool() -and (Test-Path $Destination)) {
                throw [System.InvalidOperationException] "The file already exists and must not be overwritten: $Destination "
            }
            
            # defaults to UTF8 with no Byte Order Mark ("BOM")
            [IO.File]::WriteAllText($Path, $content)
        }
    }
}