|
1 | 1 | function Get-PSDCImage { |
2 | | - <# |
| 2 | +<# |
3 | 3 | .SYNOPSIS |
4 | 4 | Get-PSDCImage get on or more clones |
5 | 5 |
|
6 | 6 | .DESCRIPTION |
7 | 7 | Get-PSDCImage 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 ImageID |
11 | 19 | Filter based on the image id |
12 | 20 |
|
|
48 | 56 | [CmdLetBinding()] |
49 | 57 |
|
50 | 58 | param( |
| 59 | + [System.Management.Automation.PSCredential]$SqlCredential, |
51 | 60 | [int[]]$ImageID, |
52 | 61 | [string[]]$ImageName, |
53 | 62 | [string[]]$ImageLocation, |
|
65 | 74 |
|
66 | 75 | $pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server |
67 | 76 | $pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name |
68 | | - } |
69 | | - |
70 | | - process { |
71 | | - # Test if there are any errors |
72 | | - if (Test-PSFFunctionInterrupt) { return } |
73 | 77 |
|
74 | 78 | $query = " |
75 | 79 | SELECT ImageID, |
|
83 | 87 | " |
84 | 88 |
|
85 | 89 | try { |
86 | | - $result = @() |
87 | | - $results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -As PSObject |
| 90 | + $results = @() |
| 91 | + $results += Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -SqlCredential $SqlCredential -Database $pdcDatabase -Query $query -As PSObject |
88 | 92 | } |
89 | 93 | catch { |
90 | | - Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query |
| 94 | + Stop-PSFFunction -Message "Could retrieve images from database $pdcDatabase" -ErrorRecord $_ -Target $query |
91 | 95 | } |
92 | 96 |
|
93 | 97 | # Filter image id |
|
109 | 113 | if ($Database) { |
110 | 114 | $results = $results | Where-Object {$_.DatabaseName -in $Database} |
111 | 115 | } |
| 116 | + } |
112 | 117 |
|
| 118 | + process { |
| 119 | + # Test if there are any errors |
| 120 | + if (Test-PSFFunctionInterrupt) { return } |
113 | 121 |
|
114 | 122 | # 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 |
| 123 | + foreach ($result in $results) { |
| 124 | + |
| 125 | + [pscustomobject]@{ |
| 126 | + ImageID = $result.ImageID |
| 127 | + ImageName = $result.ImageName |
| 128 | + ImageLocation = $result.ImageLocation |
| 129 | + SizeMB = $result.SizeMB |
| 130 | + DatabaseName = $result.DatabaseName |
| 131 | + DatabaseTimestamp = $result.DatabaseTimestamp |
| 132 | + CreatedOn = $result.CreatedOn |
| 133 | + } |
| 134 | + |
127 | 135 | } |
| 136 | + |
128 | 137 | } |
129 | 138 |
|
130 | 139 | end { |
|
0 commit comments