public void Connect(Configuration cfg, WrapperDto dto) { try { int i = 0; while (this.CheckForInternetConnection(cfg) == false && i <= cfg.maxConnectionCheck) { i++; Log.Debug($"NO Internet Connection: wait {0} sec for next try", cfg.treadSleepSeconds); System.Threading.Thread.Sleep(cfg.treadSleepSeconds * 1000); } if (i >= cfg.maxConnectionCheck) { Log.Information("NO Internet Connection"); throw new HttpsConneException("Internet Connection not available"); } this.GetFile(cfg, dto); } catch (Exception ex) { throw new HttpsConneException(ex.Message); } }
private string[] ExecutePS(WrapperDto dto) { Log.Debug("Executing PowerShell"); string[] output = new string[3]; ProcessStartInfo startInfo = new ProcessStartInfo { CreateNoWindow = true, UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden, RedirectStandardOutput = true, RedirectStandardError = true, FileName = "PowerShell.exe", Arguments = String.Format($"-ExecutionPolicy ByPass -WindowStyle Hidden -File \"{0}\"", dto.scriptPath) }; Log.Debug("Arguments: " + startInfo.Arguments); using (Process exeProcess = Process.Start(startInfo)) { output[0] = exeProcess.StandardOutput.ReadToEnd(); output[1] = exeProcess.StandardError.ReadToEnd(); exeProcess.WaitForExit(); output[2] = exeProcess.ExitCode.ToString(); } Log.Debug("StandardOutput: " + output[0]); Log.Debug("StandardError: " + output[1]); Log.Debug("ExitCode: " + output[2]); return(output); }
private void PreCheckFileSystem(Configuration cfg, WrapperDto dto) { dto.tempDirPath = AppDomain.CurrentDomain.BaseDirectory + cfg.tempDir; if (!Directory.Exists(dto.tempDirPath)) { Log.Debug("Create Temp Dir"); Directory.CreateDirectory(dto.tempDirPath); } }
private void GetFile(Configuration cfg, WrapperDto dto) { HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(cfg.scriptUrl); httpRequest.Method = WebRequestMethods.Http.Get; HttpWebResponse httpResponse = null; Stream httpResponseStream = null; try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); httpResponseStream = httpResponse.GetResponseStream(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int bytesRead = 0; dto.scriptPath = dto.tempDirPath + Path.GetFileName(cfg.scriptUrl); if (File.Exists(dto.scriptPath)) { File.Delete(dto.scriptPath); } using (FileStream fileStream = File.Create(dto.scriptPath)) { while ((bytesRead = httpResponseStream.Read(buffer, 0, bufferSize)) != 0) { fileStream.Write(buffer, 0, bytesRead); } } Log.Information("File download successfully"); } catch (Exception ex) { //On downloading exception we need to catch and execute the local script if any dto.httpError = ex.Message; dto.scriptPath = dto.tempDirPath + cfg.scriptName; Log.Error("Error on downloading script via HTTPS.. will check it into Temp directory"); Log.Error(ex.Message); } finally { if (httpResponseStream != null) { httpResponseStream.Flush(); httpResponseStream.Close(); } if (httpResponse != null) { httpResponse.Close(); } } }
public void Execute(Configuration cfg, WrapperDto dto) { if (Path.GetExtension(dto.scriptPath).Equals(cfg.possibleFileExt[0])) { Log.Debug("Script is a PowerShell file"); if (File.Exists(dto.scriptPath)) { this.ExecutePS(dto); } else { Log.Error("Script file not found in Temp directory. skip execution"); } } }
public void DoCheck(Configuration cfg, WrapperDto dto) { PreCheckFileSystem(cfg, dto); }