Repository navigation
Expand file tree
/
Copy pathcommand.action.ps1
More file actions
79 lines (71 loc) · 2.34 KB
/
Copy pathcommand.action.ps1
File metadata and controls
79 lines (71 loc) · 2.34 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
$action = {
param (
$Parameters
)
$rootPath = $Parameters.RootPath
$actualParameters = $Parameters.Parameters
#region Process Parameters
if (-not $actualParameters.Command) {
throw "Mandatory parameter: Command not specified"
}
if ($actualParameters.Command -is [System.Management.Automation.ScriptBlock]) {
$scriptblock = $actualParameters.Command
}
else {
try { $scriptblock = [scriptblock]::Create($actualParameters.Command) }
catch {
throw "Error parsing command '$($actualParameters.Command)' : $_"
}
}
$actualArguments = foreach ($argument in $actualParameters.ArgumentList) {
if ($argument -isnot [string]) {
$argument
continue
}
if ($argument -notlike '%!*!%') {
$argument
continue
}
$artifactName = $argument -replace '^%!(.+)!%$', '$1'
$artifactObject = Get-PSMDBuildArtifact -Name $artifactName
if (-not $artifactObject) { throw "Artifact for arguments not found: $artifactName" }
$artifactObject.Value
}
$inSession = $null
if ($actualParameters.InSession) {
$inSession = foreach ($sessionInput in $actualParameters.InSession) {
if ($sessionInput -is [System.Management.Automation.Runspaces.PSSession]) {
$sessionInput
continue
}
$artifactObject = Get-PSMDBuildArtifact -Name $sessionInput
if ($artifactObject.Value -is [System.Management.Automation.Runspaces.PSSession]) {
$artifactObject.Value
continue
}
if (-not $artifactObject) { throw "Artifact for parameter InSession not found: $($sessionInput)" }
throw "Artifact for parameter InSession ($($sessionInput)) is not a pssession!"
}
}
#endregion Process Parameters
#region Execution
$invokeParam = @{
ScriptBlock = $scriptblock
ArgumentList = $actualArguments
}
if ($inSession) { $invokeParam.Session = $inSession }
try { Invoke-Command @invokeParam -ErrorAction Stop }
catch { throw }
#endregion Execution
}
$params = @{
Name = 'command'
Action = $action
Description = 'Execute a scriptblock'
Parameters = @{
Command = '(mandatory) Scriptcode to run'
ArgumentList = 'Any number of arguments to pass to the command. To insert artifacts, specify a string with the special notation "%!ArtifactName!%"'
InSession = 'Execute the scriptfile in the target PSSession. Either provide a full session object or an artifact name pointing at one.'
}
}
Register-PSMDBuildAction @params