|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Outputs to a File |
| 4 | +.DESCRIPTION |
| 5 | + Outputs the result of a script into a file. |
| 6 | +.EXAMPLE |
| 7 | + Invoke-PipeScript { |
| 8 | + [OutputFile("hello.txt")] |
| 9 | + param() |
| 10 | +
|
| 11 | + 'hello world' |
| 12 | + } |
| 13 | +.Example |
| 14 | + Invoke-PipeScript { |
| 15 | + param() |
| 16 | +
|
| 17 | + $Message = 'hello world' |
| 18 | + [Save(".\Hello.txt")]$Message |
| 19 | + } |
| 20 | +#> |
| 21 | +[Alias('Save')] |
| 22 | +param( |
| 23 | +# The Output Path |
| 24 | +[Parameter(Mandatory)] |
| 25 | +[string] |
| 26 | +$OutputPath, |
| 27 | + |
| 28 | +# The Script Block that will be run. |
| 29 | +[Parameter(ValueFromPipeline)] |
| 30 | +[scriptblock] |
| 31 | +$ScriptBlock, |
| 32 | + |
| 33 | +[Parameter(ValueFromPipeline)] |
| 34 | +[Management.Automation.Language.VariableExpressionast] |
| 35 | +$VariableAst, |
| 36 | + |
| 37 | +# The encoding parameter. |
| 38 | +[string] |
| 39 | +$Encoding, |
| 40 | + |
| 41 | +# If set, will force output, overwriting existing files. |
| 42 | +[switch] |
| 43 | +$Force, |
| 44 | + |
| 45 | +# The export script |
| 46 | +[scriptblock] |
| 47 | +$ExportScript, |
| 48 | + |
| 49 | +# The serialization depth. Currently only used when saving to JSON files. |
| 50 | +[int] |
| 51 | +$Depth = 100 |
| 52 | +) |
| 53 | + |
| 54 | +begin { |
| 55 | + function SaveJson { |
| 56 | + # Determine if the content appears to already be JSON |
| 57 | + if ($jsonToSave -match '^[\s\r\n]{0,}[\[\{\`"/]') { |
| 58 | + $jsonToSave | Set-Content -Path '$safeOutputPath' $OtherParams |
| 59 | + } else { |
| 60 | + ConvertTo-JSON -InputObject `$jsonToSave -Depth $depth | |
| 61 | + Set-Content -Path '$safeOutputPath' $otherParams |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +process { |
| 67 | + $safeOutputPath = $OutputPath.Replace("'","''") |
| 68 | + $otherParams = @( |
| 69 | + if ($Encoding) { |
| 70 | + "-Encoding '$($encoding.Replace("'", "''"))'" |
| 71 | + } |
| 72 | + if ($Force) { |
| 73 | + "-Force" |
| 74 | + } |
| 75 | + ) -join ' ' |
| 76 | + |
| 77 | + $inputObjectScript = |
| 78 | + if ($VariableAst) { |
| 79 | + $VariableAst.Extent.ToString() |
| 80 | + } elseif ($ScriptBlock.Ast.ParamBlock.Parameters.Count) |
| 81 | + { |
| 82 | + "`$ParameterCopy = [Ordered]@{} + `$psBoundParameters; & { $ScriptBlock } @ParameterCopy" |
| 83 | + } else { |
| 84 | + "& { $ScriptBlock }" |
| 85 | + } |
| 86 | + |
| 87 | + if ($OutputPath -match '\.json') { |
| 88 | + [ScriptBlock]::Create(" |
| 89 | +`$jsonToSave = $inputObjectScript |
| 90 | +# Determine if the content appears to already be JSON |
| 91 | +if (`$jsonToSave -match '^[\s\r\n]{0,}[\[\{\`"/]') { |
| 92 | + `$jsonToSave | Set-Content -Path '$safeOutputPath' $OtherParams |
| 93 | +} else { |
| 94 | + ConvertTo-JSON -InputObject `$jsonToSave -Depth $depth | Set-Content -Path '$safeOutputPath' $otherParams |
| 95 | +} |
| 96 | +
|
| 97 | + ") |
| 98 | + } |
| 99 | + elseif ($OutputPath -match '\.[c|t]sv$') { |
| 100 | + [ScriptBlock]::Create("$inputObjectScript | Export-Csv -Path '$safeOutputPath' $otherParams") |
| 101 | + } |
| 102 | + elseif ($OutputPath -match '\.(?>clixml|clix|xml)$') { |
| 103 | + [ScriptBlock]::Create(" |
| 104 | +`$toSave = $inputObjectScript |
| 105 | +if (`$toSave -as [xml]) { |
| 106 | + `$strWrite = [IO.StringWriter]::new() |
| 107 | + `$configurationXml.Save(`$strWrite) |
| 108 | + (`"$strWrite`" -replace '^\<\?xml version=`"1.0`" encoding=`"utf-16`"\?\>') | Set-Content -Path '$safeOutputPath' $otherParams |
| 109 | +} else { |
| 110 | + `$toSave | Export-Clixml -Path '$safeOutputPath' $otherParams |
| 111 | +} |
| 112 | +") |
| 113 | + } |
| 114 | + else { |
| 115 | + [ScriptBlock]::Create("$inputObjectScript | Set-Content -Path '$safeOutputPath' $otherParams") |
| 116 | + } |
| 117 | +} |
0 commit comments