Skip to content

Commit 2eff861

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

1 file changed

Lines changed: 37 additions & 29 deletions

File tree

functions/Get-PSDCClone.ps1

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
function Get-PSDCClone {
2-
<#
2+
<#
33
.SYNOPSIS
44
Get-PSDCClone get on or more clones
55
66
.DESCRIPTION
77
Get-PSDCClone 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 HostName
1119
Filter based on the hostname
1220
@@ -51,6 +59,7 @@
5159
[CmdLetBinding()]
5260

5361
param(
62+
[System.Management.Automation.PSCredential]$SqlCredential,
5463
[string[]]$HostName,
5564
[string[]]$Database,
5665
[int[]]$ImageID,
@@ -71,13 +80,6 @@
7180
$pdcSqlInstance = Get-PSFConfigValue -FullName psdatabaseclone.database.server
7281
$pdcDatabase = Get-PSFConfigValue -FullName psdatabaseclone.database.name
7382

74-
}
75-
76-
process {
77-
78-
# Test if there are any errors
79-
if (Test-PSFFunctionInterrupt) { return }
80-
8183
$query = "
8284
SELECT c.CloneID,
8385
c.CloneLocation,
@@ -98,53 +100,59 @@
98100

99101
try {
100102
$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
102104
}
103105
catch {
104106
Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query
105107
}
106108

107109
# Filter host name
108-
if($HostName){
110+
if ($HostName) {
109111
$results = $results | Where-Object {$_.HostName -in $HostName}
110112
}
111113

112114
# Filter image id
113-
if($Database){
115+
if ($Database) {
114116
$results = $results | Where-Object {$_.DatabaseName -in $Database}
115117
}
116118

117119
# Filter image id
118-
if($ImageID){
120+
if ($ImageID) {
119121
$results = $results | Where-Object {$_.ImageID -in $ImageID}
120122
}
121123

122124
# Filter image name
123-
if($ImageName){
125+
if ($ImageName) {
124126
$results = $results | Where-Object {$_.ImageName -in $ImageName}
125127
}
126128

127129
# Filter image location
128-
if($ImageLocation){
130+
if ($ImageLocation) {
129131
$results = $results | Where-Object {$_.ImageLocation -in $ImageLocation}
130132
}
131133

134+
}
135+
136+
process {
137+
138+
# Test if there are any errors
139+
if (Test-PSFFunctionInterrupt) { return }
140+
132141
# 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+
}
148156
}
149157

150158
}

0 commit comments

Comments
 (0)