File size: 5,466 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Globalization;
using System.Management.Automation;

using Microsoft.Management.Infrastructure;

namespace Microsoft.PowerShell.Cmdletization.Cim
{
    internal sealed class CimJobContext
    {
        internal CimJobContext(
            CimCmdletInvocationContext cmdletInvocationContext,
            CimSession session,
            object targetObject)
        {
            this.CmdletInvocationContext = cmdletInvocationContext;

            this.Session = session;
            this.TargetObject = targetObject ?? this.ClassName;
        }

        public CimCmdletInvocationContext CmdletInvocationContext { get; }

        public CimSession Session { get; }

        public object TargetObject { get; }

        public string ClassName
        {
            get
            {
                return GetCimClassName(this.CmdletInvocationContext.CmdletDefinitionContext.CmdletizationClassName);
            }
        }

        public string ClassNameOrNullIfResourceUriIsUsed
        {
            get
            {
                if (this.CmdletInvocationContext.CmdletDefinitionContext.ResourceUri != null)
                {
                    return null;
                }

                return this.ClassName;
            }
        }

        public string Namespace
        {
            get
            {
                if (!string.IsNullOrEmpty(this.CmdletInvocationContext.NamespaceOverride))
                {
                    return this.CmdletInvocationContext.NamespaceOverride;
                }

                return GetCimNamespace(this.CmdletInvocationContext.CmdletDefinitionContext.CmdletizationClassName);
            }
        }

        private static void ExtractCimNamespaceAndClassName(string cmdletizationClassName, out string cimNamespace, out string cimClassName)
        {
            int indexOfLastBackslash = cmdletizationClassName.LastIndexOf('\\');
            int indexOfLastForwardSlash = cmdletizationClassName.LastIndexOf('/');
            int indexOfLastSeparator = Math.Max(indexOfLastBackslash, indexOfLastForwardSlash);
            if (indexOfLastSeparator != (-1))
            {
                cimNamespace = cmdletizationClassName.Substring(0, indexOfLastSeparator);
                cimClassName = cmdletizationClassName.Substring(indexOfLastSeparator + 1, cmdletizationClassName.Length - indexOfLastSeparator - 1);
            }
            else
            {
                cimNamespace = null;
                cimClassName = cmdletizationClassName;
            }
        }

        private static string GetCimClassName(string cmdletizationClassName)
        {
            string throwAway;
            string cimClassName;
            ExtractCimNamespaceAndClassName(cmdletizationClassName, out throwAway, out cimClassName);
            return cimClassName;
        }

        private static string GetCimNamespace(string cmdletizationClassName)
        {
            string cimNamespace;
            string throwAway;
            ExtractCimNamespaceAndClassName(cmdletizationClassName, out cimNamespace, out throwAway);
            return cimNamespace;
        }

        internal string PrependComputerNameToMessage(string message)
        {
            string computerName = this.Session.ComputerName;
            if (computerName == null)
            {
                return message;
            }

            return string.Format(
                CultureInfo.InvariantCulture,
                CmdletizationResources.CimJob_ComputerNameConcatenationTemplate,
                computerName,
                message);
        }

        public InvocationInfo CmdletInvocationInfo
        {
            get { return this.CmdletInvocationContext.CmdletInvocationInfo; }
        }

        public string CmdletizationClassName
        {
            get { return this.CmdletInvocationContext.CmdletDefinitionContext.CmdletizationClassName; }
        }

        public Version CmdletizationModuleVersion
        {
            get { return this.CmdletInvocationContext.CmdletDefinitionContext.CmdletizationModuleVersion; }
        }

        public ActionPreference ErrorActionPreference
        {
            get { return this.CmdletInvocationContext.ErrorActionPreference; }
        }

        public ActionPreference WarningActionPreference
        {
            get { return this.CmdletInvocationContext.WarningActionPreference; }
        }

        public ActionPreference VerboseActionPreference
        {
            get { return this.CmdletInvocationContext.VerboseActionPreference; }
        }

        public ActionPreference DebugActionPreference
        {
            get { return this.CmdletInvocationContext.DebugActionPreference; }
        }

        public bool IsRunningInBackground
        {
            get { return this.CmdletInvocationContext.IsRunningInBackground; }
        }

        public MshCommandRuntime.ShouldProcessPossibleOptimization ShouldProcessOptimization
        {
            get { return this.CmdletInvocationContext.ShouldProcessOptimization; }
        }

        public bool ShowComputerName
        {
            get { return this.CmdletInvocationContext.ShowComputerName; }
        }

        public bool SupportsShouldProcess
        {
            get { return this.CmdletInvocationContext.CmdletDefinitionContext.SupportsShouldProcess; }
        }
    }
}