|
1 | 1 | 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 |
5 | 5 |
|
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 |
9 | 9 |
|
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: |
12 | 12 |
|
13 | | -.PARAMETER Database |
14 | | - Filter based on the database |
| 13 | + $scred = Get-Credential, then pass $scred object to the -SqlCredential parameter. |
15 | 14 |
|
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. |
18 | 17 |
|
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. |
21 | 21 |
|
22 | | -.PARAMETER ImageLocation |
23 | | - Filter based on the image location |
| 22 | + .PARAMETER HostName |
| 23 | + Filter based on the hostname |
24 | 24 |
|
25 | | -.NOTES |
26 | | - Author: Sander Stad (@sqlstad, sqlstad.nl) |
| 25 | + .PARAMETER Database |
| 26 | + Filter based on the database |
27 | 27 |
|
28 | | - Website: https://psdatabaseclone.io |
29 | | - Copyright: (C) Sander Stad, sander@sqlstad.nl |
30 | | - License: MIT https://opensource.org/licenses/MIT |
| 28 | + .PARAMETER ImageID |
| 29 | + Filter based on the image id |
31 | 30 |
|
32 | | -.LINK |
33 | | - https://psdatabaseclone.io/ |
| 31 | + .PARAMETER ImageName |
| 32 | + Filter based on the image name |
34 | 33 |
|
35 | | -.EXAMPLE |
36 | | - Get-PSDCClone -HostName host1, host2 |
| 34 | + .PARAMETER ImageLocation |
| 35 | + Filter based on the image location |
37 | 36 |
|
38 | | - Retrieve the clones for host1 and host2 |
| 37 | + .NOTES |
| 38 | + Author: Sander Stad (@sqlstad, sqlstad.nl) |
39 | 39 |
|
40 | | -.EXAMPLE |
41 | | - Get-PSDCClone -Database DB1 |
| 40 | + Website: https://psdatabaseclone.io |
| 41 | + Copyright: (C) Sander Stad, sander@sqlstad.nl |
| 42 | + License: MIT https://opensource.org/licenses/MIT |
42 | 43 |
|
43 | | - Get all the clones that have the name DB1 |
| 44 | + .LINK |
| 45 | + https://psdatabaseclone.io/ |
44 | 46 |
|
45 | | -.EXAMPLE |
46 | | - Get-PSDCClone -ImageName DB1_20180703085917 |
| 47 | + .EXAMPLE |
| 48 | + Get-PSDCClone -HostName host1, host2 |
47 | 49 |
|
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 | + #> |
50 | 62 |
|
51 | 63 | [CmdLetBinding()] |
52 | 64 |
|
53 | 65 | param( |
| 66 | + [System.Management.Automation.PSCredential]$SqlCredential, |
| 67 | + [System.Management.Automation.PSCredential] |
| 68 | + $PSDCSqlCredential, |
54 | 69 | [string[]]$HostName, |
55 | 70 | [string[]]$Database, |
56 | 71 | [int[]]$ImageID, |
|
62 | 77 |
|
63 | 78 | # Test the module database setup |
64 | 79 | try { |
65 | | - Test-PSDCConfiguration -EnableException |
| 80 | + Test-PSDCConfiguration -SqlCredential $PSDCSqlCredential -EnableException |
66 | 81 | } |
67 | 82 | catch { |
68 | 83 | Stop-PSFFunction -Message "Something is wrong in the module configuration" -ErrorRecord $_ -Continue |
|
71 | 86 | $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server |
72 | 87 | $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
73 | 88 |
|
74 | | - } |
75 | | - |
76 | | - process { |
77 | | - |
78 | | - # Test if there are any errors |
79 | | - if (Test-PSFFunctionInterrupt) { return } |
80 | | - |
81 | 89 | $query = " |
82 | 90 | SELECT c.CloneID, |
83 | 91 | c.CloneLocation, |
|
98 | 106 |
|
99 | 107 | try { |
100 | 108 | $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 |
102 | 110 | } |
103 | 111 | catch { |
104 | 112 | Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query |
105 | 113 | } |
106 | 114 |
|
107 | 115 | # Filter host name |
108 | | - if($HostName){ |
| 116 | + if ($HostName) { |
109 | 117 | $results = $results | Where-Object {$_.HostName -in $HostName} |
110 | 118 | } |
111 | 119 |
|
112 | 120 | # Filter image id |
113 | | - if($Database){ |
| 121 | + if ($Database) { |
114 | 122 | $results = $results | Where-Object {$_.DatabaseName -in $Database} |
115 | 123 | } |
116 | 124 |
|
117 | 125 | # Filter image id |
118 | | - if($ImageID){ |
| 126 | + if ($ImageID) { |
119 | 127 | $results = $results | Where-Object {$_.ImageID -in $ImageID} |
120 | 128 | } |
121 | 129 |
|
122 | 130 | # Filter image name |
123 | | - if($ImageName){ |
| 131 | + if ($ImageName) { |
124 | 132 | $results = $results | Where-Object {$_.ImageName -in $ImageName} |
125 | 133 | } |
126 | 134 |
|
127 | 135 | # Filter image location |
128 | | - if($ImageLocation){ |
| 136 | + if ($ImageLocation) { |
129 | 137 | $results = $results | Where-Object {$_.ImageLocation -in $ImageLocation} |
130 | 138 | } |
131 | 139 |
|
| 140 | + } |
| 141 | + |
| 142 | + process { |
| 143 | + |
| 144 | + # Test if there are any errors |
| 145 | + if (Test-PSFFunctionInterrupt) { return } |
| 146 | + |
132 | 147 | # 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 | + } |
148 | 162 | } |
149 | 163 |
|
150 | 164 | } |
|
0 commit comments