internal/Get-OfflineSqlFileStructure.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
Function Get-OfflineSqlFileStructure
{
<#
.SYNOPSIS
Internal function. Returns dictionary object that contains file structures for SQL databases.
 
#>

[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[object]$SqlServer,
[Parameter(Mandatory = $true, Position = 1)]
[string]$dbname,
[Parameter(Mandatory = $true, Position = 2)]
[object]$filelist,
[Parameter(Mandatory = $false, Position = 3)]
[bool]$ReuseSourceFolderStructure,
[System.Management.Automation.PSCredential]$SqlCredential
)

$server = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential

$destinationfiles = @{ };
$logfiles = $filelist | Where-Object { $_.Type -eq "L" }
$datafiles = $filelist | Where-Object { $_.Type -ne "L" }
$filestream = $filelist | Where-Object { $_.Type -eq "S" }

if ($filestream)
{
$sql = "select coalesce(SERVERPROPERTY('FilestreamConfiguredLevel'),0) as fs"
$fscheck = $server.databases['master'].ExecuteWithResults($sql)
if ($fscheck.tables.fs -eq 0) { return $false }
}

# Data Files
foreach ($file in $datafiles)
{
# Destination File Structure
$d = @{ }
if ($ReuseSourceFolderStructure -eq $true)
{
$d.physical = $file.PhysicalName
}
else
{
$directory = Get-SqlDefaultPaths $server data
$filename = Split-Path $($file.PhysicalName) -leaf
$d.physical = "$directory\$filename"
}

$d.logical = $file.LogicalName
$destinationfiles.add($file.LogicalName, $d)
}

# Log Files
foreach ($file in $logfiles)
{
$d = @{ }
if ($ReuseSourceFolderStructure)
{
$d.physical = $file.PhysicalName
}
else
{
$directory = Get-SqlDefaultPaths $server log
$filename = Split-Path $($file.PhysicalName) -leaf
$d.physical = "$directory\$filename"
}

$d.logical = $file.LogicalName
$destinationfiles.add($file.LogicalName, $d)
}

return $destinationfiles
}