# encoding: ascii # api: powershell # title: Unix Out-File # description: Write line to file with Unix Line Endings # version: 0.1 # type: function # author: Bruno Martins # license: CC0 # function: Out-UnixFile # x-poshcode-id: 6308 # x-archived: 2016-04-22T21:36:10 # x-published: 2016-04-18T17:44:00 # # function Out-UnixFile { Param( [Parameter( Mandatory = $True )] [string] $FilePath, [Parameter( Mandatory = $True, ValueFromPipeline = $True )] [string[]] $InputObject, [switch] $Append = $False ) BEGIN { $Fs = $Null $Sw = $Null Try { If( -not( Test-Path $FilePath ) ) { $result = New-Item -Path $FilePath -ItemType File -ErrorAction Stop } $Encoding = New-Object System.Text.UTF8Encoding If( $Append ) { $FileMode = [System.IO.FileMode]::Append } Else { $FileMode = [System.IO.FileMode]::Open } $Fs = New-Object -TypeName System.IO.FileStream( $FilePath, $FileMode ) $Sw = New-Object -TypeName System.IO.StreamWriter( $Fs, $Encoding ) } Catch { Throw } } PROCESS { Try { ForEach( $Line in $InputObject ) { $Sw.Write( "$( $line )`n" ) } } Catch { If( $Sw -ne $Null ) { $Sw.Close() } If( $Fs -ne $Null ) { $Fs.Close() } Throw } } END { $Sw.Close() $Fs.Close() } }