|
118 | 118 |
|
119 | 119 | Write-PSFMessage -Message "Started image creation" -Level Verbose |
120 | 120 |
|
121 | | - # Make up the data from the network path |
122 | | - if ($Destination.StartsWith("\\")) { |
123 | | - try { |
124 | | - [uri]$uri = New-Object System.Uri($Destination) |
125 | | - $uriHost = $uri.Host |
126 | | - } |
127 | | - catch { |
128 | | - Stop-PSFFunction -Message "The destination path $Destination is not valid" -ErrorRecord $_ -Target $Destination |
129 | | - return |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - # Setup the computer object |
134 | | - $computer = [PsfComputer]$uriHost |
135 | | - |
136 | | - if (-not $computer.IsLocalhost) { |
137 | | - $command = "Convert-PSDCLocalUncPathToLocalPath -UncPath '$ImageNetworkPath'" |
138 | | - $commandGetLocalPath = [ScriptBlock]::Create($command) |
139 | | - } |
140 | | - |
141 | | - if ($Destination.StartsWith("\\")) { |
142 | | - Write-PSFMessage -Message "The destination cannot be an UNC path. Converting to local path" -Level Verbose |
143 | | - try { |
144 | | - if ($computer.IsLocalhost) { |
145 | | - $Destination = Convert-PSDCLocalUncPathToLocalPath -UncPath $Destination |
146 | | - } |
147 | | - else { |
148 | | - $Destination = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $commandGetLocalPath -Credential $DestinationCredential |
149 | | - } |
150 | | - } |
151 | | - catch { |
152 | | - Stop-PSFFunction -Message "Something went wrong getting the local image path" -Target $Destination |
153 | | - return |
154 | | - } |
155 | | - } |
156 | | - |
157 | 121 | # Random string |
158 | 122 | $random = -join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object {[char]$_}) |
159 | 123 |
|
|
180 | 144 | Stop-PSFFunction -Message "Could not connect to Sql Server instance $SqlInstance" -ErrorRecord $_ -Target $SqlInstance |
181 | 145 | } |
182 | 146 |
|
| 147 | + # Setup the computer object |
| 148 | + $computer = [PsfComputer]$server.Name |
| 149 | + |
183 | 150 | # Check destination |
184 | 151 | if (-not $Destination) { |
185 | 152 | $Destination = "$($server.DefaultFile)\clone" |
186 | 153 | } |
187 | 154 | else { |
| 155 | + # If the destination is a network path |
| 156 | + if ($Destination.StartsWith("\\")) { |
| 157 | + Write-PSFMessage -Message "The destination cannot be an UNC path. Trying to convert to local path" -Level Verbose |
| 158 | + |
| 159 | + try { |
| 160 | + # Check if computer is local |
| 161 | + if ($computer.IsLocalhost) { |
| 162 | + $Destination = Convert-PSDCLocalUncPathToLocalPath -UncPath $Destination -Credential $Credential |
| 163 | + } |
| 164 | + else { |
| 165 | + $command = [ScriptBlock]::Create("Convert-PSDCLocalUncPathToLocalPath -UncPath '$Destination'") |
| 166 | + $Destination = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 167 | + } |
| 168 | + } |
| 169 | + catch { |
| 170 | + Stop-PSFFunction -Message "Something went wrong getting the local image path" -Target $Destination |
| 171 | + return |
| 172 | + } |
| 173 | + } |
| 174 | + |
188 | 175 | # Remove the last "\" from the path it would mess up the mount of the VHD |
189 | 176 | if ($Destination.EndsWith("\")) { |
190 | 177 | $Destination = $Destination.Substring(0, $Destination.Length - 1) |
191 | 178 | } |
192 | 179 |
|
193 | | - if (-not (Test-Path -Path $Destination)) { |
| 180 | + # Test if the destination can be reached |
| 181 | + if (-not (Test-Path -Path $Destination -Credential $Credential)) { |
194 | 182 | Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance |
195 | 183 | } |
196 | 184 | } |
|
213 | 201 | " |
214 | 202 |
|
215 | 203 | try { |
216 | | - $result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException |
| 204 | + $result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException |
217 | 205 |
|
218 | 206 | # Check the results |
219 | 207 | if ($null -eq $result) { |
|
257 | 245 | $accessPath = "$Destination\$mountDirectory" |
258 | 246 |
|
259 | 247 | # Check if access path is already present |
260 | | - if (-not (Test-Path -Path $accessPath)) { |
| 248 | + if (-not (Test-Path -Path $accessPath -Credential $sou)) { |
261 | 249 | try { |
262 | | - $null = New-Item -Path $accessPath -ItemType Directory -Force |
| 250 | + # Check if computer is local |
| 251 | + if ($computer.IsLocalhost) { |
| 252 | + |
| 253 | + } |
| 254 | + else { |
| 255 | + $command = [ScriptBlock]::Create("") |
| 256 | + $Destination = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 257 | + } |
| 258 | + $null = New-Item -Path $accessPath -ItemType Directory -Credential $Credential -Force |
263 | 259 | } |
264 | 260 | catch { |
265 | 261 | Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue |
266 | 262 | } |
267 | 263 | } |
268 | 264 |
|
269 | 265 | # Check if the clone vhd does not yet exist |
270 | | - if (Test-Path -Path "$Destination\$CloneName.vhdx") { |
| 266 | + if (Test-Path -Path "$Destination\$CloneName.vhdx" -Credential $DestinationCredential) { |
271 | 267 | Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue |
272 | 268 | } |
273 | 269 |
|
274 | 270 | # Create the new child vhd |
275 | 271 | try { |
276 | 272 | Write-PSFMessage -Message "Creating clone from $ParentVhd" -Level Verbose |
277 | 273 |
|
| 274 | + # Check if computer is local |
| 275 | + if ($computer.IsLocalhost) { |
| 276 | + |
| 277 | + } |
| 278 | + else { |
| 279 | + $command = [ScriptBlock]::Create("") |
| 280 | + $Destination = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 281 | + } |
278 | 282 | $vhd = New-VHD -ParentPath $ParentVhd -Path "$Destination\$CloneName.vhdx" -Differencing |
279 | 283 | } |
280 | 284 | catch { |
|
285 | 289 | try { |
286 | 290 | Write-PSFMessage -Message "Mounting clone" -Level Verbose |
287 | 291 |
|
| 292 | + # Check if computer is local |
| 293 | + if ($computer.IsLocalhost) { |
| 294 | + |
| 295 | + } |
| 296 | + else { |
| 297 | + $command = [ScriptBlock]::Create("") |
| 298 | + $Destination = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 299 | + } |
288 | 300 | # Mount the disk |
289 | 301 | $null = Mount-VHD -Path "$Destination\$CloneName.vhdx" -NoDriveLetter |
290 | 302 |
|
|
298 | 310 |
|
299 | 311 | # Check if the disk is offline |
300 | 312 | if ($disk.OperationalStatus -eq 'Offline') { |
301 | | - $null = Initialize-Disk -Number $disk.DiskNumber -PartitionStyle GPT -ErrorAction SilentlyContinue |
| 313 | + # Check if computer is local |
| 314 | + if ($computer.IsLocalhost) { |
| 315 | + $null = Initialize-Disk -Number $disk.DiskNumber -PartitionStyle GPT -ErrorAction SilentlyContinue |
| 316 | + } |
| 317 | + else { |
| 318 | + $command = [ScriptBlock]::Create("Initialize-Disk -Number $($disk.DiskNumber) -PartitionStyle GPT -ErrorAction SilentlyContinue") |
| 319 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 320 | + } |
302 | 321 | } |
303 | 322 |
|
304 | 323 | try { |
305 | | - # Get the partition based on the disk |
306 | | - $partition = Get-Partition -Disk $disk |
| 324 | + # Check if computer is local |
| 325 | + if ($computer.IsLocalhost) { |
| 326 | + # Get the partition based on the disk |
| 327 | + $partition = Get-Partition -Disk $disk |
| 328 | + |
| 329 | + # Create an access path for the disk |
| 330 | + $null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction Ignore |
| 331 | + } |
| 332 | + else { |
| 333 | + $command = [ScriptBlock]::Create("Get-Partition -Disk $disk") |
| 334 | + $partition = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 335 | + |
| 336 | + $command = [ScriptBlock]::Create("Add-PartitionAccessPath -DiskNumber $($disk.Number) -PartitionNumber $($partition[1].PartitionNumber) -AccessPath $accessPath -ErrorAction Ignore") |
| 337 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 338 | + } |
307 | 339 |
|
308 | | - # Create an access path for the disk |
309 | | - $null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction Ignore |
310 | 340 | } |
311 | 341 | catch { |
312 | 342 | Stop-PSFFunction -Message "Couldn't create access path for partition" -ErrorRecord $_ -Target $partition -Continue |
313 | 343 | } |
314 | 344 |
|
315 | 345 | # Get all the files of the database |
316 | | - $databaseFiles = Get-ChildItem -Path $accessPath -Recurse | Where-Object {-not $_.PSIsContainer} |
| 346 | + # Check if computer is local |
| 347 | + if ($computer.IsLocalhost) { |
| 348 | + $databaseFiles = Get-ChildItem -Path $accessPath -Recurse | Where-Object {-not $_.PSIsContainer} |
| 349 | + } |
| 350 | + else { |
| 351 | + $command = [ScriptBlock]::Create("Get-ChildItem -Path $accessPath -Recurse | Where-Object {-not $($_.PSIsContainer)}") |
| 352 | + $databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 353 | + } |
| 354 | + |
317 | 355 |
|
318 | 356 | # Setup the database filestructure |
319 | 357 | $dbFileStructure = New-Object System.Collections.Specialized.StringCollection |
|
327 | 365 | try { |
328 | 366 | Write-PSFMessage -Message "Mounting database from clone" -Level Verbose |
329 | 367 |
|
330 | | - $null = Mount-DbaDatabase -SqlInstance $SqlInstance -Database $cloneDatabase -FileStructure $dbFileStructure |
| 368 | + # Check if computer is local |
| 369 | + if ($computer.IsLocalhost) { |
| 370 | + $null = Mount-DbaDatabase -SqlInstance $SqlInstance -Database $cloneDatabase -FileStructure $dbFileStructure |
| 371 | + } |
| 372 | + else { |
| 373 | + $command = [ScriptBlock]::Create("Mount-DbaDatabase -SqlInstance $SqlInstance -Database $cloneDatabase -FileStructure $dbFileStructure") |
| 374 | + $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
| 375 | + } |
331 | 376 | } |
332 | 377 | catch { |
333 | 378 | Stop-PSFFunction -Message "Couldn't mount database $cloneDatabase" -Target $SqlInstance -Continue |
|
355 | 400 | " |
356 | 401 |
|
357 | 402 | # Execute the query |
358 | | - $result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException |
| 403 | + $result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException |
359 | 404 | } |
360 | 405 | catch { |
361 | 406 | Stop-PSFFunction -Message "Couldnt execute query to see if host was known" -Target $query -ErrorRecord $_ -Continue |
|
378 | 423 | Write-PSFMessage -Message "Query New Host`n$query" -Level Debug |
379 | 424 |
|
380 | 425 | try { |
381 | | - $hostId = (Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException).HostID |
| 426 | + $hostId = (Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException).HostID |
382 | 427 | } |
383 | 428 | catch { |
384 | 429 | Stop-PSFFunction -Message "Couldnt execute query for adding host" -Target $query -ErrorRecord $_ -Continue |
|
389 | 434 | $query = "SELECT HostID FROM Host WHERE HostName = '$hostname'" |
390 | 435 |
|
391 | 436 | try { |
392 | | - $hostId = (Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException).HostID |
| 437 | + $hostId = (Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException).HostID |
393 | 438 | } |
394 | 439 | catch { |
395 | 440 | Stop-PSFFunction -Message "Couldnt execute query for retrieving host id" -Target $query -ErrorRecord $_ -Continue |
|
401 | 446 | Write-PSFMessage -Message "Selecting image from database" -Level Verbose |
402 | 447 | try { |
403 | 448 | $query = "SELECT ImageID, ImageName FROM dbo.Image WHERE ImageLocation = '$ParentVhd'" |
404 | | - $resultImage = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException |
| 449 | + $resultImage = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException |
405 | 450 | } |
406 | 451 | catch { |
407 | 452 | Stop-PSFFunction -Message "Couldnt execute query for retrieving image id" -Target $query -ErrorRecord $_ -Continue |
|
432 | 477 |
|
433 | 478 | # execute the query |
434 | 479 | try { |
435 | | - $result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException |
| 480 | + $result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException |
436 | 481 | } |
437 | 482 | catch { |
438 | 483 | Stop-PSFFunction -Message "Couldnt execute query for adding clone" -Target $query -ErrorRecord $_ -Continue |
|
0 commit comments