Skip to content

Commit 30f8a83

Browse files
committed
Added sql cred parameter. Moved code to begin block. Changed return data type to object
1 parent 2eff861 commit 30f8a83

1 file changed

Lines changed: 30 additions & 21 deletions

File tree

functions/Get-PSDCImage.ps1

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
function Get-PSDCImage {
2-
<#
2+
<#
33
.SYNOPSIS
44
Get-PSDCImage get on or more clones
55
66
.DESCRIPTION
77
Get-PSDCImage will retrieve the clones and apply filters if needed.
88
By default all the clones are returned
99
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+
1018
.PARAMETER ImageID
1119
Filter based on the image id
1220
@@ -48,6 +56,7 @@
4856
[CmdLetBinding()]
4957

5058
param(
59+
[System.Management.Automation.PSCredential]$SqlCredential,
5160
[int[]]$ImageID,
5261
[string[]]$ImageName,
5362
[string[]]$ImageLocation,
@@ -65,11 +74,6 @@
6574

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

7478
$query = "
7579
SELECT ImageID,
@@ -83,11 +87,11 @@
8387
"
8488

8589
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
8892
}
8993
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
9195
}
9296

9397
# Filter image id
@@ -109,22 +113,27 @@
109113
if ($Database) {
110114
$results = $results | Where-Object {$_.DatabaseName -in $Database}
111115
}
116+
}
112117

118+
process {
119+
# Test if there are any errors
120+
if (Test-PSFFunctionInterrupt) { return }
113121

114122
# 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+
127135
}
136+
128137
}
129138

130139
end {

0 commit comments

Comments
 (0)