Ejemplo n.º 1
0
        /// <summary>
        /// Set the value of an existing variable
        /// or create new variable if it does not exist already.
        /// </summary>
        ///
        /// <param name="name">
        /// The name of the variable.
        /// </param>
        ///
        /// <param name="value">
        /// new value of the variable.
        /// rules apply.
        /// </param>
        ///
        /// <param name="scope">
        /// The scope of the variable. If scope is null or empty,
        /// default scope 'Process' is used.
        /// </param>
        ///
        /// <param name="force">
        /// use force operation
        /// rules apply.
        /// </param>
        ///
        /// <param name="passthru">
        /// return the variable if PassThru is set to True.
        /// rules apply.
        /// </param>
        ///
        /// <param name="action">
        /// Name of the action which is being performed. This will
        /// be displayed to the user when whatif parameter is specified.
        /// (New Environment Variable\Set Environment Variable).
        /// </param>
        ///
        /// <param name="target">
        /// Name of the target resource being acted upon. This will
        /// be displayed to the user when whatif parameter is specified.
        /// </param>
        ///
        protected void SetVariable(string name, string value, EnvironmentVariableTarget scope, bool force, bool passthru, PSCmdlet myCmdlet)
        {
            // If Force is not specified, see if the variable already exists
            // in the specified scope. If the scope isn't specified, then
            // check to see if it exists in the current scope.
            if (!force)
            {
                List <EnvironmentVariable> varFound = GetMatchingVariables(name, scope, false);
                if (myCmdlet is NewEnvironmentVariableCommand && varFound.Count > 0)
                {
                    throw new IOException(name);
                }
                else if (myCmdlet is SetEnvironmentVariableCommand && varFound.Count == 0)
                {
                    throw new ItemNotFoundException(name);
                }
            }

            // Since the variable doesn't exist or -Force was specified,
            // Call should process to validate the set with the user.

            if (ShouldProcess(name))
            {
                EnvironmentVariable newVariable = new EnvironmentVariable(name, value, scope);
                newVariable.Activate();
                if (passthru)
                {
                    WriteObject(newVariable);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets the value of an existing variable or
        /// create a new variable if it does not exist.
        /// </summary>
        ///
        /// <param name="variable">
        /// The name of the variables to retrieve.
        /// </param>
        ///
        /// <param name="value">
        /// The new value of the variable
        /// </param>
        ///
        /// <param name="scope">
        ///  The scope tp create the variable.
        /// </param>
        ///
        protected void SetVariable(string variable, string value, EnvironmentVariableTarget scope)
        {
            EnvironmentVariable envvar = new EnvironmentVariable(variable, value, scope);

            envvar.Activate();
        }