protected override void ProcessRecord() { base.ProcessRecord(); var logger = new CmdletLogger(TreatWarningsAsErrors); try { logger.LogInfo("About to start importing data from Dynamics365"); var manager = new Dynamics365DataManager(); var cancellationTokenSource = new CancellationTokenSource(); var importConfig = new CrmImportConfig(); if (!string.IsNullOrWhiteSpace(ConfigFilePath)) { if (!File.Exists(ConfigFilePath)) { WriteWarning($"Import config file path does not exist, will be ignored {ConfigFilePath}"); } else { importConfig = CrmImportConfig.GetConfiguration(ConfigFilePath); } } PopulateConfigFile(importConfig); if (!Directory.Exists(JsonFolderPath)) { WriteWarning($"JsonFolderPath {JsonFolderPath} does not exist, cannot continue!"); throw new DirectoryNotFoundException($"JsonFolderPath {JsonFolderPath} does not exist, cannot continue!"); } CrmSchemaConfiguration schemaConfig = null; if (CsvImport) { if (string.IsNullOrWhiteSpace(SchemaFilePath)) { throw new ConfigurationException("Schema file is required for CSV Import!"); } schemaConfig = CrmSchemaConfiguration.ReadFromFile(SchemaFilePath); logger.LogInfo("Using Csv import"); } else { logger.LogInfo("Using JSon import"); } if (MaxThreads > 1) { var result = manager.StartImport(importConfig, logger, cancellationTokenSource, ConnectionString, MaxThreads, CsvImport, schemaConfig) .ContinueWith(a => { logger.LogInfo("Dynamics365 data import completed successfully."); }, cancellationTokenSource.Token); result.Wait(cancellationTokenSource.Token); } else { manager.StartSingleThreadedImport(importConfig, new CmdletLoggerPS(this, TreatWarningsAsErrors), cancellationTokenSource, ConnectionString, CsvImport, schemaConfig); } } catch (Exception exception) { var errorMessage = $"Dynamics365 data import failed: {exception.Message}"; logger.LogVerbose(errorMessage); logger.LogError(errorMessage); throw; } }