|
21 | 21 | Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
|
22 | 22 | To connect as a different Windows user, run PowerShell as that user.
|
23 | 23 |
|
| 24 | + .PARAMETER Credential |
| 25 | + Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. |
| 26 | + This works similar as SqlCredential but is only meant for authentication to the the host |
| 27 | +
|
24 | 28 | .PARAMETER PSDCSqlCredential
|
25 | 29 | Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
|
26 | 30 | This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
|
|
53 | 57 |
|
54 | 58 | #>
|
55 | 59 |
|
56 |
| - [CmdLetBinding()] |
| 60 | + [CmdLetBinding(SupportsShouldProcess = $true)] |
57 | 61 |
|
58 | 62 | param(
|
59 | 63 | [Parameter(Mandatory = $true)]
|
60 | 64 | [string[]]$HostName,
|
61 | 65 | [System.Management.Automation.PSCredential]
|
62 | 66 | $SqlCredential,
|
63 | 67 | [System.Management.Automation.PSCredential]
|
| 68 | + $Credential, |
| 69 | + [System.Management.Automation.PSCredential] |
64 | 70 | $PSDCSqlCredential,
|
65 | 71 | [switch]$EnableException
|
66 | 72 | )
|
67 | 73 |
|
68 | 74 | begin {
|
| 75 | + # Get the module configurations |
| 76 | + $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.Server |
| 77 | + $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
| 78 | + if (-not $pdcCredential) { |
| 79 | + $pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null |
| 80 | + } |
| 81 | + else { |
| 82 | + $pdcCredential = $PSDCSqlCredential |
| 83 | + } |
| 84 | + |
69 | 85 | # Test the module database setup
|
70 | 86 | try {
|
71 |
| - Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException |
| 87 | + Test-PSDCConfiguration -SqlCredential $pdcCredential -EnableException |
72 | 88 | }
|
73 | 89 | catch {
|
74 | 90 | Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
|
75 | 91 | }
|
76 | 92 |
|
77 |
| - $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server |
78 |
| - $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
79 |
| - |
80 | 93 | }
|
81 | 94 |
|
82 | 95 | process {
|
|
86 | 99 | # Loop through each of the hosts
|
87 | 100 | foreach ($hst in $HostName) {
|
88 | 101 |
|
| 102 | + # Setup the computer object |
| 103 | + $computer = [PsfComputer]$hst |
| 104 | + |
| 105 | + if (-not $computer.IsLocalhost) { |
| 106 | + $command = [scriptblock]::Create("Import-Module PSDatabaseClone") |
| 107 | + |
| 108 | + try { |
| 109 | + Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 110 | + } |
| 111 | + catch { |
| 112 | + Stop-PSFFunction -Message "Couldn't import module remotely" -Target $command |
| 113 | + return |
| 114 | + } |
| 115 | + } |
| 116 | + |
89 | 117 | $query = "
|
90 | 118 | SELECT i.ImageLocation,
|
91 | 119 | c.CloneLocation,
|
|
105 | 133 | # Get the clones registered for the host
|
106 | 134 | try {
|
107 | 135 | Write-PSFMessage -Message "Get the clones for host $hst" -Level Verbose
|
108 |
| - $results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query |
| 136 | + $results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $pdcCredential -Database $pdcDatabase -Query $query |
109 | 137 | }
|
110 | 138 | catch {
|
111 | 139 | Stop-PSFFunction -Message "Couldn't get the clones for $hst" -Target $pdcSqlInstance -ErrorRecord $_ -Continue
|
|
125 | 153 | try {
|
126 | 154 | Write-PSFMessage -Message "Mounting vhd $($result.CloneLocation)" -Level Verbose
|
127 | 155 |
|
128 |
| - Mount-VHD -Path $result.CloneLocation -NoDriveLetter -ErrorAction SilentlyContinue |
| 156 | + # Check if computer is local |
| 157 | + if ($PSCmdlet.ShouldProcess($result.CloneLocation, "Mounting $($result.CloneLocation)")) { |
| 158 | + if ($computer.IsLocalhost) { |
| 159 | + $null = Mount-VHD -Path $result.CloneLocation -NoDriveLetter -ErrorAction SilentlyContinue |
| 160 | + } |
| 161 | + else { |
| 162 | + $command = [ScriptBlock]::Create("Mount-VHD -Path $($result.CloneLocation) -NoDriveLetter -ErrorAction SilentlyContinue") |
| 163 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 164 | + } |
| 165 | + } |
129 | 166 | }
|
130 | 167 | catch {
|
131 | 168 | Stop-PSFFunction -Message "Couldn't mount vhd" -Target $clone -Continue
|
|
139 | 176 | if ($result.DatabaseName -notin $databases.Name) {
|
140 | 177 |
|
141 | 178 | # Get all the files of the database
|
142 |
| - $databaseFiles = Get-ChildItem -Path $result.AccessPath -Recurse | Where-Object {-not $_.PSIsContainer} |
| 179 | + if ($PSCmdlet.ShouldProcess($result.AccessPath, "Retrieving database files from $($result.AccessPath)")) { |
| 180 | + # Check if computer is local |
| 181 | + if ($computer.IsLocalhost) { |
| 182 | + $databaseFiles = Get-ChildItem -Path $result.AccessPath -Recurse | Where-Object {-not $_.PSIsContainer} |
| 183 | + } |
| 184 | + else { |
| 185 | + $commandText = "Get-ChildItem -Path $($result.AccessPath) -Recurse | " + 'Where-Object {-not $_.PSIsContainer}' |
| 186 | + $command = [ScriptBlock]::Create($commandText) |
| 187 | + $databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 188 | + } |
| 189 | + } |
143 | 190 |
|
144 | 191 | # Setup the database filestructure
|
145 | 192 | $dbFileStructure = New-Object System.Collections.Specialized.StringCollection
|
|
152 | 199 | Write-PSFMessage -Message "Mounting database from clone" -Level Verbose
|
153 | 200 |
|
154 | 201 | # Mount the database using the config file
|
155 |
| - $null = Mount-DbaDatabase -SqlInstance $result.SQLInstance -Database $result.DatabaseName -FileStructure $dbFileStructure |
| 202 | + if ($PSCmdlet.ShouldProcess($result.DatabaseName, "Mounting database $($result.DatabaseName) to $($result.SQLInstance)")) { |
| 203 | + try { |
| 204 | + $null = Mount-DbaDatabase -SqlInstance $result.SQLInstance -Database $result.DatabaseName -FileStructure $dbFileStructure |
| 205 | + } |
| 206 | + catch { |
| 207 | + Stop-PSFFunction -Message "Couldn't mount database $($result.DatabaseName)" -Target $result.DatabaseName -Continue |
| 208 | + } |
| 209 | + } |
156 | 210 | }
|
157 | 211 | else {
|
158 | 212 | Write-PSFMessage -Message "Database $($result.Database) is already attached" -Level Verbose
|
|
0 commit comments