src/functions/Export-RegentRecordsByFind.ps1
#Requires -Modules @{ ModuleName="Microsoft.Xrm.Data.Powershell"; ModuleVersion="2.5"} . $PSScriptRoot\Get-RegentConnection.ps1 function Export-RegentRecordsByFind { ############################## #.SYNOPSIS #Exports the results of all advanced finds listed in a config file, or from a single configuration object piped to it. # #.DESCRIPTION #This command reads a config file and exports the results all the Advanced Finds listed in that config file. Alternatively, a single config object (such as that generated by Get-RegentExportConfig) can be piped to the command # #.PARAMETER PathToConfig #The path to the config file. Defaults to a file in the current directory called AdvancedFindExport.config.json # #.PARAMETER ParameterObject #Optional parameter for the an object to be used as the config. You can use Get-RegentConnection to generate this object. # #.PARAMETER JsonDepth #The JSON returned from an Advanced Find is often several layers deep. This parameter indicates how deep to extract the data from the returned JSON. The default value (4) should be adequate for most cases, but the value can be adjusted down if more data than necessary is being returned, or up if not enough. # #.EXAMPLE # Find a job named "MyImportantJob" and export it. # Get-RegentExportConfig -JobName "MyImportantJob" | Export-RegentRecordsByFind # #.EXAMPLE # Export all jobs in a specific config file, using a higher JSON depth. # Export-RegentRecordsByFind -PathToConfig C:\Path\To\Config.json -JsonDepth 6 # # #.NOTES #General notes ############################## [CmdletBinding()] param( [string]$PathToConfig = ".\AdvancedFindExport.config.json", [Parameter(ValueFromPipeline = $true)] [System.Object]$ParameterObject, [int]$JsonDepth = 4 ) process { if ($ParameterObject) { if ($PathToConfig -ne ".\AdvancedFindExport.config.json") { throw "Please pass in either a path to the config or a parameter object, but not both" } Write-Host -ForegroundColor Yellow "Executing job $($ParameterObject.JobName)" Write-Host "Connection to CRM $($ParameterObject.CrmInstance)" $conn = Get-RegentConnection -CrmInstance $ParameterObject.CrmInstance Write-Host "Fetching result from view $($ParameterObject.ViewName)" Get-CrmRecordsByViewName ` -conn $conn ` -ViewName $ParameterObject.ViewName ` -IsUserView $ParameterObject.IsUserView ` -AllRows | ` ConvertTo-Json -Depth $JsonDepth > $ParameterObject.ExportPath Write-Host "Results exported to $($ParameterObject.ExportPath)" } else { $config = Get-Content $PathToConfig | ConvertFrom-Json foreach ($item in $config) { Write-Host "Executing job $($item.JobName)" Write-Host "Connection to CRM $($item.CrmInstance)" $conn = Get-RegentConnection -CrmInstance $item.CrmInstance Write-Host "Fetching result from view $($item.ViewName)" Get-CrmRecordsByViewName ` -conn $conn ` -ViewName $item.ViewName ` -IsUserView $item.IsUserView ` -AllRows | ` ConvertTo-Json -Depth $JsonDepth > $item.ExportPath Write-Host -ForegroundColor Green "Results exported to $($item.ExportPath)" } } } } |