public/Unpublish-Cube.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
function Unpublish-Cube {
<#
    .SYNOPSIS
    Unpublish-Cube drops a tabular or multidimenstional cube from a SQL Server Analysis Services instance.
 
    .DESCRIPTION
    Unpublish-Cube drops a tabular or multidimenstional cube from a SQL Server Analysis Services instance.
 
    .PARAMETER Server
    Name of the SSAS server, including instance and port if required.
 
    .PARAMETER CubeDatabase
    The name of the cube database to be dropped.
 
    .PARAMETER Credential
    [Optional] A PSCredential object containing the credentials to connect to the AAS server.
 
    .EXAMPLE
    Unpublish-Cube -Server $ServerName -CubeDatabase $CubeName;
 
    .LINK
    https://github.com/DrJohnT/DeployCube
 
    .NOTES
    Written by (c) Dr. John Tunnicliffe, 2019-2021 https://github.com/DrJohnT/DeployCube
    This PowerShell script is released under the MIT license http://www.opensource.org/licenses/MIT
#>

    [CmdletBinding()]
    param
    (
        [String] [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $Server,

        [String] [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $CubeDatabase,

        [PSCredential] [Parameter(Mandatory = $false)]
        $Credential = $null
    )

    $asCmd = "<Delete xmlns='http://schemas.microsoft.com/analysisservices/2003/engine'><Object><DatabaseID>$CubeDatabase</DatabaseID></Object></Delete>";
    if ($null -eq $Credential) {
        $returnResult = Invoke-ASCmd -Server $Server -Query $asCmd;
    } else {
        $returnResult = Invoke-ASCmd -Server $Server -Credential $Credential -Query $asCmd;
    }
    
    if (-not ($returnResult -like '*urn:schemas-microsoft-com:xml-analysis:empty*')) {
        throw "Failed to drop cube $CubeDatabase";
    }
}

New-Alias -Name Drop-Cube -Value Unpublish-Cube;