Esempio n. 1
0
        public virtual object Execute(T cmdlet)
        {
            var restCommand = RestService.ReverseLookup[cmdlet.GetType()];
            var result      = new PowershellReponse();
            AsynchronouslyEnumerableList <ErrorRecord> errors = null;

            using (var dps = new DynamicPowershell(RestService.RunspacePool)) {
                if (cmdlet.Session != null && cmdlet.Session.HasRole("password_must_be_changed") && typeof(T) != typeof(SetServicePassword))
                {
                    result.Warnings = new string[] {
                        "WARNING: USER PASSWORD SHOULD BE CHANGED.\r\n"
                    };
                }
                var dynamicResult = dps.Invoke(restCommand.Name, _persistableElements, cmdlet, restCommand.DefaultParameters, restCommand.ForcedParameters, out errors);
                result.Output = dynamicResult.ToArray();
                result.LastIsTerminatingError = dynamicResult.LastIsTerminatingError;
            }

            if (errors != null && errors.Any())
            {
                result.Error = errors.Select(e => new RemoteError {
                    Category         = e.CategoryInfo.Category,
                    Message          = e.FullyQualifiedErrorId,
                    ExceptionType    = e.Exception.GetType().Name,
                    ExceptionMessage = e.Exception.Message,
                }).ToArray();
            }

            return(result);
        }
Esempio n. 2
0
        protected virtual void ProcessRecordViaRest()
        {
            var client = new JsonServiceClient(ServiceUrl);

            if (Credential != null)
            {
                client.SetCredentials(Credential.UserName, Credential.Password.ToUnsecureString());
            }
            PowershellReponse response = null;

            try {
                // try connecting where the URL is the base URL
                response = client.Send <PowershellReponse>((this as T));

                if (response != null)
                {
                    if (!response.Warnings.IsNullOrEmpty())
                    {
                        foreach (var warning in response.Warnings)
                        {
                            WriteWarning(warning);
                        }
                    }

                    if (!response.Error.IsNullOrEmpty())
                    {
                        foreach (var error in response.Error.TakeAllBut(1))
                        {
                            WriteError(new ErrorRecord(new Exception("{0} - {1}".format(error.ExceptionType, error.ExceptionMessage)), error.Message, error.Category, null));
                        }
                    }

                    if (!response.Output.IsNullOrEmpty())
                    {
                        foreach (var ob in response.Output)
                        {
                            WriteObject(ob);
                        }
                    }

                    if (response.LastIsTerminatingError)
                    {
                        var error = response.Error.Last();
                        ThrowTerminatingError(new ErrorRecord(new Exception("{0} - {1}".format(error.ExceptionType, error.ExceptionMessage)), error.Message, error.Category, null));
                    }
                }
            } catch (WebServiceException wse) {
                switch (wse.StatusCode)
                {
                case 401:
                    if (Credential == null)
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception("Not Authenticated: you must supply credentials to access the remote service", wse), "", ErrorCategory.PermissionDenied, null));
                        return;
                    }
                    else
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception("Invalid Authentication: the given credentials are not valid with the remote service", wse), "", ErrorCategory.PermissionDenied, null));
                        return;
                    }

                case 403:
                    ThrowTerminatingError(new ErrorRecord(new Exception("Not Authorized: You are not authorized to access that remote service", wse), "", ErrorCategory.PermissionDenied, null));
                    return;

                case 404:
                    ThrowTerminatingError(new ErrorRecord(new Exception("Unknown Service: no remote service for {0} found at {1}".format(GetType().Name, ServiceStack.Text.StringExtensions.WithTrailingSlash(client.SyncReplyBaseUri) + GetType().Name), wse), "", ErrorCategory.ResourceUnavailable, null));
                    return;
                }

                ThrowTerminatingError(new ErrorRecord(new Exception("Unable to call remote cmdlet -- error: {0}".format(wse.Message), wse), "", ErrorCategory.NotSpecified, null));
            }
        }