public void RecordStream(PoshJob poshJob, object sender, DataAddedEventArgs e) { var type = sender.GetType(); LogPostParameters logPostParameters = new LogPostParameters(); InformationalRecord record; if (type == typeof(PSDataCollection <VerboseRecord>)) { record = ((PSDataCollection <VerboseRecord>)sender)[e.Index]; logPostParameters.message = record.Message; logPostParameters.level = JobLogLevelEnum.Verbose; } if (type == typeof(PSDataCollection <DebugRecord>)) { record = ((PSDataCollection <DebugRecord>)sender)[e.Index]; logPostParameters.message = record.Message; logPostParameters.level = JobLogLevelEnum.Debug; } if (type == typeof(PSDataCollection <ErrorRecord>)) { FaultJob(poshJob.JobUID); logPostParameters.level = JobLogLevelEnum.Error; var errorRecord = ((PSDataCollection <ErrorRecord>)sender)[e.Index]; logPostParameters.Exception = errorRecord.Exception.Message; logPostParameters.message = errorRecord.ScriptStackTrace; //this is where the error occured. Not sure if there is a better message for error streams } if (type == typeof(PSDataCollection <WarningRecord>)) { logPostParameters.level = JobLogLevelEnum.Warning; record = ((PSDataCollection <WarningRecord>)sender)[e.Index]; logPostParameters.message = record.Message; } logPostParameters.jobUid = poshJob.JobUID; LogPoshMessage(logPostParameters); }
public async Task <PSDataCollection <PSObject> > StartScript(PoshJob poshJob) //maybe add withRunspace parameter in the future. I have no idea what a runspace provides. { PSDataCollection <PSDataCollection <PSObject> > output = new PSDataCollection <PSDataCollection <PSObject> >(); output.DataAdded += delegate(object sender, DataAddedEventArgs e) { RecordOutput(poshJob, sender, e); }; return(await poshJob.PoshInstance.InvokeAsync <PSDataCollection <PSObject>, PSDataCollection <PSObject> >(null, output)); }
public PoshJob RunJob(PoshJob poshJob) { if (poshJob.Parameters != null && poshJob.Parameters.Count != 0) { poshJob.PoshInstance.AddParameters(poshJob.Parameters); } poshJob.RunningJob = StartScript(poshJob); Log.Information($"Job {poshJob.JobUID} started."); return(poshJob); }
public void RecordOutput(PoshJob poshJob, object sender, DataAddedEventArgs e) { var record = ((PSDataCollection <PSDataCollection <PSObject> >)sender)[e.Index]; JobOutputPostParameters jopp = new JobOutputPostParameters(); jopp.JobUid = poshJob.JobUID; jopp.Type = record[0].TypeNames[0]; jopp.JsonValue = JsonSerializer.Serialize(record[0].BaseObject); string url = automationHelper.GetUrl() + $"/api/Job/{poshJob.JobUID}/Output"; automationHelper.PostWebCall(url, jopp); Log.Information($"{poshJob.JobUID} Output: Type - {record[0].TypeNames[0]} Value - {record[0].BaseObject}"); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { string url = _automationHelper.GetUrl() + $"/api/Job/Queued?language={ScriptLanguageEnum.PowerShellCore}"; VMScriptJob newJob = null; try { newJob = _automationHelper.GetWebCall <VMScriptJob>(url); } catch (Exception e) { if (null != e.InnerException) { if (e.InnerException.Message.Equals("No connection could be made because the target machine actively refused it.")) { _logger.LogError("Something went wrong trying to get queued job."); } } else { _logger.LogInformation("Server is offline"); } } if (newJob != null) { PoshJob job = new PoshJob(); job.JobUID = newJob.Job.JobUid; job.Script = newJob.Script; if (newJob.Parameters != null) { job.Parameters = newJob.Parameters; } if (job.Script != null) { _poshJobManager.QueuePendingJob(job); } else { _logger.LogWarning("No script version found."); } } await Task.Delay(_automationHelper.GetGetQueuedJobDelay(), stoppingToken); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { //_logger.LogInformation("Monitor Running Jobs at: {time}", DateTimeOffset.Now); PoshJob job1 = new PoshJob(); job1.Script = TestScript; Hashtable parameters = new Hashtable(); parameters.Add("stringParam", "stringValue"); parameters.Add("intParam", 1); job1.Parameters = parameters; job1.JobUID = Guid.NewGuid(); PoshJob job2 = new PoshJob(); job2.Script = TestScript2; job2.JobUID = Guid.NewGuid(); _poshJobManager.QueuePendingJob(job2); await Task.Delay(100000, stoppingToken); } }
public PoshJob ConfigureStreams(PoshJob poshJob) { //capture stream output poshJob.PoshInstance.Streams.Verbose.DataAdded += delegate(object sender, DataAddedEventArgs e) { RecordStream(poshJob, sender, e); }; poshJob.PoshInstance.Streams.Debug.DataAdded += delegate(object sender, DataAddedEventArgs e) { RecordStream(poshJob, sender, e); }; poshJob.PoshInstance.Streams.Error.DataAdded += delegate(object sender, DataAddedEventArgs e) { RecordStream(poshJob, sender, e); }; poshJob.PoshInstance.Streams.Information.DataAdded += delegate(object sender, DataAddedEventArgs e) { RecordStream(poshJob, sender, e); }; poshJob.PoshInstance.Streams.Warning.DataAdded += delegate(object sender, DataAddedEventArgs e) { RecordStream(poshJob, sender, e); }; return(poshJob); }
public void StartPendingJob() { if (!HasPendingJob()) { return; } /*if(!_poshManager.RunspaceAvailable()) * { * return; * }*/ PowerShell ps = _poshManager.GetPowerShell(); if (ps == null) { Log.Warning("All PowerShell instances are in use."); return; } PoshJob pendingJob; pendingJobQueue.TryDequeue(out pendingJob); if (pendingJob == null) { return; } try { ps = InitializeAutomationComponents(ps); ps.AddScript(pendingJob.Script); pendingJob.PoshInstance = ps; pendingJob = ConfigureStreams(pendingJob); PoshJob runningJob = RunJob(pendingJob); if (runningJob == null) { retryJobs.Enqueue(pendingJob); } else { bool success = runningJobs.TryAdd(pendingJob.JobUID, runningJob); if (!success) { //this is bad and hopefully never happens. Log.Error("Unable to queue the running job, this job will not be monitored."); } else { string url = automationHelper.GetUrl() + $"/api/Job/{runningJob.JobUID}/Start"; automationHelper.PostWebCall(url, null); } } } catch (Exception e) { ProblemJob problemJob = new ProblemJob(); problemJob.Exception = e; problemJob.Job = pendingJob; if (!problemJobs.Contains(problemJob)) { problemJobs.Enqueue(problemJob); } } }
public void QueuePendingJob(PoshJob job) { pendingJobQueue.Enqueue(job); }