|
1 | 1 | function Get-PSDCClone { |
2 | | -<# |
| 2 | + <# |
3 | 3 | .SYNOPSIS |
4 | 4 | Get-PSDCClone get on or more clones |
5 | 5 |
|
6 | 6 | .DESCRIPTION |
7 | 7 | Get-PSDCClone will retrieve the clones and apply filters if needed. |
8 | 8 | By default all the clones are returned |
9 | 9 |
|
| 10 | +.PARAMETER SqlCredential |
| 11 | + Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use: |
| 12 | +
|
| 13 | + $scred = Get-Credential, then pass $scred object to the -SqlCredential parameter. |
| 14 | +
|
| 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. |
| 17 | +
|
10 | 18 | .PARAMETER HostName |
11 | 19 | Filter based on the hostname |
12 | 20 |
|
|
51 | 59 | [CmdLetBinding()] |
52 | 60 |
|
53 | 61 | param( |
| 62 | + [System.Management.Automation.PSCredential]$SqlCredential, |
54 | 63 | [string[]]$HostName, |
55 | 64 | [string[]]$Database, |
56 | 65 | [int[]]$ImageID, |
|
71 | 80 | $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server |
72 | 81 | $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
73 | 82 |
|
74 | | - } |
75 | | - |
76 | | - process { |
77 | | - |
78 | | - # Test if there are any errors |
79 | | - if (Test-PSFFunctionInterrupt) { return } |
80 | | - |
81 | 83 | $query = " |
82 | 84 | SELECT c.CloneID, |
83 | 85 | c.CloneLocation, |
|
98 | 100 |
|
99 | 101 | try { |
100 | 102 | $results = @() |
101 | | - $results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -As PSObject |
| 103 | + $results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -As PSObject |
102 | 104 | } |
103 | 105 | catch { |
104 | 106 | Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query |
105 | 107 | } |
106 | 108 |
|
107 | 109 | # Filter host name |
108 | | - if($HostName){ |
| 110 | + if ($HostName) { |
109 | 111 | $results = $results | Where-Object {$_.HostName -in $HostName} |
110 | 112 | } |
111 | 113 |
|
112 | 114 | # Filter image id |
113 | | - if($Database){ |
| 115 | + if ($Database) { |
114 | 116 | $results = $results | Where-Object {$_.DatabaseName -in $Database} |
115 | 117 | } |
116 | 118 |
|
117 | 119 | # Filter image id |
118 | | - if($ImageID){ |
| 120 | + if ($ImageID) { |
119 | 121 | $results = $results | Where-Object {$_.ImageID -in $ImageID} |
120 | 122 | } |
121 | 123 |
|
122 | 124 | # Filter image name |
123 | | - if($ImageName){ |
| 125 | + if ($ImageName) { |
124 | 126 | $results = $results | Where-Object {$_.ImageName -in $ImageName} |
125 | 127 | } |
126 | 128 |
|
127 | 129 | # Filter image location |
128 | | - if($ImageLocation){ |
| 130 | + if ($ImageLocation) { |
129 | 131 | $results = $results | Where-Object {$_.ImageLocation -in $ImageLocation} |
130 | 132 | } |
131 | 133 |
|
| 134 | + } |
| 135 | + |
| 136 | + process { |
| 137 | + |
| 138 | + # Test if there are any errors |
| 139 | + if (Test-PSFFunctionInterrupt) { return } |
| 140 | + |
132 | 141 | # 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 |
| 142 | + foreach ($result in $results) { |
| 143 | + |
| 144 | + [pscustomobject]@{ |
| 145 | + CloneID = $result.CloneID |
| 146 | + CloneLocation = $result.CloneLocation |
| 147 | + AccessPath = $result.AccessPath |
| 148 | + SqlInstance = $result.SqlInstance |
| 149 | + DatabaseName = $result.IsEnabled |
| 150 | + IsEnabled = $result.IsEnabled |
| 151 | + ImageID = $result.ImageID |
| 152 | + ImageName = $result.ImageName |
| 153 | + ImageLocation = $result.ImageLocation |
| 154 | + HostName = $result.HostName |
| 155 | + } |
148 | 156 | } |
149 | 157 |
|
150 | 158 | } |
|
0 commit comments