functions/Invoke-DbatoolsRenameHelper.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function Invoke-DbatoolsRenameHelper {
    <#
    .SYNOPSIS
        Older dbatools command names have been changed. This script helps keep up.
 
    .DESCRIPTION
        Older dbatools command names have been changed. This script helps keep up.
 
    .PARAMETER InputObject
        A piped in object from Get-ChildItem
 
    .PARAMETER Encoding
        Specifies the file encoding. The default is UTF8.
 
        Valid values are:
        -- ASCII: Uses the encoding for the ASCII (7-bit) character set.
        -- BigEndianUnicode: Encodes in UTF-16 format using the big-endian byte order.
        -- Byte: Encodes a set of characters into a sequence of bytes.
        -- String: Uses the encoding type for a string.
        -- Unicode: Encodes in UTF-16 format using the little-endian byte order.
        -- UTF7: Encodes in UTF-7 format.
        -- UTF8: Encodes in UTF-8 format.
        -- Unknown: The encoding type is unknown or invalid. The data can be treated as binary.
 
    .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.
 
    .PARAMETER WhatIf
        Shows what would happen if the command were to run. No actions are actually performed
 
    .PARAMETER Confirm
        Prompts you for confirmation before executing any changing operations within the command
 
    .NOTES
        Tags: Module
        Author: Chrissy LeMaire (@cl), netnerds.net
 
        Website: https://dbatools.io
        Copyright: (c) 2018 by dbatools, licensed under MIT
        License: MIT https://opensource.org/licenses/MIT
 
    .LINK
        https://dbatools.io/Invoke-DbatoolsRenameHelper
 
    .EXAMPLE
        PS C:\> Get-ChildItem C:\temp\ps\*.ps1 -Recurse | Invoke-DbatoolsRenameHelper
 
        Checks to see if any ps1 file in C:\temp\ps matches an old command name.
        If so, then the command name within the text is updated and the resulting changes are written to disk in UTF-8.
 
    .EXAMPLE
        PS C:\> Get-ChildItem C:\temp\ps\*.ps1 -Recurse | Invoke-DbatoolsRenameHelper -Encoding Ascii -WhatIf
 
        Shows what would happen if the command would run. If the command would run and there were matches,
        the resulting changes would be written to disk as Ascii encoded.
 
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        [parameter(Mandatory, ValueFromPipeline)]
        [System.IO.FileInfo[]]$InputObject,
        [ValidateSet('ASCII', 'BigEndianUnicode', 'Byte', 'String', 'Unicode', 'UTF7', 'UTF8', 'Unknown')]
        [string]$Encoding = 'UTF8',
        [switch]$EnableException
    )
    begin {
        $morerenames = @(
            @{
                "AliasName"  = "Invoke-Sqlcmd2"
                "Definition" = "Invoke-DbaQuery"
            },
            @{
                "AliasName"  = "UseLastBackups"
                "Definition" = "UseLastBackup"
            },
            @{
                "AliasName"  = "NetworkShare"
                "Definition" = "SharedPath"
            },
            @{
                "AliasName"  = "NoSystemLogins"
                "Definition" = "ExcludeSystemLogins"
            },
            @{
                "AliasName"  = "NoJobSteps"
                "Definition" = "ExcludeJobSteps"
            },
            @{
                "AliasName"  = "NoSystemObjects"
                "Definition" = "ExcludeSystemObjects"
            },
            @{
                "AliasName"  = "NoJobs"
                "Definition" = "ExcludeJobs"
            },
            @{
                "AliasName"  = "NoDatabases"
                "Definition" = "ExcludeDatabases"
            },
            @{
                "AliasName"  = "NoDisabledJobs"
                "Definition" = "ExcludeDisabledJobs"
            },
            @{
                "AliasName"  = "NoJobSteps"
                "Definition" = "ExcludeJobSteps"
            },
            @{
                "AliasName"  = "NoSystem"
                "Definition" = "ExcludeSystemLogins"
            },
            @{
                "AliasName"  = "NoSystemDb"
                "Definition" = "ExcludeSystem"
            },
            @{
                "AliasName"  = "NoSystemObjects"
                "Definition" = "ExcludeSystemObjects"
            },
            @{
                "AliasName"  = "NoSystemSpid"
                "Definition" = "ExcludeSystemSpids"
            },
            @{
                "AliasName"  = "NoQueryTextColumn"
                "Definition" = "ExcludeQueryTextColumn"
            },
            @{
                "AliasName"  = "ExcludeAllSystemDb"
                "Definition" = "ExcludeSystem"
            },
            @{
                "AliasName"  = "ExcludeAllUserDb"
                "Definition" = "ExcludeUser"
            }
        )

        $allrenames = $script:renames + $morerenames
    }
    process {
        foreach ($fileobject in $InputObject) {
            $file = $fileobject.FullName

            foreach ($name in $allrenames) {
                if ((Select-String -Pattern $name.AliasName -Path $file)) {
                    if ($Pscmdlet.ShouldProcess($file, "Replacing $($name.AliasName) with $($name.Definition)")) {
                        $content = (Get-Content -Path $file -Raw).Replace($name.AliasName, $name.Definition).Trim()
                        Set-Content -Path $file -Encoding $Encoding -Value $content
                        [pscustomobject]@{
                            Path         = $file
                            Pattern      = $name.AliasName
                            ReplacedWith = $name.Definition
                        }
                    }
                }
            }
        }
    }
}