functions/Start-DbaPowerBi.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
58
59
60
61
62
63
64
65
66
67
68
function Start-DbaPowerBi {
    <#
        .SYNOPSIS
            Launches the PowerBi dashboard for dbatools
 
        .DESCRIPTION
            Launches the PowerBi dashboard for dbatools
 
        .PARAMETER Path
            The location of the pbix file. "$script:ModuleRoot\bin\pbix\dbatools.pbix" by default.
 
        .PARAMETER InputObject
            Enables piping.
 
        .PARAMETER EnableException
            By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
            This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
            Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
        .EXAMPLE
            Start-DbaPowerBi
 
            Launches PowerBi from "$script:ModuleRoot\bin\pbix\dbatools.pbix" using "C:\windows\Temp\dbatools\" (generated by Update-DbaPowerBiDataSource) as the datasource.
 
        .EXAMPLE
            Start-DbaPowerBi -Path \\nas\projects\dbatools.pbix
 
            Launches \\nas\projects\dbatools.pbix
 
    #>

    [CmdletBinding()]
    param (
        [string]$Path = "$script:ModuleRoot\bin\pbix\dbatools.pbix",
        [parameter(ValueFromPipeline)]
        [pscustomobject]$InputObject,
        [switch]$EnableException
    )

    process {
        if (-not (Test-Path -Path $Path)) {
            Stop-PSFFunction -Message "$Path does not exist"
            return
        }

        $association = Get-ItemProperty "Registry::HKEY_Classes_root\.pbix" -ErrorAction SilentlyContinue

        if (-not $association) {
            Stop-PSFFunction -Message ".pbix not associated with any program. Please (re)install Power BI"
            return
        }

        if ($Path -match "Program Files") {
            $newpath = "$script:localapp\dbatools.pbix"
            #if ((Test-Path -Path $newpath)) { # Would be nice if we could tell if it needed to be replaced or not
            #I suppose we could use dbatools versioning and wintemp?
            Copy-Item -Path $Path -Destination $newpath -Force -ErrorAction SilentlyContinue
            $Path = $newpath
        }

        try {
            Invoke-Item -Path $path
        }
        catch {
            Stop-PSFFunction -Message "Failure" -Exception $_
            return
        }
    }
}