Skip to content

Commit ff9126d

Browse files
committed
Added remote execution support
1 parent 3252736 commit ff9126d

1 file changed

Lines changed: 98 additions & 53 deletions

File tree

functions/New-PSDCClone.ps1

Lines changed: 98 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -118,42 +118,6 @@
118118

119119
Write-PSFMessage -Message "Started image creation" -Level Verbose
120120

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-
157121
# Random string
158122
$random = -join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object {[char]$_})
159123

@@ -180,17 +144,41 @@
180144
Stop-PSFFunction -Message "Could not connect to Sql Server instance $SqlInstance" -ErrorRecord $_ -Target $SqlInstance
181145
}
182146

147+
# Setup the computer object
148+
$computer = [PsfComputer]$server.Name
149+
183150
# Check destination
184151
if (-not $Destination) {
185152
$Destination = "$($server.DefaultFile)\clone"
186153
}
187154
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+
188175
# Remove the last "\" from the path it would mess up the mount of the VHD
189176
if ($Destination.EndsWith("\")) {
190177
$Destination = $Destination.Substring(0, $Destination.Length - 1)
191178
}
192179

193-
if (-not (Test-Path -Path $Destination)) {
180+
# Test if the destination can be reached
181+
if (-not (Test-Path -Path $Destination -Credential $Credential)) {
194182
Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance
195183
}
196184
}
@@ -213,7 +201,7 @@
213201
"
214202

215203
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
217205

218206
# Check the results
219207
if ($null -eq $result) {
@@ -257,24 +245,40 @@
257245
$accessPath = "$Destination\$mountDirectory"
258246

259247
# Check if access path is already present
260-
if (-not (Test-Path -Path $accessPath)) {
248+
if (-not (Test-Path -Path $accessPath -Credential $sou)) {
261249
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
263259
}
264260
catch {
265261
Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue
266262
}
267263
}
268264

269265
# 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) {
271267
Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue
272268
}
273269

274270
# Create the new child vhd
275271
try {
276272
Write-PSFMessage -Message "Creating clone from $ParentVhd" -Level Verbose
277273

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+
}
278282
$vhd = New-VHD -ParentPath $ParentVhd -Path "$Destination\$CloneName.vhdx" -Differencing
279283
}
280284
catch {
@@ -285,6 +289,14 @@
285289
try {
286290
Write-PSFMessage -Message "Mounting clone" -Level Verbose
287291

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+
}
288300
# Mount the disk
289301
$null = Mount-VHD -Path "$Destination\$CloneName.vhdx" -NoDriveLetter
290302

@@ -298,22 +310,48 @@
298310

299311
# Check if the disk is offline
300312
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+
}
302321
}
303322

304323
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+
}
307339

308-
# Create an access path for the disk
309-
$null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction Ignore
310340
}
311341
catch {
312342
Stop-PSFFunction -Message "Couldn't create access path for partition" -ErrorRecord $_ -Target $partition -Continue
313343
}
314344

315345
# 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+
317355

318356
# Setup the database filestructure
319357
$dbFileStructure = New-Object System.Collections.Specialized.StringCollection
@@ -327,7 +365,14 @@
327365
try {
328366
Write-PSFMessage -Message "Mounting database from clone" -Level Verbose
329367

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+
}
331376
}
332377
catch {
333378
Stop-PSFFunction -Message "Couldn't mount database $cloneDatabase" -Target $SqlInstance -Continue
@@ -355,7 +400,7 @@
355400
"
356401

357402
# 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
359404
}
360405
catch {
361406
Stop-PSFFunction -Message "Couldnt execute query to see if host was known" -Target $query -ErrorRecord $_ -Continue
@@ -378,7 +423,7 @@
378423
Write-PSFMessage -Message "Query New Host`n$query" -Level Debug
379424

380425
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
382427
}
383428
catch {
384429
Stop-PSFFunction -Message "Couldnt execute query for adding host" -Target $query -ErrorRecord $_ -Continue
@@ -389,7 +434,7 @@
389434
$query = "SELECT HostID FROM Host WHERE HostName = '$hostname'"
390435

391436
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
393438
}
394439
catch {
395440
Stop-PSFFunction -Message "Couldnt execute query for retrieving host id" -Target $query -ErrorRecord $_ -Continue
@@ -401,7 +446,7 @@
401446
Write-PSFMessage -Message "Selecting image from database" -Level Verbose
402447
try {
403448
$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
405450
}
406451
catch {
407452
Stop-PSFFunction -Message "Couldnt execute query for retrieving image id" -Target $query -ErrorRecord $_ -Continue
@@ -432,7 +477,7 @@
432477

433478
# execute the query
434479
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
436481
}
437482
catch {
438483
Stop-PSFFunction -Message "Couldnt execute query for adding clone" -Target $query -ErrorRecord $_ -Continue

0 commit comments

Comments
 (0)