2020-03-24 19:08:37 +01:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2018-02-13 18:23:53 +01:00
|
|
|
// Licensed under the MIT License.
|
2016-03-30 23:20:52 +02:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Management.Automation;
|
2019-04-30 07:25:11 +02:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
using Microsoft.Management.Infrastructure;
|
|
|
|
|
|
|
|
namespace Microsoft.PowerShell.Cmdletization.Cim
|
|
|
|
{
|
|
|
|
internal class CimJobContext
|
|
|
|
{
|
|
|
|
internal CimJobContext(
|
|
|
|
CimCmdletInvocationContext cmdletInvocationContext,
|
2016-07-29 22:02:49 +02:00
|
|
|
CimSession session,
|
2016-03-30 23:20:52 +02:00
|
|
|
object targetObject)
|
|
|
|
{
|
|
|
|
this.CmdletInvocationContext = cmdletInvocationContext;
|
|
|
|
|
|
|
|
this.Session = session;
|
|
|
|
this.TargetObject = targetObject ?? this.ClassName;
|
|
|
|
}
|
|
|
|
|
2020-09-02 05:11:16 +02:00
|
|
|
public CimCmdletInvocationContext CmdletInvocationContext { get; }
|
2016-03-30 23:20:52 +02:00
|
|
|
|
2020-09-02 05:11:16 +02:00
|
|
|
public CimSession Session { get; }
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2020-09-02 05:11:16 +02:00
|
|
|
public object TargetObject { get; }
|
2016-03-30 23:20:52 +02:00
|
|
|
|
|
|
|
public string ClassName
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return GetCimClassName(this.CmdletInvocationContext.CmdletDefinitionContext.CmdletizationClassName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ClassNameOrNullIfResourceUriIsUsed
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (this.CmdletInvocationContext.CmdletDefinitionContext.ResourceUri != null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
return this.ClassName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Namespace
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(this.CmdletInvocationContext.NamespaceOverride))
|
|
|
|
{
|
|
|
|
return this.CmdletInvocationContext.NamespaceOverride;
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
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;
|
2020-07-31 01:06:38 +02:00
|
|
|
if (computerName == null)
|
2016-03-30 23:20:52 +02:00
|
|
|
{
|
|
|
|
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; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|