public ScriptExecutionResult ExecuteScript(ScriptJobDescriptor descriptor)
        {
            var result = new ScriptExecutionResult();

            try
            {
                var scriptType = descriptor.ScriptType.ToEnum <ScriptType>();
                var scriptBody = descriptor.ScriptBody;

                if (descriptor.RemoteExecution)
                {
                    if (!descriptor.TargetID.HasValue)
                    {
                        throw new Exception("Target not specified.");
                    }
                    var target = _targetService.GetByKey(descriptor.TargetID.Value);

                    result.Output = this.ExecuteScriptRemotely(scriptBody, scriptType,
                                                               target.ComputerName, target.PortNumber, target.TargetKey);
                }
                else
                {
                    result.Output = this.ExecuteScriptLocally(scriptBody, scriptType);
                }

                result.IsSuccessful = this.IsSuccessful(descriptor, result.Output);
            }
            catch (Exception ex)
            {
                Log.Exception(ex);

                foreach (var error in ex.GetExceptionTreeAsFlatList())
                {
                    result.Output += error.Message + Environment.NewLine;
                    result.Output += error.StackTrace;
                }
            }

            return(result);
        }