File size: 1,765 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Management.Infrastructure;
using Microsoft.PowerShell.Cim;

namespace Microsoft.PowerShell.Cmdletization.Cim
{
    /// <summary>
    /// Job wrapping invocation of a CreateInstance or ModifyInstance intrinsic CIM method.
    /// </summary>
    internal abstract class PropertySettingJob<T> : MethodInvocationJobBase<T>
    {
        internal PropertySettingJob(CimJobContext jobContext, bool passThru, CimInstance objectToModify, MethodInvocationInfo methodInvocationInfo)
                : base(
                    jobContext,
                    passThru,
                    objectToModify.ToString(),
                    methodInvocationInfo)
        {
        }

        internal void ModifyLocalCimInstance(CimInstance cimInstance)
        {
            foreach (MethodParameter methodParameter in this.GetMethodInputParameters())
            {
                CimValueConverter.AssertIntrinsicCimType(methodParameter.ParameterType);
                CimProperty propertyBeingModified = cimInstance.CimInstanceProperties[methodParameter.Name];
                if (propertyBeingModified != null)
                {
                    propertyBeingModified.Value = methodParameter.Value;
                }
                else
                {
                    CimProperty propertyBeingAdded = CimProperty.Create(
                        methodParameter.Name,
                        methodParameter.Value,
                        CimValueConverter.GetCimTypeEnum(methodParameter.ParameterType),
                        CimFlags.None);
                    cimInstance.CimInstanceProperties.Add(propertyBeingAdded);
                }
            }
        }
    }
}