functions/Update-DbaPowerBiDataSource.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function Update-DbaPowerBiDataSource {
<#
    .SYNOPSIS
        Converts the results of dbatools commands for our PowerBI Dashboard related commands. This command is specific to our toolset and not a general Power BI command.
         
    .DESCRIPTION
        Converts the results of dbatools commands for our PowerBI Dashboard related commands. This command is specific to our toolset and not a general Power BI command.
         
    .PARAMETER InputObject
        Enables piping
         
    .PARAMETER Path
        The directory to store your files. "C:\windows\temp\dbatools\" by default
         
    .PARAMETER Enviornment
        Tag your data with an enviornment. Defaults to "Default"
         
    .PARAMETER Append
        Don't delete previous default data sources.
         
    .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
        Get-DbaPfDataCollectorSet -ComputerName sql2016 | Invoke-DbaPfRelog -AllowClobber | Update-DbaPowerBiDataSource | Start-DbaPowerBi
         
        Converts the results of the performance monitor data source and stores it in the appropriate directory then launches our Power BI dashboard
         
         
#>

    [CmdletBinding()]
    param (
        [parameter(ValueFromPipeline, Mandatory)]
        [pscustomobject]$InputObject,
        [string]$Path = "$env:windir\temp\dbatools",
        [string]$Enviornment = "Default",
        [switch]$Append,
        [switch]$EnableException
    )
    begin {
        if ($Environment -ne "Default" -and -not $Append) {
            $null = Remove-Item "$Path\*Default*.*sv" -ErrorAction SilentlyContinue
        }
        $orginalpath = $Path
    }
    process {
        ++$i

        if ($InputObject.RelogFile) {
            $Path = "$orginalpath\perfmon"
        }
        else {
            $Path = "$orginalpath\xevents"
        }

        try {
            if (-not (Test-Path -Path $Path)) {
                $null = New-Item -ItemType Directory -Path $Path -ErrorAction Stop
            }
        }
        catch {
            Stop-Function -Message "Failure" -Exception $_
            return
        }

        $extension = $InputObject.Extension.TrimStart(".")
        $basename = "dbatools_$i"
        if ($InputObject.TagFilter) {
            $basename = "$basename`_$($InputObject.TagFilter -join "_")"
        }

        if ($Enviornment) {
            $basename = "$basename`_$Enviornment"
        }

        $filename = "$basename.$extension"

        try {
            Write-Message -Level Verbose -Message "Writing $filename to $path"
            $inputObject | Copy-Item -Destination "$path\$filename"
            Get-ChildItem "$path\$filename"
        }
        catch {
            Stop-Function -Message "Failure" -ErrorRecord $_
            return
        }
    }
    end {
        if ($InputObject -isnot [System.IO.FileInfo] -and $InputObject -isnot [System.IO.DirectoryInfo]) {
            Stop-Function -Message "Invalid input"
            return
        }
    }
}