Skip to content

Commit 4c4c499

Browse files
committed
Fixed authentication to PSDC database and database server
1 parent 4d1c898 commit 4c4c499

7 files changed

Lines changed: 63 additions & 21 deletions

functions/Get-PSDCClone.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
1616
To connect as a different Windows user, run PowerShell as that user.
1717
18+
.PARAMETER PSDCSqlCredential
19+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
20+
This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
21+
1822
.PARAMETER HostName
1923
Filter based on the hostname
2024
@@ -60,6 +64,8 @@
6064

6165
param(
6266
[System.Management.Automation.PSCredential]$SqlCredential,
67+
[System.Management.Automation.PSCredential]
68+
$PSDCSqlCredential,
6369
[string[]]$HostName,
6470
[string[]]$Database,
6571
[int[]]$ImageID,
@@ -100,7 +106,7 @@
100106

101107
try {
102108
$results = @()
103-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -As PSObject
109+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -As PSObject
104110
}
105111
catch {
106112
Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query

functions/Get-PSDCImage.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
1616
To connect as a different Windows user, run PowerShell as that user.
1717
18+
.PARAMETER PSDCSqlCredential
19+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
20+
This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
21+
1822
.PARAMETER ImageID
1923
Filter based on the image id
2024
@@ -57,6 +61,8 @@
5761

5862
param(
5963
[System.Management.Automation.PSCredential]$SqlCredential,
64+
[System.Management.Automation.PSCredential]
65+
$PSDCSqlCredential,
6066
[int[]]$ImageID,
6167
[string[]]$ImageName,
6268
[string[]]$ImageLocation,
@@ -88,7 +94,7 @@
8894

8995
try {
9096
$results = @()
91-
$results += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -As PSObject
97+
$results += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -As PSObject
9298
}
9399
catch {
94100
Stop-PSFFunction -Message "Could retrieve images from database $pdcDatabase" -ErrorRecord $_ -Target $query

functions/Invoke-PSDCRepairClone.ps1

Lines changed: 7 additions & 1 deletion
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 PSDatabaseClone database server and database.
27+
2428
.PARAMETER EnableException
2529
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
2630
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
@@ -56,6 +60,8 @@
5660
[string[]]$HostName,
5761
[System.Management.Automation.PSCredential]
5862
$SqlCredential,
63+
[System.Management.Automation.PSCredential]
64+
$PSDCSqlCredential,
5965
[switch]$EnableException
6066
)
6167

@@ -99,7 +105,7 @@
99105
# Get the clones registered for the host
100106
try {
101107
Write-PSFMessage -Message "Get the clones for host $hst" -Level Verbose
102-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query
108+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query
103109
}
104110
catch {
105111
Stop-PSFFunction -Message "Couldn't get the clones for $hst" -Target $pdcSqlInstance -ErrorRecord $_ -Continue

functions/New-PSDCClone.ps1

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
1919
To connect as a different Windows user, run PowerShell as that user.
2020
21+
.PARAMETER PSDCSqlCredential
22+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
23+
This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
24+
2125
.PARAMETER Credential
2226
Allows you to login to servers using Windows Auth/Integrated/Trusted. To use:
2327
@@ -89,6 +93,8 @@
8993
[System.Management.Automation.PSCredential]
9094
$SqlCredential,
9195
[System.Management.Automation.PSCredential]
96+
$PSDCSqlCredential,
97+
[System.Management.Automation.PSCredential]
9298
$Credential,
9399
[parameter(Mandatory = $true, ParameterSetName = "ByParent")]
94100
[string]$ParentVhd,
@@ -201,7 +207,7 @@
201207
"
202208

203209
try {
204-
$result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException
210+
$result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException
205211

206212
# Check the results
207213
if ($null -eq $result) {
@@ -367,10 +373,10 @@
367373

368374
# Check if computer is local
369375
if ($computer.IsLocalhost) {
370-
$null = Mount-DbaDatabase -SqlInstance $SqlInstance -Database $cloneDatabase -FileStructure $dbFileStructure
376+
$null = Mount-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $cloneDatabase -FileStructure $dbFileStructure
371377
}
372378
else {
373-
$command = [ScriptBlock]::Create("Mount-DbaDatabase -SqlInstance $SqlInstance -Database $cloneDatabase -FileStructure $dbFileStructure")
379+
$command = [ScriptBlock]::Create("Mount-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $cloneDatabase -FileStructure $dbFileStructure")
374380
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
375381
}
376382
}
@@ -400,7 +406,7 @@
400406
"
401407

402408
# Execute the query
403-
$result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException
409+
$result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException
404410
}
405411
catch {
406412
Stop-PSFFunction -Message "Couldnt execute query to see if host was known" -Target $query -ErrorRecord $_ -Continue
@@ -423,7 +429,7 @@
423429
Write-PSFMessage -Message "Query New Host`n$query" -Level Debug
424430

425431
try {
426-
$hostId = (Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException).HostID
432+
$hostId = (Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException).HostID
427433
}
428434
catch {
429435
Stop-PSFFunction -Message "Couldnt execute query for adding host" -Target $query -ErrorRecord $_ -Continue
@@ -434,7 +440,7 @@
434440
$query = "SELECT HostID FROM Host WHERE HostName = '$hostname'"
435441

436442
try {
437-
$hostId = (Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException).HostID
443+
$hostId = (Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException).HostID
438444
}
439445
catch {
440446
Stop-PSFFunction -Message "Couldnt execute query for retrieving host id" -Target $query -ErrorRecord $_ -Continue
@@ -446,7 +452,7 @@
446452
Write-PSFMessage -Message "Selecting image from database" -Level Verbose
447453
try {
448454
$query = "SELECT ImageID, ImageName FROM dbo.Image WHERE ImageLocation = '$ParentVhd'"
449-
$resultImage = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException
455+
$resultImage = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException
450456
}
451457
catch {
452458
Stop-PSFFunction -Message "Couldnt execute query for retrieving image id" -Target $query -ErrorRecord $_ -Continue
@@ -477,7 +483,7 @@
477483

478484
# execute the query
479485
try {
480-
$result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -EnableException
486+
$result = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException
481487
}
482488
catch {
483489
Stop-PSFFunction -Message "Couldnt execute query for adding clone" -Target $query -ErrorRecord $_ -Continue

functions/New-PSDCImage.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
4040
$scred = Get-Credential, then pass $scred object to the -DestinationCredential parameter.
4141
42+
.PARAMETER PSDCSqlCredential
43+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
44+
This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
45+
4246
.PARAMETER ImageNetworkPath
4347
Network path where to save the image. This has to be a UNC path
4448
@@ -103,6 +107,8 @@
103107
$DestinationSqlCredential,
104108
[System.Management.Automation.PSCredential]
105109
$DestinationCredential,
110+
[System.Management.Automation.PSCredential]
111+
$PSDCSqlCredential,
106112
[Parameter(Mandatory = $true)]
107113
[ValidateNotNullOrEmpty()]
108114
[string]$ImageNetworkPath,
@@ -250,17 +256,17 @@
250256
if ($CreateFullBackup) {
251257
# Create the backup
252258
Write-PSFMessage -Message "Creating new full backup for database $db" -Level Verbose
253-
$null = Backup-DbaDatabase -SqlInstance $SourceSqlInstance -Database $db.Name
259+
$null = Backup-DbaDatabase -SqlInstance $SourceSqlInstance -SqlCredential $SourceSqlCredential -Database $db.Name
254260

255261
# Get the last full backup
256262
Write-PSFMessage -Message "Trying to retrieve the last full backup for $db" -Level Verbose
257-
$lastFullBackup = Get-DbaBackupHistory -SqlServer $SourceSqlInstance -Databases $db.Name -LastFull -Credential $SourceSqlCredential
263+
$lastFullBackup = Get-DbaBackupHistory -SqlServer $SourceSqlInstance -SqlCredential $SourceSqlCredential -Databases $db.Name -LastFull
258264
}
259265
elseif ($UseLastFullBackup) {
260266
Write-PSFMessage -Message "Trying to retrieve the last full backup for $db" -Level Verbose
261267

262268
# Get the last full backup
263-
$lastFullBackup = Get-DbaBackupHistory -SqlServer $SourceSqlInstance -Databases $db.Name -LastFull -Credential $SourceSqlCredential
269+
$lastFullBackup = Get-DbaBackupHistory -SqlServer $SourceSqlInstance -SqlCredential $SourceSqlCredential -Databases $db.Name -LastFull
264270
}
265271

266272
# try to create the new VHD
@@ -450,7 +456,7 @@
450456
try {
451457
Write-PSFMessage -Message "Saving image information in database" -Level Verbose
452458

453-
$result += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException
459+
$result += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException
454460
}
455461
catch {
456462
Stop-PSFFunction -Message "Couldn't add image to database" -Target $imageName -ErrorRecord $_

functions/Remove-PSDCClone.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
2323
To connect as a different Windows user, run PowerShell as that user.
2424
25+
.PARAMETER PSDCSqlCredential
26+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
27+
This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
28+
2529
.PARAMETER Credential
2630
Allows you to login to systems using a credential. To use:
2731
@@ -86,6 +90,8 @@
8690
[System.Management.Automation.PSCredential]
8791
$SqlCredential,
8892
[System.Management.Automation.PSCredential]
93+
$PSDCSqlCredential,
94+
[System.Management.Automation.PSCredential]
8995
$Credential,
9096
[string[]]$Database,
9197
[string[]]$ExcludeDatabase,
@@ -197,8 +203,8 @@
197203
# Removing records from database
198204
try {
199205
$query = "DELETE FROM dbo.Clone WHERE CloneID = $($item.CloneID);"
200-
Write-PSFMessage -Message $query -Level Verbose
201-
Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -EnableException
206+
207+
$null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -EnableException
202208
}
203209
catch {
204210
Stop-PSFFunction -Message "Could not remove clone record from database" -ErrorRecord $_ -Target $query -Continue

functions/Remove-PSDCImage.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
.PARAMETER ExcludeDatabase
2323
Filter the images based on the excluded database
2424
25+
.PARAMETER PSDCSqlCredential
26+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
27+
This works similar as SqlCredential but is only meant for authentication to the PSDatabaseClone database server and database.
28+
2529
.PARAMETER Credential
2630
Allows you to login to servers using Windows Auth/Integrated/Trusted. To use:
2731
@@ -75,6 +79,8 @@
7579
[string[]]$Database,
7680
[string[]]$ExcludeDatabase,
7781
[System.Management.Automation.PSCredential]
82+
$PSDCSqlCredential,
83+
[System.Management.Automation.PSCredential]
7884
$Credential,
7985
[switch]$Force,
8086
[parameter(ValueFromPipeline = $true, ParameterSetName = "Image")]
@@ -99,7 +105,7 @@
99105
Write-PSFMessage -Message "Started removing database images" -Level Verbose
100106

101107
# Get all the items
102-
$items = Get-PSDCImage
108+
$items = Get-PSDCImage -PSDCSqlCredential $PSDCSqlCredential
103109

104110
if ($ImageID) {
105111
Write-PSFMessage -Message "Filtering image ids" -Level Verbose
@@ -164,7 +170,7 @@
164170
try {
165171
Write-PSFMessage -Message "Retrieving data for image '$($item.Name)'" -Level Verbose
166172
$results = @()
167-
$results += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query
173+
$results += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query
168174

169175
# Check the results
170176
if ($results.Count -ge 1) {
@@ -175,7 +181,7 @@
175181
# Remove the clones for the host
176182
try {
177183
Write-PSFMessage -Message "Removing clones for host $($result.HostName) and database $($result.DatabaseName)" -Level Verbose
178-
Remove-PSDCClone -HostName $result.HostName -Database $result.DatabaseName -Credential $Credential -Confirm:$false
184+
Remove-PSDCClone -HostName $result.HostName -Database $result.DatabaseName -PSDCSqlCredential $PSDCSqlCredential -Credential $Credential -Confirm:$false
179185
}
180186
catch {
181187
Stop-PSFFunction -Message "Couldn't remove clones from host $($result.HostName)" -ErrorRecord $_ -Target $result -Continue
@@ -208,7 +214,7 @@
208214
try {
209215
$query = "DELETE FROM dbo.Image WHERE ImageID = $($item.ImageID)"
210216

211-
$null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query
217+
$null = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query
212218
}
213219
catch {
214220
Stop-PSFFunction -Message "Couldn't remove image '$($item.ImageLocation)' from database" -ErrorRecord $_ -Target $query

0 commit comments

Comments
 (0)