|
| 1 | +Function Export-OneShellData |
| 2 | +{ |
| 3 | + [cmdletbinding(DefaultParameterSetName = 'delimited')] |
| 4 | + param( |
| 5 | + $ExportFolderPath = $script:ExportDataPath |
| 6 | + , |
| 7 | + [string]$DataToExportTitle |
| 8 | + , |
| 9 | + $DataToExport |
| 10 | + , |
| 11 | + [parameter(ParameterSetName = 'xml/json')] |
| 12 | + [int]$Depth = 2 |
| 13 | + , |
| 14 | + [parameter(ParameterSetName = 'delimited')] |
| 15 | + [parameter(ParameterSetName = 'xml/json')] |
| 16 | + [ValidateSet('xml', 'csv', 'json', 'clixml')] |
| 17 | + [string]$DataType |
| 18 | + , |
| 19 | + [parameter(ParameterSetName = 'delimited')] |
| 20 | + [switch]$Append |
| 21 | + , |
| 22 | + [parameter(ParameterSetName = 'delimited')] |
| 23 | + [string]$Delimiter = ',' |
| 24 | + , |
| 25 | + [switch]$ReturnExportFilePath |
| 26 | + , |
| 27 | + [parameter()] |
| 28 | + [ValidateSet('Unicode', 'BigEndianUnicode', 'Ascii', 'Default', 'UTF8', 'UTF8NOBOM', 'UTF7', 'UTF32')] |
| 29 | + [string]$Encoding = 'Ascii' |
| 30 | + ) |
| 31 | + #Determine Export File Path |
| 32 | + #validate append |
| 33 | + if ($Append -eq $true -and $DataType -ne 'csv') |
| 34 | + { |
| 35 | + throw("-Append is not supported with data type $DataType. It is only supported with data type 'csv'") |
| 36 | + } |
| 37 | + Function GetTimeStamp |
| 38 | + { |
| 39 | + [string]$Stamp = Get-Date -Format yyyyMMdd-HHmmss |
| 40 | + #$([DateTime]::Now.ToShortDateString()) $([DateTime]::Now.ToShortTimeString()) #check if this is faster to use than Get-Date |
| 41 | + $Stamp |
| 42 | + } |
| 43 | + $stamp = GetTimeStamp |
| 44 | + #Build the ExportFilePath value |
| 45 | + switch ($DataType) |
| 46 | + { |
| 47 | + 'xml' |
| 48 | + { |
| 49 | + $ExportFilePath = Join-Path -Path $exportFolderPath -ChildPath $($Stamp + $DataToExportTitle + '.xml') |
| 50 | + }#xml |
| 51 | + 'clixml' |
| 52 | + { |
| 53 | + $ExportFilePath = Join-Path -Path $exportFolderPath -ChildPath $($Stamp + $DataToExportTitle + '.xml') |
| 54 | + }#xml |
| 55 | + 'json' |
| 56 | + { |
| 57 | + $ExportFilePath = Join-Path -Path $exportFolderPath -ChildPath $($Stamp + $DataToExportTitle + '.json') |
| 58 | + }#json |
| 59 | + 'csv' |
| 60 | + { |
| 61 | + if ($Append) |
| 62 | + { |
| 63 | + $mostrecent = @(get-childitem -Path $ExportFolderPath -Filter "*$DataToExportTitle.csv" | Sort-Object -Property CreationTime -Descending | Select-Object -First 1) |
| 64 | + if ($mostrecent.count -eq 1) |
| 65 | + { |
| 66 | + $ExportFilePath = $mostrecent[0].fullname |
| 67 | + }#if |
| 68 | + else {$ExportFilePath = Join-Path -Path $exportFolderPath -ChildPath $($Stamp + $DataToExportTitle + '.csv')}#else |
| 69 | + }#if |
| 70 | + else {$ExportFilePath = Join-Path -Path $exportFolderPath -ChildPath $($Stamp + $DataToExportTitle + '.csv')}#else |
| 71 | + }#csv |
| 72 | + }#switch $dataType |
| 73 | + #Attempt Export of Data to File |
| 74 | + $message = "Export of $DataToExportTitle as Data Type $DataType to File $ExportFilePath" |
| 75 | + Write-OneShellLog -Message $message -EntryType Attempting |
| 76 | + Try |
| 77 | + { |
| 78 | + $formattedData = $( |
| 79 | + switch ($DataType) |
| 80 | + { |
| 81 | + 'xml' |
| 82 | + { |
| 83 | + $DataToExport | ConvertTo-Xml -Depth $Depth -ErrorAction Stop -NoTypeInformation -As String |
| 84 | + }#xml |
| 85 | + 'clixml' |
| 86 | + { |
| 87 | + #$DataToExport | ConvertTo-CliXML -Depth -errorAction Stop -Encoding $Encoding #not supported in Windows PowerShell, also need to handle Encoding if UTF8NOBOM is specified |
| 88 | + }#xml |
| 89 | + 'json' |
| 90 | + { |
| 91 | + $DataToExport | ConvertTo-Json -Depth $Depth -ErrorAction Stop |
| 92 | + }#json |
| 93 | + 'csv' |
| 94 | + { |
| 95 | + $DataToExport | ConvertTo-Csv -ErrorAction Stop -NoTypeInformation -Delimiter $Delimiter |
| 96 | + }#csv |
| 97 | + } |
| 98 | + ) |
| 99 | + $outFileParams = @{ |
| 100 | + ErrorAction = 'Stop' |
| 101 | + LiteralPath = $ExportFilePath |
| 102 | + } |
| 103 | + switch ($Encoding) |
| 104 | + { |
| 105 | + 'UTF8NOBOM' |
| 106 | + { |
| 107 | + if ($Append) |
| 108 | + { |
| 109 | + $outFileParams.Append = $true |
| 110 | + $outFileParams.InputObject = $formattedData |
| 111 | + } |
| 112 | + Out-FileUtf8NoBom @outFileParams |
| 113 | + } |
| 114 | + Default |
| 115 | + { |
| 116 | + $outFileParams.Encoding = $Encoding |
| 117 | + if ($DataType -eq 'clixml') |
| 118 | + { |
| 119 | + $outFileParams.Depth = $Depth |
| 120 | + $outFileParams.InputObject = $DataToExport |
| 121 | + Export-Clixml @outFileParams |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + $outFileParams.InputObject = $formattedData |
| 126 | + if ($append) |
| 127 | + { |
| 128 | + $outFileParams.Append = $true |
| 129 | + } |
| 130 | + Out-File @outFileParams |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + if ($ReturnExportFilePath) {$ExportFilePath} |
| 135 | + Write-OneShellLog -Message $message -EntryType Succeeded |
| 136 | + }#try |
| 137 | + Catch |
| 138 | + { |
| 139 | + Write-OneShellLog -Message "FAILED: Export of $DataToExportTitle as Data Type $DataType to File $ExportFilePath" -Verbose -ErrorLog |
| 140 | + Write-OneShellLog -Message $_.tostring() -ErrorLog |
| 141 | + }#catch |
| 142 | + |
| 143 | +} |
0 commit comments