Skip to content

Commit 6f54d53

Browse files
committed
Added remote support
1 parent be144a1 commit 6f54d53

1 file changed

Lines changed: 51 additions & 8 deletions

File tree

functions/Invoke-PSDCRepairClone.ps1

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
2222
To connect as a different Windows user, run PowerShell as that user.
2323
24+
.PARAMETER PSDCSqlCredential
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+
2428
.PARAMETER PSDCSqlCredential
2529
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
2630
This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
@@ -61,22 +65,31 @@
6165
[System.Management.Automation.PSCredential]
6266
$SqlCredential,
6367
[System.Management.Automation.PSCredential]
64-
$PSDCSqlCredential,
68+
$Credential,
69+
[System.Management.Automation.PSCredential]
70+
$pdcCredential,
6571
[switch]$EnableException
6672
)
6773

6874
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 = $pdcCredential
83+
}
84+
6985
# Test the module database setup
7086
try {
71-
Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException
87+
Test-PSDCConfiguration -SqlCredential $pdcCredential -EnableException
7288
}
7389
catch {
7490
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
7591
}
7692

77-
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
78-
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
79-
8093
}
8194

8295
process {
@@ -86,6 +99,21 @@
8699
# Loop through each of the hosts
87100
foreach ($hst in $HostName) {
88101

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+
89117
$query = "
90118
SELECT i.ImageLocation,
91119
c.CloneLocation,
@@ -105,7 +133,7 @@
105133
# Get the clones registered for the host
106134
try {
107135
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
109137
}
110138
catch {
111139
Stop-PSFFunction -Message "Couldn't get the clones for $hst" -Target $pdcSqlInstance -ErrorRecord $_ -Continue
@@ -125,7 +153,14 @@
125153
try {
126154
Write-PSFMessage -Message "Mounting vhd $($result.CloneLocation)" -Level Verbose
127155

128-
Mount-VHD -Path $result.CloneLocation -NoDriveLetter -ErrorAction SilentlyContinue
156+
# Check if computer is local
157+
if ($computer.IsLocalhost) {
158+
$null = Mount-VHD -Path $result.CloneLocation -NoDriveLetter -ErrorAction SilentlyContinue
159+
}
160+
else {
161+
$command = [ScriptBlock]::Create("Mount-VHD -Path $($result.CloneLocation) -NoDriveLetter -ErrorAction SilentlyContinue")
162+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
163+
}
129164
}
130165
catch {
131166
Stop-PSFFunction -Message "Couldn't mount vhd" -Target $clone -Continue
@@ -139,7 +174,15 @@
139174
if ($result.DatabaseName -notin $databases.Name) {
140175

141176
# Get all the files of the database
142-
$databaseFiles = Get-ChildItem -Path $result.AccessPath -Recurse | Where-Object {-not $_.PSIsContainer}
177+
# Check if computer is local
178+
if ($computer.IsLocalhost) {
179+
$databaseFiles = Get-ChildItem -Path $result.AccessPath -Recurse | Where-Object {-not $_.PSIsContainer}
180+
}
181+
else {
182+
$commandText = "Get-ChildItem -Path $($result.AccessPath) -Recurse | " + 'Where-Object {-not $_.PSIsContainer}'
183+
$command = [ScriptBlock]::Create($commandText)
184+
$databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
185+
}
143186

144187
# Setup the database filestructure
145188
$dbFileStructure = New-Object System.Collections.Specialized.StringCollection

0 commit comments

Comments
 (0)