Public/PowerShell.ps1

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
function PowerShell {
    [cmdletbinding(DefaultParameterSetName = 'ScriptBlock')]
    param(
        [parameter(Mandatory, Position = 0, ParameterSetName = 'ScriptBlock')]
        [scriptblock]$ScriptBlock,

        #[ValidateScript({Test-Path $_})]
        [parameter(Mandatory, Position = 0, ParameterSetName = 'script')]
        [string]$Path,

        [parameter(Position = 1)]
        [ValidateSet('Always', 'OnSuccess', 'OnFailure')]
        [string]$When = $script:Watchmen.Options.NotifierConditions.WatchmenTest
    )

    begin {
        Write-Debug -Message "Entering: $($PSCmdlet.MyInvocation.MyCommand.Name)"
        Assert-InWatchmen -Command $PSCmdlet.MyInvocation.MyCommand.Name
    }

    process {
        $p = [pscustomobject]@{
            PSTypeName = 'Watchmen.Notifier.PowerShell'
            Type = 'PowerShell'
            ScriptBlock = $null
            ScriptPath = $null
            Enabled = $true
            NotifierCondition = $When
        }

        if ($PSCmdlet.ParameterSetName -eq 'ScriptBlock') {
            $p.ScriptBlock = $ScriptBlock
        } else {
            $resolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)

            # Is PowerShell script relative or absolute?
            if ([System.IO.Path]::IsPathRooted($path)) {
                $file = Get-Item -Path $resolvedPath
            } else {
                $file = Get-Item -Path (Join-Path -Path $script:Watchmen.CurrentWatchmenFileRoot -ChildPath $Path)
            }

            if (-not $file.PSIsContainer) {
                $p.ScriptPath = $file.FullName
            } else {
                Write-Error -Message "PowerShell script path must be a file, not a folder. [$($file.FullName)]"
            }
        }

        return $p
    }

    end {
        Write-Debug -Message "Exiting: $($PSCmdlet.MyInvocation.MyCommand.Name)"
    }
}