Repository navigation
Expand file tree
/
Copy pathremove-item.action.ps1
More file actions
63 lines (54 loc) · 2.07 KB
/
Copy pathremove-item.action.ps1
File metadata and controls
63 lines (54 loc) · 2.07 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
$action = {
param (
$Parameters
)
$rootPath = $Parameters.RootPath
$actualParameters = $Parameters.Parameters
if (-not $actualParameters.Path) {
throw "Invalid parameters! Specify a Path to delete."
}
$paths = $actualParameters.Path -replace '%ProjectRoot%', $rootPath
$deleteParam = @{ }
if ($actualParameters.Recurse) { $deleteParam.Recurse = $true }
if ($actualParameters.Force) { $deleteParam.Force = $true }
$inSession = $null
if ($actualParameters.InSession) {
if ($actualParameters.InSession -is [System.Management.Automation.Runspaces.PSSession]) {
$inSession = $actualParameters.InSession
}
$artifactObject = Get-PSMDBuildArtifact -Name $actualParameters.InSession
if (-not $artifactObject) { throw "Artifact for parameter InSession not found: $($actualParameters.InSession)" }
if ($artifactObject.Value -isnot [System.Management.Automation.Runspaces.PSSession]) { throw "Artifact for parameter InSession ($($actualParameters.InSession)) is not a pssession!" }
$inSession = $artifactObject.Value
}
if ($inSession) {
$failed = Invoke-Command -Session $inSession -ScriptBlock {
param ($DeleteParam, $Paths)
foreach ($path in $Paths) {
if (-not (Get-Item -Path $path -Force -ErrorAction Ignore)) { continue }
try { Remove-Item @DeleteParam -Path $path -ErrorAction Stop }
catch { return $_ }
}
} -ArgumentList $deleteParam, $paths
if ($failed) {
throw $failed
}
}
foreach ($path in $paths) {
if (-not (Get-Item -Path $path -Force -ErrorAction Ignore)) { continue }
try { Remove-Item @DeleteParam -Path $path -ErrorAction Stop }
catch { throw }
}
}
$params = @{
Name = 'remove-item'
Action = $action
Description = 'Removes files or folders'
Parameters = @{
Path = '(mandatory) Path(s) to the item(s) to delete. Use "%ProjectRoot%" to reference to the root path containing the build file.'
InSession = 'Artifact Name of the PSSession within which to execute the deletion'
Recurse = 'Whether to delete child items'
Force = 'Whether to use force'
}
}
Register-PSMDBuildAction @params