Skip to content

Commit 9d9bcb5

Browse files
committed
Merge branch 'development' into 'master'
Development See merge request sanderstad/PSDatabaseClone!5
2 parents 0e373a2 + a95c81a commit 9d9bcb5

15 files changed

+858
-610
lines changed

functions/Get-PSDCClone.ps1

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,71 @@
11
function Get-PSDCClone {
2-
<#
3-
.SYNOPSIS
4-
Get-PSDCClone get on or more clones
2+
<#
3+
.SYNOPSIS
4+
Get-PSDCClone get on or more clones
55
6-
.DESCRIPTION
7-
Get-PSDCClone will retrieve the clones and apply filters if needed.
8-
By default all the clones are returned
6+
.DESCRIPTION
7+
Get-PSDCClone will retrieve the clones and apply filters if needed.
8+
By default all the clones are returned
99
10-
.PARAMETER HostName
11-
Filter based on the hostname
10+
.PARAMETER SqlCredential
11+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
1212
13-
.PARAMETER Database
14-
Filter based on the database
13+
$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.
1514
16-
.PARAMETER ImageID
17-
Filter based on the image id
15+
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
16+
To connect as a different Windows user, run PowerShell as that user.
1817
19-
.PARAMETER ImageName
20-
Filter based on the image name
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.
2121
22-
.PARAMETER ImageLocation
23-
Filter based on the image location
22+
.PARAMETER HostName
23+
Filter based on the hostname
2424
25-
.NOTES
26-
Author: Sander Stad (@sqlstad, sqlstad.nl)
25+
.PARAMETER Database
26+
Filter based on the database
2727
28-
Website: https://psdatabaseclone.io
29-
Copyright: (C) Sander Stad, [email protected]
30-
License: MIT https://opensource.org/licenses/MIT
28+
.PARAMETER ImageID
29+
Filter based on the image id
3130
32-
.LINK
33-
https://psdatabaseclone.io/
31+
.PARAMETER ImageName
32+
Filter based on the image name
3433
35-
.EXAMPLE
36-
Get-PSDCClone -HostName host1, host2
34+
.PARAMETER ImageLocation
35+
Filter based on the image location
3736
38-
Retrieve the clones for host1 and host2
37+
.NOTES
38+
Author: Sander Stad (@sqlstad, sqlstad.nl)
3939
40-
.EXAMPLE
41-
Get-PSDCClone -Database DB1
40+
Website: https://psdatabaseclone.io
41+
Copyright: (C) Sander Stad, [email protected]
42+
License: MIT https://opensource.org/licenses/MIT
4243
43-
Get all the clones that have the name DB1
44+
.LINK
45+
https://psdatabaseclone.io/
4446
45-
.EXAMPLE
46-
Get-PSDCClone -ImageName DB1_20180703085917
47+
.EXAMPLE
48+
Get-PSDCClone -HostName host1, host2
4749
48-
Get all the clones that were made with image "DB1_20180703085917"
49-
#>
50+
Retrieve the clones for host1 and host2
51+
52+
.EXAMPLE
53+
Get-PSDCClone -Database DB1
54+
55+
Get all the clones that have the name DB1
56+
57+
.EXAMPLE
58+
Get-PSDCClone -ImageName DB1_20180703085917
59+
60+
Get all the clones that were made with image "DB1_20180703085917"
61+
#>
5062

5163
[CmdLetBinding()]
5264

5365
param(
66+
[System.Management.Automation.PSCredential]$SqlCredential,
67+
[System.Management.Automation.PSCredential]
68+
$PSDCSqlCredential,
5469
[string[]]$HostName,
5570
[string[]]$Database,
5671
[int[]]$ImageID,
@@ -62,7 +77,7 @@
6277

6378
# Test the module database setup
6479
try {
65-
Test-PSDCConfiguration -EnableException
80+
Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException
6681
}
6782
catch {
6883
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
@@ -71,13 +86,6 @@
7186
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
7287
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
7388

74-
}
75-
76-
process {
77-
78-
# Test if there are any errors
79-
if (Test-PSFFunctionInterrupt) { return }
80-
8189
$query = "
8290
SELECT c.CloneID,
8391
c.CloneLocation,
@@ -98,53 +106,59 @@
98106

99107
try {
100108
$results = @()
101-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -As PSObject
109+
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -As PSObject
102110
}
103111
catch {
104112
Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query
105113
}
106114

107115
# Filter host name
108-
if($HostName){
116+
if ($HostName) {
109117
$results = $results | Where-Object {$_.HostName -in $HostName}
110118
}
111119

112120
# Filter image id
113-
if($Database){
121+
if ($Database) {
114122
$results = $results | Where-Object {$_.DatabaseName -in $Database}
115123
}
116124

117125
# Filter image id
118-
if($ImageID){
126+
if ($ImageID) {
119127
$results = $results | Where-Object {$_.ImageID -in $ImageID}
120128
}
121129

122130
# Filter image name
123-
if($ImageName){
131+
if ($ImageName) {
124132
$results = $results | Where-Object {$_.ImageName -in $ImageName}
125133
}
126134

127135
# Filter image location
128-
if($ImageLocation){
136+
if ($ImageLocation) {
129137
$results = $results | Where-Object {$_.ImageLocation -in $ImageLocation}
130138
}
131139

140+
}
141+
142+
process {
143+
144+
# Test if there are any errors
145+
if (Test-PSFFunctionInterrupt) { return }
146+
132147
# Convert the results to the PSDCClone data type
133-
foreach($result in $results){
134-
135-
[PSDCClone]$clone = New-Object PSDCClone
136-
$clone.CloneID = $result.CloneID
137-
$clone.CloneLocation = $result.CloneLocation
138-
$clone.AccessPath = $result.AccessPath
139-
$clone.SqlInstance = $result.SqlInstance
140-
$clone.DatabaseName = $result.DatabaseName
141-
$clone.IsEnabled = $result.IsEnabled
142-
$clone.ImageID = $result.ImageID
143-
$clone.ImageName = $result.ImageName
144-
$clone.ImageLocation = $result.ImageLocation
145-
$clone.HostName = $result.HostName
146-
147-
return $clone
148+
foreach ($result in $results) {
149+
150+
[pscustomobject]@{
151+
CloneID = $result.CloneID
152+
CloneLocation = $result.CloneLocation
153+
AccessPath = $result.AccessPath
154+
SqlInstance = $result.SqlInstance
155+
DatabaseName = $result.DatabaseName
156+
IsEnabled = $result.IsEnabled
157+
ImageID = $result.ImageID
158+
ImageName = $result.ImageName
159+
ImageLocation = $result.ImageLocation
160+
HostName = $result.HostName
161+
}
148162
}
149163

150164
}

functions/Get-PSDCImage.ps1

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
11
function Get-PSDCImage {
22
<#
3-
.SYNOPSIS
4-
Get-PSDCImage get on or more clones
3+
.SYNOPSIS
4+
Get-PSDCImage get on or more clones
55
6-
.DESCRIPTION
7-
Get-PSDCImage will retrieve the clones and apply filters if needed.
8-
By default all the clones are returned
6+
.DESCRIPTION
7+
Get-PSDCImage will retrieve the clones and apply filters if needed.
8+
By default all the clones are returned
99
10-
.PARAMETER ImageID
11-
Filter based on the image id
10+
.PARAMETER SqlCredential
11+
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
1212
13-
.PARAMETER ImageName
14-
Filter based on the image name
13+
$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.
1514
16-
.PARAMETER ImageLocation
17-
Filter based on the image location
15+
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
16+
To connect as a different Windows user, run PowerShell as that user.
1817
19-
.PARAMETER Database
20-
Filter based on the database
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.
2121
22-
.NOTES
23-
Author: Sander Stad (@sqlstad, sqlstad.nl)
22+
.PARAMETER ImageID
23+
Filter based on the image id
2424
25-
Website: https://psdatabaseclone.io
26-
Copyright: (C) Sander Stad, [email protected]
27-
License: MIT https://opensource.org/licenses/MIT
25+
.PARAMETER ImageName
26+
Filter based on the image name
2827
29-
.LINK
30-
https://psdatabaseclone.io/
28+
.PARAMETER ImageLocation
29+
Filter based on the image location
3130
32-
.EXAMPLE
33-
Get-PSDCImage -ImageName DB1_20180704220944, DB2_20180704221144
31+
.PARAMETER Database
32+
Filter based on the database
3433
35-
Retrieve the images for DB1_20180704220944, DB2_20180704221144
34+
.NOTES
35+
Author: Sander Stad (@sqlstad, sqlstad.nl)
3636
37-
.EXAMPLE
38-
Get-PSDCImage -ImageLocation "\\fileserver1\psdatabaseclone\images\DB1_20180704220944.vhdx"
37+
Website: https://psdatabaseclone.io
38+
Copyright: (C) Sander Stad, [email protected]
39+
License: MIT https://opensource.org/licenses/MIT
3940
40-
Get all the images that are the same as the image location
41+
.LINK
42+
https://psdatabaseclone.io/
4143
42-
.EXAMPLE
43-
Get-PSDCImage -Database DB1, DB2
44+
.EXAMPLE
45+
Get-PSDCImage -ImageName DB1_20180704220944, DB2_20180704221144
4446
45-
Get all the images that were made for databases DB1 and DB2
46-
#>
47+
Retrieve the images for DB1_20180704220944, DB2_20180704221144
48+
49+
.EXAMPLE
50+
Get-PSDCImage -ImageLocation "\\fileserver1\psdatabaseclone\images\DB1_20180704220944.vhdx"
51+
52+
Get all the images that are the same as the image location
53+
54+
.EXAMPLE
55+
Get-PSDCImage -Database DB1, DB2
56+
57+
Get all the images that were made for databases DB1 and DB2
58+
#>
4759

4860
[CmdLetBinding()]
4961

5062
param(
63+
[System.Management.Automation.PSCredential]$SqlCredential,
64+
[System.Management.Automation.PSCredential]
65+
$PSDCSqlCredential,
5166
[int[]]$ImageID,
5267
[string[]]$ImageName,
5368
[string[]]$ImageLocation,
@@ -57,19 +72,14 @@
5772
begin {
5873
# Test the module database setup
5974
try {
60-
Test-PSDCConfiguration -EnableException
75+
Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException
6176
}
6277
catch {
6378
Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue
6479
}
6580

6681
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
6782
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
68-
}
69-
70-
process {
71-
# Test if there are any errors
72-
if (Test-PSFFunctionInterrupt) { return }
7383

7484
$query = "
7585
SELECT ImageID,
@@ -83,11 +93,11 @@
8393
"
8494

8595
try {
86-
$result = @()
87-
$results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -As PSObject
96+
$results = @()
97+
$results += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $PSDCSqlCredential -Database $pdcDatabase -Query $query -As PSObject
8898
}
8999
catch {
90-
Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query
100+
Stop-PSFFunction -Message "Could retrieve images from database $pdcDatabase" -ErrorRecord $_ -Target $query
91101
}
92102

93103
# Filter image id
@@ -109,22 +119,27 @@
109119
if ($Database) {
110120
$results = $results | Where-Object {$_.DatabaseName -in $Database}
111121
}
122+
}
112123

124+
process {
125+
# Test if there are any errors
126+
if (Test-PSFFunctionInterrupt) { return }
113127

114128
# Convert the results to the PSDCClone data type
115-
foreach($result in $results){
116-
117-
[PSDCImage]$image = New-Object PSDCImage
118-
$image.ImageID = $result.ImageID
119-
$image.ImageName = $result.ImageName
120-
$image.ImageLocation = $result.ImageLocation
121-
$image.SizeMB = $result.SizeMB
122-
$image.DatabaseName = $result.DatabaseName
123-
$image.DatabaseTimestamp = $result.DatabaseTimestamp
124-
$image.CreatedOn = $result.CreatedOn
125-
126-
return $image
129+
foreach ($result in $results) {
130+
131+
[pscustomobject]@{
132+
ImageID = $result.ImageID
133+
ImageName = $result.ImageName
134+
ImageLocation = $result.ImageLocation
135+
SizeMB = $result.SizeMB
136+
DatabaseName = $result.DatabaseName
137+
DatabaseTimestamp = $result.DatabaseTimestamp
138+
CreatedOn = $result.CreatedOn
139+
}
140+
127141
}
142+
128143
}
129144

130145
end {

0 commit comments

Comments
 (0)