Skip to content

Commit 2ad4114

Browse files
committed
Added proper remote support. FIxed lots of errors
1 parent 7e0eb02 commit 2ad4114

1 file changed

Lines changed: 43 additions & 16 deletions

File tree

functions/New-PSDCClone.ps1

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
if (-not $PSDCSqlCredential) {
118118
$pdcCredential = Get-PSFConfigValue -FullName psdatabaseclone.database.credential -Fallback $null
119119
}
120-
else{
120+
else {
121121
$pdcCredential = $PSDCSqlCredential
122122
}
123123

@@ -258,14 +258,28 @@
258258
}
259259

260260
# Take apart the vhd directory
261-
if (Test-Path -Path $ParentVhd) {
262-
$parentVhdFileName = $ParentVhd.Split("\")[-1]
263-
$parentVhdFile = $parentVhdFileName.Split(".")[0]
261+
if ($computer.IsLocalhost) {
262+
if (Test-Path -Path $ParentVhd) {
263+
$parentVhdFileName = $ParentVhd.Split("\")[-1]
264+
$parentVhdFile = $parentVhdFileName.Split(".")[0]
265+
}
266+
else {
267+
Stop-PSFFunction -Message "Parent vhd could not be found" -Target $SqlInstance -Continue
268+
}
264269
}
265270
else {
266-
Stop-PSFFunction -Message "Parent vhd could not be found" -Target $SqlInstance -Continue
271+
$command = [scriptblock]::Create("Test-Path -Path $ParentVhd")
272+
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
273+
if ($result) {
274+
$parentVhdFileName = $ParentVhd.Split("\")[-1]
275+
$parentVhdFile = $parentVhdFileName.Split(".")[0]
276+
}
277+
else {
278+
Stop-PSFFunction -Message "Parent vhd could not be found" -Target $SqlInstance -Continue
279+
}
267280
}
268281

282+
269283
# Check clone name parameter
270284
if (-not $CloneName) {
271285
$cloneDatabase = $parentVhdFile
@@ -360,9 +374,7 @@
360374
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
361375

362376
# Get the disk based on the name of the vhd
363-
#$commandText = 'Get-Disk | Where-Object {$_.Location -eq' + " `"$Destination\$CloneName.vhdx`"}"
364-
$commandText = "Get-Vhd -Path `"$Destination\$CloneName.vhdx`""
365-
$command = [ScriptBlock]::Create($commandText)
377+
$command = [ScriptBlock]::Create("Get-Vhd -Path `"$Destination\$CloneName.vhdx`"")
366378
$disk = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
367379
}
368380
}
@@ -371,16 +383,17 @@
371383
}
372384

373385
# Check if the disk is offline
374-
if ($disk.OperationalStatus -eq 'Offline') {
386+
#if ($disk.OperationalStatus -eq 'Offline') {
375387
# Check if computer is local
376388
if ($computer.IsLocalhost) {
377-
$null = Initialize-Disk -Number $disk.DiskNumber -PartitionStyle GPT -ErrorAction SilentlyContinue
389+
$null = Initialize-Disk -Number $disk.Number -PartitionStyle GPT -ErrorAction SilentlyContinue
378390
}
379391
else {
380-
$command = [ScriptBlock]::Create("Initialize-Disk -Number $($disk.DiskNumber) -PartitionStyle GPT -ErrorAction SilentlyContinue")
392+
$command = [ScriptBlock]::Create("Initialize-Disk -Number $($disk.Number) -PartitionStyle GPT -ErrorAction SilentlyContinue")
393+
381394
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
382395
}
383-
}
396+
# }
384397

385398
try {
386399
# Check if computer is local
@@ -396,6 +409,7 @@
396409
$partition = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
397410

398411
$command = [ScriptBlock]::Create("Add-PartitionAccessPath -DiskNumber $($disk.Number) -PartitionNumber $($partition[1].PartitionNumber) -AccessPath $accessPath -ErrorAction Ignore")
412+
399413
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
400414
}
401415

@@ -436,11 +450,24 @@
436450
# Write the data to the database
437451
try {
438452
# Get the data of the host
439-
$computerinfo = [System.Net.Dns]::GetHostByName(($env:computerName))
453+
if ($computer.IsLocalhost) {
454+
$computerinfo = [System.Net.Dns]::GetHostByName(($env:computerName))
455+
456+
$hostname = $computerinfo.HostName
457+
$ipAddress = $computerinfo.AddressList[0]
458+
$fqdn = $computerinfo.HostName
459+
}
460+
else {
461+
$command = [scriptblock]::Create('[System.Net.Dns]::GetHostByName(($env:computerName))')
462+
$computerinfo = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
463+
464+
$command = [scriptblock]::Create('$env:COMPUTERNAME')
465+
466+
$hostname = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
467+
$ipAddress = $computerinfo.AddressList[0]
468+
$fqdn = $computerinfo.HostName
469+
}
440470

441-
$hostname = $env:computerName
442-
$ipAddress = $computerinfo.AddressList[0]
443-
$fqdn = $computerinfo.HostName
444471

445472
# Setup the query to check of the host is already added
446473
$query = "

0 commit comments

Comments
 (0)