internal/Rename-LogicalFile.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
97
98
99
100
Function Rename-LogicalFile
{
<#
 .SYNOPSIS
 Internal function. Renames logical files.
    Can either use prefix with -Prefix, ALL files log and data will be prefixed
 
    Or pass in a mapping hash like:
    $mapping = @{
        'File1'='NewNamefile1'
        'File3'='somethingelse'
    }
    Can select which files need remappping.
#>

[CmdletBinding()]
param (
        [parameter(Mandatory = $true)]
[Alias("ServerInstance", "SqlInstance")]
[object]$SqlServer,
[string]$DbName,
[System.Management.Automation.PSCredential]$SqlCredential,
        [string]$Prefix,
        [hashtable]$Mapping
)
    $FunctionName = "Rename-LogicalFile"
    if ('' -ne $Prefix -and $Mapping.count -ne 0)
    {
        Write-Error "$FunctionName only accepts a new prefix OR a mapping hash"
        return $false
    }

    $server = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
    if ($null -eq $server.Databases[$DbName])
    {
        Write-Error "$FunctionName - Database $DbName does not exist"
        return $false
        break
    }
    $database = $server.Databases[$DbName]
    Write-Verbose "$FunctionName - Getting data files"
    $LogicalFiles = @()
    foreach ($FileGroup in $Database.filegroups)
    {
        foreach($File in $FileGroup.files)
        {
            $LogicalFiles += $File
        }
    }
    Write-Verbose "$FunctionName - getting log files"
    foreach ($File in $Database.LogFiles)
    {
        $LogicalFiles += $File
    }
    if ($null -ne $Mapping -and $LogicalFiles.count -lt $Mapping.count)
    {
        Write-Error "$FunctionName - More mappings than files"
        return $false
    }
    Write-Verbose "File Count = $($LogicalFiles.count)"
    if ('' -ne $Prefix)
    {
        foreach ($File in $LogicalFiles)
        {
            $NewName = $Prefix+$File.Name
            Write-Verbose "$FunctionName prefixing $($File.Name) to $NewName"
            try {
                $File.Rename($NewName)    
            }
            catch  {
                Write-Exception $_
                return 
            }
            
        }
    }
    if ($null -ne $Mapping)
    {
        foreach ($File in $LogicalFiles)
        {
            if ($null -eq $Mapping[($File.Name)])
            {
                Write-Verbose "$FunctionName - No mapping for $($File.Name) "
            }
            else
            {
                $NewName = $Mapping[($File.Name)]
                Write-Verbose "$FunctionName - mapping $($File.Name) to $NewName"
                try {
                    $File.Rename($NewName)    
                }
                catch  {
                    Write-Exception $_
                    return 
                }
            }
        }
    }
    return $true
}