/// <summary>
        /// Initialize all values, as well as start the pipeline and await for data
        /// </summary>
        /// <param name="moduleDefinition">The module definition for the classifier</param>
        /// <param name="rule">The rule definition for the rule that this represents</param>
        /// <param name="propertyDefinition">The property definition for the property that this rule modifies</param>
        public PowerShellRuleHoster(
            IFsrmPipelineModuleDefinition moduleDefinition,
            IFsrmClassificationRule rule,
            IFsrmPropertyDefinition propertyDefinition
            )
        {
            m_propertyBagWriter = new BlockablePropertyBagEnumerator();
            m_ruleName          = rule.Name;

            // create the waitHandles and initialize them
            // *note the pipeline waitHandle is created in CreateAndBeginPipeline
            m_powershellPaused = new WaitHandle[(int)m_waitHandleIndex.Count];
            m_powershellPaused[(int)m_waitHandleIndex.Enumerator] = m_propertyBagWriter.RequestedDataWaitHandle;

            // cache the method info for adding the GetStream to PropertyBag
            Type extensionClassForPropertyBag = typeof(ExtensionClassForPropertyBag);

            m_getStreamMethodInfo = extensionClassForPropertyBag.GetMethod("GetStream");


            string fileText = GetScriptText(rule);

            // this enables users to create a scriptblock rather then enumerate over the PropertyBagList
            m_scriptText = "$PropertyBagList | &{" + fileText + "}\n";

            // construct the runspace and set global proxy values for powershell script to use
            m_runSpace = RunspaceFactory.CreateRunspace();
            m_runSpace.Open();
            m_runSpace.SessionStateProxy.SetVariable("ModuleDefinition", moduleDefinition);
            m_runSpace.SessionStateProxy.SetVariable("PropertyBagList", m_propertyBagWriter);
            m_runSpace.SessionStateProxy.SetVariable("Rule", rule);
            m_runSpace.SessionStateProxy.SetVariable("PropertyDefinition", propertyDefinition);

            //launch the pipeline creation
            CreateAndBeginPipeline();
        }
Ejemplo n.º 2
0
        /*++
         *
         *  Routine DisplayPropertyDefinition
         *
         * Description:
         *
         *  This routine prints an FSRM property definitions
         *
         * Arguments:
         *
         *  PropertyDefinition - An IFsrmPropertyDefinition object to pring
         *
         * Return value:
         *
         *  void
         *
         * Notes:
         *
         *
         * --*/
        static void DisplayPropertyDefinition(
            IFsrmPropertyDefinition PropertyDefinition
            )
        {
            string description;
            string name;
            Array  possibleValues;
            Array  valueDescriptions;

            _FsrmPropertyDefinitionType type;

            // get the members of the PropertyDefinition
            description       = PropertyDefinition.Description;
            name              = PropertyDefinition.Name;
            type              = PropertyDefinition.Type;
            possibleValues    = PropertyDefinition.PossibleValues;
            valueDescriptions = PropertyDefinition.ValueDescriptions;

            //getting all the values will have succeeded
            //print all values

            Console.WriteLine("\tName:\t\t{0}", name);
            Console.WriteLine("\tDefinition:\t{0}", description);
            Console.Write("\tType:\t\t");
            switch (type)
            {
            case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Unknown:
                Console.Write("Unknown\n");
                break;

            case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_OrderedList:
                Console.Write("Ordered List\n");

                //For an ordered list, print all possible values
                for (int i = 0; i < possibleValues.Length && i < valueDescriptions.Length; ++i)
                {
                    Console.Write("\t\t{0} Value:\t {1}\n", i + 1, possibleValues.GetValue(i));
                    Console.Write("\t\t{0} Description:\t {1}\n", i + 1, valueDescriptions.GetValue(i));
                }
                break;

            case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_MultiChoiceList:
                Console.Write("Multichoice List\n");

                //For a multichoice property, print all possible values
                for (int i = 0; i < possibleValues.Length && i < valueDescriptions.Length; ++i)
                {
                    Console.Write("\t\t{0} Value:\t {1}\n", i + 1, possibleValues.GetValue(i));
                    Console.Write("\t\t{0} Description:\t {1}\n", i + 1, valueDescriptions.GetValue(i));
                }
                break;

            case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Int:
                Console.Write("Int\n");
                break;

            case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Bool:
                Console.Write("Bool\n");
                break;

            case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Date:
                Console.Write("Date\n");
                break;

            case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_MultiString:
                Console.Write("Multistring\n");
                break;

            case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_String:
                Console.Write("String\n");
                break;

            default:
                Console.Write("Error type {0} not defined\n", type);
                break;
            }
        }
        /// <summary>
        /// Initialize all values, as well as start the pipeline and await for data
        /// </summary>
        /// <param name="moduleDefinition">The module definition for the classifier</param>
        /// <param name="rule">The rule definition for the rule that this represents</param>
        /// <param name="propertyDefinition">The property definition for the property that this rule modifies</param>
        public PowerShellRuleHoster(
            IFsrmPipelineModuleDefinition moduleDefinition,
            IFsrmClassificationRule rule,
            IFsrmPropertyDefinition propertyDefinition
            )
        {
            m_propertyBagWriter = new BlockablePropertyBagEnumerator();
            m_ruleName = rule.Name;

            // create the waitHandles and initialize them
            // *note the pipeline waitHandle is created in CreateAndBeginPipeline
            m_powershellPaused = new WaitHandle[(int)m_waitHandleIndex.Count];
            m_powershellPaused[(int)m_waitHandleIndex.Enumerator] = m_propertyBagWriter.RequestedDataWaitHandle;

            // cache the method info for adding the GetStream to PropertyBag
            Type extensionClassForPropertyBag = typeof(ExtensionClassForPropertyBag);
            m_getStreamMethodInfo = extensionClassForPropertyBag.GetMethod("GetStream");

            string fileText = GetScriptText(rule);

            // this enables users to create a scriptblock rather then enumerate over the PropertyBagList
            m_scriptText = "$PropertyBagList | &{" + fileText + "}\n";

            // construct the runspace and set global proxy values for powershell script to use
            m_runSpace = RunspaceFactory.CreateRunspace();
            m_runSpace.Open();
            m_runSpace.SessionStateProxy.SetVariable("ModuleDefinition", moduleDefinition);
            m_runSpace.SessionStateProxy.SetVariable("PropertyBagList", m_propertyBagWriter);
            m_runSpace.SessionStateProxy.SetVariable("Rule", rule);
            m_runSpace.SessionStateProxy.SetVariable("PropertyDefinition", propertyDefinition);

            //launch the pipeline creation
            CreateAndBeginPipeline();
        }
        /*++

            Routine DisplayPropertyDefinition

        Description:

            This routine prints an FSRM property definitions

        Arguments:

            PropertyDefinition - An IFsrmPropertyDefinition object to pring

        Return value:

            void

        Notes:

        --*/
        static void DisplayPropertyDefinition(
            IFsrmPropertyDefinition PropertyDefinition
            )
        {
            string description;
            string name;
            Array possibleValues;
            Array valueDescriptions;

            _FsrmPropertyDefinitionType type;

            // get the members of the PropertyDefinition
            description = PropertyDefinition.Description;
            name = PropertyDefinition.Name;
            type = PropertyDefinition.Type;
            possibleValues = PropertyDefinition.PossibleValues;
            valueDescriptions = PropertyDefinition.ValueDescriptions;

            //getting all the values will have succeeded
            //print all values

            Console.WriteLine( "\tName:\t\t{0}", name );
            Console.WriteLine( "\tDefinition:\t{0}", description );
            Console.Write( "\tType:\t\t" );
            switch (type) {
                case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Unknown:
                    Console.Write( "Unknown\n" );
                    break;

                case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_OrderedList:
                    Console.Write( "Ordered List\n" );

                    //For an ordered list, print all possible values
                    for (int i = 0; i < possibleValues.Length && i < valueDescriptions.Length; ++i) {
                        Console.Write( "\t\t{0} Value:\t {1}\n", i + 1, possibleValues.GetValue( i ) );
                        Console.Write( "\t\t{0} Description:\t {1}\n", i + 1, valueDescriptions.GetValue( i ) );
                    }
                    break;

                case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_MultiChoiceList:
                    Console.Write( "Multichoice List\n" );

                    //For a multichoice property, print all possible values
                    for (int i = 0; i < possibleValues.Length && i < valueDescriptions.Length; ++i) {
                        Console.Write( "\t\t{0} Value:\t {1}\n", i + 1, possibleValues.GetValue( i ) );
                        Console.Write( "\t\t{0} Description:\t {1}\n", i + 1, valueDescriptions.GetValue( i ) );
                    }
                    break;

                case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Int:
                    Console.Write( "Int\n" );
                    break;

                case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Bool:
                    Console.Write( "Bool\n" );
                    break;

                case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Date:
                    Console.Write( "Date\n" );
                    break;

                case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_MultiString:
                    Console.Write( "Multistring\n" );
                    break;

                case _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_String:
                    Console.Write( "String\n" );
                    break;

                default:
                    Console.Write( "Error type {0} not defined\n", type );
                    break;

            }
        }
        /// <summary>
        /// Gets the value for the property and rule to apply
        /// The caller will handle aggregating multiple values for a single property
        ///
        /// *Note
        /// This function need not be implemented if only supporting yes no classifiers
        /// </summary>
        /// <param name="property">The name of the property that this rule can modify</param>
        /// <param name="Value">The value to set the property to</param>
        /// <param name="idRule">The id of the rule to process</param>
        /// <param name="idPropDef">The id of the definition that this rule can modify</param>
        public void GetPropertyValueToApply(
            string property,
            out string Value,
            Guid idRule,
            Guid idPropDef
            )
        {
            PowerShellRuleHoster rule = m_dictAllRules[idRule];

            // run the powershell script over the propertyBag
            rule.StepPipeline(m_propertyBag);

            // If the rule does not modify the property value throw an error that specifies this
            // You must specify the HR that is being specified below if you wish to not modify the rule's value
            if (rule.RuleNoApply)
            {
                throw new COMException("Value does not apply", FSRM_E_NO_PROPERTY_VALUE); //, FSRM_E_NO_PROPERTY_VALUE);
            }

            //convert the powershell value into the approrpiate value
            try
            {
                IFsrmPropertyDefinition propertyDefinition = m_dictAllProperties[idPropDef];

                if (propertyDefinition.Type == _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Bool)
                {
                    //convert from true/false to "1","0"
                    Value = (bool)rule.PropertyValue ? "1" : "0";
                }
                else if (propertyDefinition.Type == _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Date)
                {
                    // convert from datetime to str
                    DateTime time     = (DateTime)rule.PropertyValue;
                    long     filetime = time.ToFileTimeUtc();
                    Value = filetime.ToString(CultureInfo.InvariantCulture);
                }
                else if (propertyDefinition.Type == _FsrmPropertyDefinitionType.FsrmPropertyDefinitionType_Int)
                {
                    Value = rule.PropertyValue.ToString();
                }
                else
                {
                    Value = (string)rule.PropertyValue;
                }
            }
            catch (Exception ex)
            {
                String message = "";

                if (rule.PropertyValue == null)
                {
                    message = String.Format(CultureInfo.InvariantCulture,
                                            "PowerShell Classifier for rule [{0}] and file [{1}], received null value from powershell, with error [{2}]",
                                            rule.RuleName,
                                            m_propertyBag.VolumeName + m_propertyBag.RelativePath + "\\" + m_propertyBag.Name,
                                            ex.Message);
                }
                else
                {
                    //Assuming this error is due to casting
                    message = String.Format(CultureInfo.InvariantCulture,
                                            "PowerShell Classifier for rule [{0}] and file [{1}], failed to convert the property recieved from powershell of type [{2}] and value [{3}] into the property [{4}], with error [{5}]",
                                            rule.RuleName,
                                            m_propertyBag.VolumeName + m_propertyBag.RelativePath + "\\" + m_propertyBag.Name,
                                            rule.PropertyValue.GetType().ToString(),
                                            rule.PropertyValue.ToString(),
                                            property,
                                            ex.Message);
                }

                m_propertyBag.AddMessage(message);

                throw new COMException(message, ex.InnerException);
            }
        }