forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunspaceAttribute.cs
More file actions
97 lines (82 loc) · 3.3 KB
/
Copy pathRunspaceAttribute.cs
File metadata and controls
97 lines (82 loc) · 3.3 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma warning disable 1634, 1691
#pragma warning disable 56506
using Microsoft.PowerShell.Commands;
namespace System.Management.Automation.Runspaces
{
/// <summary>
/// Defines the attribute used to designate a cmdlet parameter as one that
/// should accept runspaces.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public sealed class RunspaceAttribute : ArgumentTransformationAttribute
{
/// <summary>
/// Transforms the input data to a Runspace.
/// </summary>
/// <param name="engineIntrinsics">
/// The engine APIs for the context under which the transformation is being
/// made.
/// </param>
/// <param name="inputData">
/// If a string, the transformation uses the input as the runspace name.
/// If an int, the transformation uses the input as the runspace ID.
/// If a guid, the transformation uses the input as the runspace GUID.
/// If already a Runspace, the transform does nothing.
/// </param>
/// <returns>A runspace object representing the inputData.</returns>
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
if (engineIntrinsics?.Host?.UI == null)
{
throw PSTraceSource.NewArgumentNullException("engineIntrinsics");
}
if (inputData == null)
{
return null;
}
// Try to coerce the input as a runspace
Runspace runspace = LanguagePrimitives.FromObjectAs<Runspace>(inputData);
if (runspace != null)
{
return runspace;
}
// Try to coerce the runspace if the user provided a string, int, or guid
switch (inputData)
{
case string name:
var runspacesByName = GetRunspaceUtils.GetRunspacesByName(new[] { name });
if (runspacesByName.Count == 1)
{
return runspacesByName[0];
}
break;
case int id:
var runspacesById = GetRunspaceUtils.GetRunspacesById(new[] { id });
if (runspacesById.Count == 1)
{
return runspacesById[0];
}
break;
case Guid guid:
var runspacesByGuid = GetRunspaceUtils.GetRunspacesByInstanceId(new[] { guid });
if (runspacesByGuid.Count == 1)
{
return runspacesByGuid[0];
}
break;
default:
// Non-convertible type
break;
}
// If we couldn't get a single runspace, return the inputData
return inputData;
}
/// <summary>
/// Gets a flag indicating whether or not null optional parameters are transformed.
/// </summary>
public override bool TransformNullOptionalParameters { get { return false; } }
}
}
#pragma warning restore 56506