private void SetAllowedMethods(XTable.CorsRule corsRule, string[] allowedMethods)
        {
            corsRule.AllowedMethods = XTable.CorsHttpMethods.None;

            if (null != allowedMethods)
            {
                foreach (var method in allowedMethods)
                {
                    XTable.CorsHttpMethods allowedCorsMethod = XTable.CorsHttpMethods.None;
                    if (Enum.TryParse <XTable.CorsHttpMethods>(method, true, out allowedCorsMethod))
                    {
                        corsRule.AllowedMethods |= allowedCorsMethod;
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidHTTPMethod, method));
                    }
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ServiceType != StorageServiceType.Table)
            {
                ServiceProperties serviceProperties = new ServiceProperties();
                serviceProperties.Clean();
                serviceProperties.Cors = new CorsProperties();

                foreach (var corsRuleObject in this.CorsRules)
                {
                    CorsRule corsRule = new CorsRule();
                    corsRule.AllowedHeaders  = corsRuleObject.AllowedHeaders;
                    corsRule.AllowedOrigins  = corsRuleObject.AllowedOrigins;
                    corsRule.ExposedHeaders  = corsRuleObject.ExposedHeaders;
                    corsRule.MaxAgeInSeconds = corsRuleObject.MaxAgeInSeconds;
                    this.SetAllowedMethods(corsRule, corsRuleObject.AllowedMethods);
                    serviceProperties.Cors.CorsRules.Add(corsRule);
                }

                try
                {
                    Channel.SetStorageServiceProperties(ServiceType, serviceProperties,
                                                        GetRequestOptions(ServiceType), OperationContext);
                }
                catch (StorageException se)
                {
                    if ((null != se.RequestInformation) &&
                        ((int)HttpStatusCode.BadRequest == se.RequestInformation.HttpStatusCode) &&
                        (null != se.RequestInformation.ExtendedErrorInformation) &&
                        (string.Equals(InvalidXMLNodeValueError, se.RequestInformation.ErrorCode, StringComparison.OrdinalIgnoreCase) ||
                         string.Equals(InvalidXMLDocError, se.RequestInformation.ErrorCode, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new InvalidOperationException(Resources.CORSRuleError);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else //Table use old XSCL
            {
                StorageTableManagement tableChannel = new StorageTableManagement(Channel.StorageContext);

                if (!tableChannel.IsTokenCredential)
                {
                    XTable.ServiceProperties serviceProperties = new XTable.ServiceProperties();
                    serviceProperties.Clean();
                    serviceProperties.Cors = new XTable.CorsProperties();

                    foreach (var corsRuleObject in this.CorsRules)
                    {
                        XTable.CorsRule corsRule = new XTable.CorsRule();
                        corsRule.AllowedHeaders  = corsRuleObject.AllowedHeaders;
                        corsRule.AllowedOrigins  = corsRuleObject.AllowedOrigins;
                        corsRule.ExposedHeaders  = corsRuleObject.ExposedHeaders;
                        corsRule.MaxAgeInSeconds = corsRuleObject.MaxAgeInSeconds;
                        this.SetAllowedMethods(corsRule, corsRuleObject.AllowedMethods);
                        serviceProperties.Cors.CorsRules.Add(corsRule);
                    }

                    try
                    {
                        tableChannel.SetStorageTableServiceProperties(serviceProperties,
                                                                      GetTableRequestOptions(), TableOperationContext);
                    }
                    catch (XTable.StorageException se)
                    {
                        if ((null != se.RequestInformation) &&
                            ((int)HttpStatusCode.BadRequest == se.RequestInformation.HttpStatusCode) &&
                            (null != se.RequestInformation.ExtendedErrorInformation) &&
                            (string.Equals(InvalidXMLNodeValueError, se.RequestInformation.ErrorCode, StringComparison.OrdinalIgnoreCase) ||
                             string.Equals(InvalidXMLDocError, se.RequestInformation.ErrorCode, StringComparison.OrdinalIgnoreCase)))
                        {
                            throw new InvalidOperationException(Resources.CORSRuleError);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                else
                {
                    TableServiceProperties serviceProperties = tableChannel.GetProperties(this.CmdletCancellationToken);
                    serviceProperties.Cors.Clear();

                    foreach (PSCorsRule corsRule in this.CorsRules)
                    {
                        serviceProperties.Cors.Add(new TableCorsRule(
                                                       corsRule.AllowedOrigins == null ? string.Empty : string.Join(",", corsRule.AllowedOrigins),
                                                       this.CheckAndJoinAllowedMethods(corsRule.AllowedMethods),
                                                       corsRule.AllowedHeaders == null ? string.Empty : string.Join(",", corsRule.AllowedHeaders),
                                                       corsRule.ExposedHeaders == null ? string.Empty : string.Join(",", corsRule.ExposedHeaders),
                                                       corsRule.MaxAgeInSeconds));
                    }

                    try
                    {
                        tableChannel.SetProperties(serviceProperties, this.CmdletCancellationToken);
                    }
                    catch (RequestFailedException ex)
                    {
                        this.WriteExceptionError(ex);
                        throw new InvalidOperationException(Resources.CORSRuleError);
                    }
                }
            }

            if (PassThru)
            {
                WriteObject(this.CorsRules);
            }
        }