Repository navigation
Expand file tree
/
Copy pathParameterScript.cs
More file actions
90 lines (82 loc) · 3.41 KB
/
Copy pathParameterScript.cs
File metadata and controls
90 lines (82 loc) · 3.41 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using PSFramework.Utility;
namespace PSModuleDevelopment.Template.Parameter
{
/// <summary>
/// Parameter type executing
/// </summary>
[Serializable]
public class ParameterScript : ParameterBase
{
/// <summary>
/// The scriptblock to execute.
/// Wrapped as string for serialization purposes.
/// </summary>
public string ScriptBlock
{
get
{
if (_ScriptBlock == null)
return "";
return _ScriptBlock.ToString();
}
set
{
_ScriptBlock = new PsfScriptBlock(System.Management.Automation.ScriptBlock.Create(value));
}
}
private PsfScriptBlock _ScriptBlock;
/// <summary>
/// The value of the scriptblock.
/// Populated by the GetValue() method usually called with the "StartUp" timing.
/// </summary>
public string Value;
/// <summary>
/// When exactly during the template process should this scriptblock be executed?
/// </summary>
public ScriptExecutionTime Timing = ScriptExecutionTime.StartUp;
/// <summary>
/// Setting this to true will cause the Invoke-PSMDTemplate command to omit inserting values for the
/// </summary>
public bool SkipInsert;
/// <summary>
/// Returns the string value of the scriptblock by executing it!
/// </summary>
/// <returns>The string value of the scriptblock by executing it!</returns>
public override string GetValue()
{
if (String.IsNullOrEmpty(Value))
try { Value = (string)LanguagePrimitives.ConvertTo(_ScriptBlock.InvokeEx(true, true, false), typeof(string)); }
catch (Exception e) { Value = $"<Error: Scriptblock {Name} failed: {e.Message}>"; }
return Value;
}
/// <summary>
/// Execute the scriptblock "Just-in-time" during either PreItemCreation or PostItemCreation Timing.
/// </summary>
/// <param name="Info">The file/directory info object of the object recently or about to be created</param>
/// <returns>Returns a string value resulting from the scriptblock to insert</returns>
public string GetInTimeValue(FileSystemInfo Info)
{
try { return (string)LanguagePrimitives.ConvertTo(_ScriptBlock.InvokeEx(Info, true, true, false), typeof(string)); }
catch (Exception e) { return $"<Error: Scriptblock {Name} failed: {e.Message}>"; }
}
/// <summary>
/// Execute the scriptblock "Just-in-time" during either PreItemCreation or PostItemCreation Timing.
/// </summary>
/// <param name="Path">The path to the file/directory info object of the object recently or about to be created</param>
/// <param name="IsFile">Whether the object (about to be) created is a file.</param>
/// <returns>Returns a string value resulting from the scriptblock to insert</returns>
public string GetInTimeValue(string Path, bool IsFile)
{
if (IsFile)
return GetInTimeValue(new FileInfo(Path));
return GetInTimeValue(new DirectoryInfo(Path));
}
}
}