src/functions/AdvFindConfigUtilities.ps1
function Add-RegentExportConfig { ############################## #.SYNOPSIS #Add an Advanced Find Export configuration to a configuration file # #.DESCRIPTION #This command generates the necessary JSON to configure an automated export from Export-RegentRecordsByFind, and appends that JSON to the specified configuration file. # #.PARAMETER PathToConfig #The path to the config file. If the file doesn't exist, it will be created. Defaults to a file in the current directory called "AdvancedFindExport.config.json" # #.PARAMETER JobName #The title of the job to be added. This also serves as an ID for other utilities to use, and must be unique or an error will be thrown # #.PARAMETER CrmInstance #The name of the CRM Instance to use. E.g CRMADVISETEST # #.PARAMETER ViewName #The name of the advanced find to export. The name must be unique in the target CRM, otherwise the other utilites may yeild unexpected results. # #.PARAMETER IsUserView #A boolean representing whether the view is a system or user view. # #.PARAMETER ExportPath #The path that the results of the advanced find will be exported to. The user executing must have write permissions to the location specified. # #.EXAMPLE # To add a job named "Marketing Contact Export" to a config in the current directory for a user view in CRMRECRUIT named "Regent | Marketing List View", and export it to M:\Departments\Marketing\list.json # Add-RegentExportConfig ` # -JobName "Marketing Contact Export" ` # -CrmInstance "CRMRECRUIT" ` # -ViewName "Regent | Marketing List View" ` # -IsUserVeiw $true ` # -ExportPath "M:\Departments\Marketing\list.json" # ############################## [CmdletBinding()] param ( [string] $PathToConfig = ".\AdvancedFindExport.config.json", [Parameter(Mandatory = $true)] [string] $JobName, [ValidateSet("CRMRECRUIT", "CRMRECRUITTEST", "CRMADVISE", "CRMADVISETEST")] [Parameter(Mandatory = $true)] $CrmInstance, [Parameter(Mandatory = $true)] [string] $ViewName, [Parameter(Mandatory = $true)] [bool] $IsUserView, [Parameter(Mandatory = $true)] [string] $ExportPath ) # Structure the params in an object and wrap it in an array to be added to the existing array $params = New-Object psobject -Property @{ JobName = $JobName; CrmInstance = $CrmInstance; ViewName = $ViewName; IsUserView = $IsUserView; ExportPath = $ExportPath; } if (Test-Path $PathToConfig) { Write-Host "Configuration file found. Adding job $JobName" $existingConfig = Get-Content $PathToConfig | ConvertFrom-Json } if ($existingConfig) { if ($existingConfig | Where-Object -Property "JobName" -EQ -Value $params.JobName) { throw "A job with that name already exists. Please remove it or use another name." } if ($existingConfig.GetType().Name -eq "PSCustomObject") { $json = @($existingConfig, $params) | ConvertTo-Json } else { $json = ($existingConfig + @($params)) | ConvertTo-Json } } else { Write-Host "No configuration file found. Creating a new one..." $json = $params | ConvertTo-Json } $json > $PathToConfig Write-Host -ForegroundColor Green "Configuration for job $JobName added to $PathToConfig" } function Get-RegentExportConfig { ############################## #.SYNOPSIS #Returns the specified Advanced Find Export job from a configuration file, or returns all of them. # #.DESCRIPTION #This command deserializes the JSON of a configuration file into an object that can be used with other Powershell commands. If a JobName is provided, only that job is returned (the JobName is not case-sensitive). Otherwise, all jobs are returned as an array. The results of this command can be piped into several other of the utilities to succinctly work with a configuration file. # #.PARAMETER PathToConfig #The path to the configuration file. If the file doesn't not exist, an error will be thrown. # #.PARAMETER JobName #The name of the export job. If none is provided, all jobs will be returned. # #.EXAMPLE #To inspect all jobs in a given configuration file: #Get-RegentExportConfig -PathToConfig C:\path\to\AdvancedFindExport.config.json | Format-Table # #.EXAMPLE #To export a specific find immediately. #Get-RegentExportConfig -JobName "MyUnimportantJob" | Export-RegentRecordsByFind # ############################## [CmdletBinding()] param ( [string] $PathToConfig = ".\AdvancedFindExport.config.json", [string] $JobName ) $originalErrorActionPreference = $ErrorActionPreference try { $ErrorActionPreference = "Stop" $existingConfig = Get-Content $PathToConfig | ConvertFrom-Json } catch { throw $_ } finally { $ErrorActionPreference = $originalErrorActionPreference } if ($JobName) { $result = $existingConfig | Where-Object -Property JobName -EQ -Value $JobName if (-not $result) { Write-Host "No Job ""$JobName"" was found." } return $result } else { return $existingConfig } } function Remove-RegentExportConfig { ############################## #.SYNOPSIS #Removes a specified export job from a config file # #.DESCRIPTION #Deletes the JSON entry for a specific job in the config file. The JobName is not case-sensitive. # #.PARAMETER PathToConfig # The path to the config file. Defaults to a file in the current directory called AdvancedFindExport.config.json # #.PARAMETER JobName # The name of the job to be removed # #.EXAMPLE #To remove a specific job from a config file in the current directory: #Remove-RegentExportConfig -JobName "MyUnimportantJob" #.EXAMPLE #To remove a job from a config file in another directory. Note that the "-JobName" is implied, and therefore optional #Remove-RegentExportConfig "MyOthernimportantJob" -PathToConfig C:\Path\To\config.json ############################## [CmdletBinding()] param ( [string] $PathToConfig = ".\AdvancedFindExport.config.json", [Parameter(Mandatory = $true)] [string] $JobName ) $originalErrorActionPreference = $ErrorActionPreference try { $ErrorActionPreference = "Stop" $existingConfig = Get-Content $PathToConfig | ConvertFrom-Json $filteredConfig = $existingConfig | Where-Object -Property "JobName" -NE -Value $JobName $filteredConfig | ConvertTo-Json > $PathToConfig Write-Host "Job $JobName was removed." } catch { throw $_ } finally { $ErrorActionPreference = $originalErrorActionPreference } } |