Modules/SharePointDsc.ContentDatabase/SPContentDatabase.psm1

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
function Mount-SPDscContentDatabase()
{
    param
    (
        [Parameter(Mandatory=$true)]
        [System.Collections.Hashtable]
        $params,

        [Parameter(Mandatory=$true)]
        [System.Boolean]
        $enabled
    )

    if ($params.ContainsKey("Enabled"))
    {
        $params.Remove("Enabled")
    }
    
    if ($params.ContainsKey("Ensure"))
    {
        $params.Remove("Ensure")
    }
    
    if ($params.ContainsKey("InstallAccount"))
    {
        $params.Remove("InstallAccount")
    }
    
    if ($params.ContainsKey("MaximumSiteCount"))
    {
        $params.MaxSiteCount = $params.MaximumSiteCount
        $params.Remove("MaximumSiteCount")
    }
    if ($params.ContainsKey("WebAppUrl"))
    {
        $params.WebApplication = $params.WebAppUrl
        $params.Remove("WebAppUrl")
    }

    try
    {
        $cdb = Mount-SPContentDatabase @params
    }
    catch
    {
        throw ("Error occurred while mounting content database. " + `
                "Content database is not mounted. " + `
                "Error details: $($_.Exception.Message)")
    }

    if ($cdb.Status -eq "Online")
    {
        $cdbenabled = $true
    }
    else
    {
        $cdbenabled = $false
    }

    if ($enabled -ne $cdbenabled)
    {
        switch ($params.Enabled)
        {
            $true
            { 
                $cdb.Status = [Microsoft.SharePoint.Administration.SPObjectStatus]::Online
            }
            $false
            {
                $cdb.Status = [Microsoft.SharePoint.Administration.SPObjectStatus]::Disabled
            }
        }
    }

    return $cdb
}