|
153 | 153 | # Setup the computer object |
154 | 154 | $computer = [PsfComputer]$server.Name |
155 | 155 |
|
| 156 | + if (-not $computer.IsLocalhost) { |
| 157 | + $command = [scriptblock]::Create("Import-Module PSDatabaseClone") |
| 158 | + |
| 159 | + try { |
| 160 | + Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 161 | + } |
| 162 | + catch { |
| 163 | + Stop-PSFFunction -Message "Couldn't import module remotely" -Target $command |
| 164 | + return |
| 165 | + } |
| 166 | + } |
| 167 | + |
156 | 168 | # Check destination |
157 | 169 | if (-not $Destination) { |
158 | 170 | $Destination = "$($server.DefaultFile)\clone" |
|
168 | 180 | $Destination = Convert-PSDCLocalUncPathToLocalPath -UncPath $Destination -Credential $Credential |
169 | 181 | } |
170 | 182 | else { |
171 | | - $command = [ScriptBlock]::Create("Convert-PSDCLocalUncPathToLocalPath -UncPath '$Destination'") |
| 183 | + $command = [ScriptBlock]::Create("Convert-PSDCLocalUncPathToLocalPath -UncPath `"$Destination`"") |
172 | 184 | $Destination = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
173 | 185 | } |
174 | 186 | } |
|
184 | 196 | } |
185 | 197 |
|
186 | 198 | # Test if the destination can be reached |
187 | | - if (-not (Test-Path -Path $Destination -Credential $Credential)) { |
188 | | - Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance |
| 199 | + # Check if computer is local |
| 200 | + if ($computer.IsLocalhost) { |
| 201 | + if (-not (Test-Path -Path $Destination -Credential $Credential)) { |
| 202 | + Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance |
| 203 | + } |
| 204 | + } |
| 205 | + else { |
| 206 | + $command = [ScriptBlock]::Create("Test-Path -Path $Destination") |
| 207 | + $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 208 | + if (-not $result) { |
| 209 | + Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance |
| 210 | + } |
189 | 211 | } |
| 212 | + |
190 | 213 | } |
191 | 214 |
|
192 | 215 | # Loopt through all the databases |
|
251 | 274 | $accessPath = "$Destination\$mountDirectory" |
252 | 275 |
|
253 | 276 | # Check if access path is already present |
254 | | - if (-not (Test-Path -Path $accessPath -Credential $Credential)) { |
255 | | - try { |
256 | | - # Check if computer is local |
257 | | - if ($computer.IsLocalhost) { |
| 277 | + if ($computer.IsLocalhost) { |
| 278 | + if (-not (Test-Path -Path $accessPath -Credential $Credential)) { |
| 279 | + try { |
258 | 280 | $null = New-Item -Path $accessPath -ItemType Directory -Credential $Credential -Force |
259 | 281 | } |
260 | | - else { |
261 | | - $command = [ScriptBlock]::Create("New-Item -Path $accessPath -ItemType Directory -Credential $Credential -Force") |
262 | | - $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 282 | + catch { |
| 283 | + Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue |
263 | 284 | } |
264 | | - |
265 | 285 | } |
266 | | - catch { |
267 | | - Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue |
| 286 | + } |
| 287 | + else { |
| 288 | + $command = [ScriptBlock]::Create("Test-Path -Path $accessPath") |
| 289 | + $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 290 | + if (-not $result) { |
| 291 | + try { |
| 292 | + $command = [ScriptBlock]::Create("New-Item -Path $accessPath -ItemType Directory -Force") |
| 293 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 294 | + } |
| 295 | + catch { |
| 296 | + Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue |
| 297 | + } |
268 | 298 | } |
269 | 299 | } |
270 | 300 |
|
271 | 301 | # Check if the clone vhd does not yet exist |
272 | | - if (Test-Path -Path "$Destination\$CloneName.vhdx" -Credential $DestinationCredential) { |
273 | | - Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue |
| 302 | + if ($computer.IsLocalhost) { |
| 303 | + if (Test-Path -Path "$Destination\$CloneName.vhdx" -Credential $DestinationCredential) { |
| 304 | + Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue |
| 305 | + } |
| 306 | + } |
| 307 | + else { |
| 308 | + $command = [ScriptBlock]::Create("Test-Path -Path `"$Destination\$CloneName.vhdx`"") |
| 309 | + $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 310 | + if (-not $result) { |
| 311 | + Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue |
| 312 | + } |
274 | 313 | } |
275 | 314 |
|
276 | 315 | # Create the new child vhd |
|
282 | 321 | $vhd = New-VHD -ParentPath $ParentVhd -Path "$Destination\$CloneName.vhdx" -Differencing |
283 | 322 | } |
284 | 323 | else { |
285 | | - $command = [ScriptBlock]::Create("New-VHD -ParentPath $ParentVhd -Path '$Destination\$CloneName.vhdx' -Differencing") |
| 324 | + $command = [ScriptBlock]::Create("New-VHD -ParentPath $ParentVhd -Path `"$Destination\$CloneName.vhdx`" -Differencing") |
286 | 325 | $vhd = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
287 | 326 | } |
288 | 327 |
|
|
309 | 348 | $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
310 | 349 |
|
311 | 350 | # Get the disk based on the name of the vhd |
312 | | - $command = [ScriptBlock]::Create("Get-Disk | Where-Object {$_.Location -eq `"$Destination\$CloneName.vhdx`"}") |
| 351 | + $commandText = "Get-Disk | Where-Object {$_.Location -eq `"$Destination\$CloneName.vhdx`"}" |
| 352 | + $command = [ScriptBlock]::Create($commandText) |
313 | 353 | $disk = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
314 | 354 | } |
315 | 355 | } |
|
379 | 419 | $null = Mount-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $cloneDatabase -FileStructure $dbFileStructure |
380 | 420 | } |
381 | 421 | else { |
382 | | - $command = [ScriptBlock]::Create("Mount-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $cloneDatabase -FileStructure $dbFileStructure") |
383 | | - $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 422 | + $command = [ScriptBlock]::Create("Mount-DbaDatabase -SqlInstance $SqlInstance -Database $cloneDatabase -FileStructure $dbFileStructure") |
| 423 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $SqlCredential |
384 | 424 | } |
385 | 425 | } |
386 | 426 | catch { |
|
0 commit comments