/// <summary> /// Carga una sentencia de ejecución de scripts /// </summary> private BaseSentence LoadScriptSentence(MLNode rootML) { ExecuteScriptSentence sentence = new ExecuteScriptSentence(); // Asigna las propiedades AssignProperties(sentence, rootML); sentence.FileName = rootML.Attributes[TagFileName].Value.TrimIgnoreNull(); sentence.Content = rootML.Nodes[TagContent].Value.TrimIgnoreNull(); // Carga los mapeos foreach (MLNode nodeML in rootML.Nodes) { if (nodeML.Name == TagMap) { sentence.Mappings.Add(nodeML.Attributes[TagFrom].Value.TrimIgnoreNull(), nodeML.Attributes[TagTo].Value.TrimIgnoreNull()); } } // Carga los directorios foreach (MLNode nodeML in rootML.Nodes) { if (nodeML.Name == TagPath) { sentence.Paths.Add(nodeML.Attributes[TagParameterName].Value.TrimIgnoreNull(), nodeML.Attributes[TagValue].Value.TrimIgnoreNull()); } } // Devuelve la sentencia leida return(sentence); }
/// <summary> /// Procesa un script /// </summary> private async Task ProcessScriptAsync(BlockLogModel parent, ExecuteScriptSentence sentence, NormalizedDictionary <object> parameters, CancellationToken cancellationToken) { // Ejecuta la sentencia using (BlockLogModel block = parent.CreateBlock(LogModel.LogType.Info, $"Start execute script")) { try { PowerShellController controller = new PowerShellController(this, Step); // Ejecuta el script await controller.ExecuteAsync(block, sentence, parameters); // Muestra los errores if (controller.Errors.Count > 0) { AddErrors(block, controller.Errors); } else { block.Info("End script execution"); } } catch (Exception exception) { AddError(block, $"Error when execute script Powershell. {exception.Message}"); } } }
/// <summary> /// Ejecuta una sentencia de un script de Powershell /// </summary> internal async Task ExecuteAsync(BlockLogModel block, ExecuteScriptSentence sentence, NormalizedDictionary <object> parameters) { if (!string.IsNullOrWhiteSpace(sentence.Content)) { await ExecuteContentAsync(block, sentence.Content, parameters, sentence); } else if (!string.IsNullOrWhiteSpace(sentence.FileName)) { string fileName = Step.Project.GetFullFileName(sentence.FileName); if (!System.IO.File.Exists(fileName)) { Errors.Add($"Can't find the file '{fileName}'"); } else { await ExecuteFileAsync(block, fileName, parameters, sentence); } } else { Errors.Add("There is no content nor filename at sentence"); } }
/// <summary> /// Añade los parámetros al powershell que se va a ejecutar /// </summary> private bool AddParameters(PowerShellManager manager, NormalizedDictionary <object> parameters, ExecuteScriptSentence sentence, out string error) { // Inicializa los argumentos de salida error = string.Empty; // Añade los parámetros foreach ((string key, string value) in sentence.Mappings.Enumerate()) { if (parameters.ContainsKey(key)) { manager.AddParameter(value, parameters[key]); } } // Añade los directorios foreach ((string key, string value) in sentence.Paths.Enumerate()) { manager.AddParameter(key, Step.Project.GetFullFileName(value)); } // Devuelve el valor que indica si ha habido algún error return(string.IsNullOrEmpty(error)); }
/// <summary> /// Ejecuta un script de powershell /// </summary> private async Task ExecuteContentAsync(BlockLogModel block, string content, NormalizedDictionary <object> parameters, ExecuteScriptSentence sentence) { PowerShellManager manager = new PowerShellManager(); // Carga el script en memoria manager.LoadScript(content); // Asigna los parámetros if (!AddParameters(manager, parameters, sentence, out string error)) { Errors.Add(error); } else { // Ejecuta await manager.ExecuteAsync(); // Comprueba los errores if (manager.Errors.Count > 0) { Errors.AddRange(manager.Errors); } else if (manager.OutputItems.Count > 0) { foreach (object output in manager.OutputItems) { block.Info($"{manager.OutputItems.IndexOf(output).ToString()}: {output?.ToString()}"); } } } }
/// <summary> /// Ejecuta un archivo de Powershell /// </summary> private async Task ExecuteFileAsync(BlockLogModel block, string fileName, NormalizedDictionary <object> parameters, ExecuteScriptSentence sentence) { await ExecuteContentAsync(block, LibHelper.Files.HelperFiles.LoadTextFile(fileName), parameters, sentence); }