1+ function Get-PDCClone {
2+
3+ <#
4+ . SYNOPSIS
5+ Get-PDCClone get on or more clones
6+
7+ . DESCRIPTION
8+ Get-PDCClone will retrieve the clones and apply filters if needed.
9+ By default all the clones are returned
10+
11+ . PARAMETER HostName
12+ Filter based on the hostname
13+
14+ . PARAMETER Database
15+ Filter based on the database
16+
17+ . PARAMETER ImageID
18+ Filter based on the image id
19+
20+ . PARAMETER ImageName
21+ Filter based on the image name
22+
23+ . PARAMETER ImageLocation
24+ Filter based on the image location
25+
26+ . NOTES
27+ Author: Sander Stad (@sqlstad, sqlstad.nl)
28+
29+ Website: https://psdatabaseclone.io
30+ Copyright: (C) Sander Stad, sander@sqlstad.nl
31+ License: MIT https://opensource.org/licenses/MIT
32+
33+ . LINK
34+ https://psdatabaseclone.io/
35+
36+ . EXAMPLE
37+ Get-PDCClone -HostName host1, host2
38+
39+ Retrieve the clones for host1 and host2
40+
41+ . EXAMPLE
42+ Get-PDCClone -Database DB1
43+
44+ Get all the clones that have the name DB1
45+
46+ . EXAMPLE
47+ Get-PDCClone -ImageName DB1_20180703085917
48+
49+ Get all the clones that were made with image "DB1_20180703085917"
50+
51+ #>
52+
53+ [CmdLetBinding ()]
54+
55+ param (
56+ [string []]$HostName ,
57+ [string []]$Database ,
58+ [int []]$ImageID ,
59+ [string []]$ImageName ,
60+ [string []]$ImageLocation
61+ )
62+
63+ begin {
64+
65+ # Test the module database setup
66+ try {
67+ Test-PDCConfiguration - EnableException
68+ }
69+ catch {
70+ Stop-PSFFunction - Message " Something is wrong in the module configuration" - ErrorRecord $_ - Continue
71+ }
72+
73+ $pdcSqlInstance = Get-PSFConfigValue - FullName psdatabaseclone.database.server
74+ $pdcDatabase = Get-PSFConfigValue - FullName psdatabaseclone.database.name
75+
76+ }
77+
78+ process {
79+
80+ # Test if there are any errors
81+ if (Test-PSFFunctionInterrupt ) { return }
82+
83+ $query = "
84+ SELECT c.CloneID,
85+ c.CloneLocation,
86+ c.AccessPath,
87+ c.SqlInstance,
88+ c.DatabaseName,
89+ c.IsEnabled,
90+ i.ImageID,
91+ i.ImageName,
92+ i.ImageLocation,
93+ h.HostName
94+ FROM dbo.Clone AS c
95+ INNER JOIN dbo.Host AS h
96+ ON h.HostID = c.HostID
97+ INNER JOIN dbo.Image AS i
98+ ON i.ImageID = c.ImageID;
99+ "
100+
101+ try {
102+ $results = Invoke-DbaSqlQuery - SqlInstance $pdcSqlInstance - Database $pdcDatabase - Query $query -As PSObject
103+ }
104+ catch {
105+ Stop-PSFFunction - Message " Could not execute query" - ErrorRecord $_ - Target $query
106+ }
107+
108+ # Filter host name
109+ if ($HostName ){
110+ $results = $results | Where-Object {$_.HostName -in $HostName }
111+ }
112+
113+ # Filter image id
114+ if ($Database ){
115+ $results = $results | Where-Object {$_.DatabaseName -in $Database }
116+ }
117+
118+ # Filter image id
119+ if ($ImageID ){
120+ $results = $results | Where-Object {$_.ImageID -in $ImageID }
121+ }
122+
123+ # Filter image name
124+ if ($ImageName ){
125+ $results = $results | Where-Object {$_.ImageName -in $ImageName }
126+ }
127+
128+ # Filter image location
129+ if ($ImageLocation ){
130+ $results = $results | Where-Object {$_.ImageLocation -in $ImageLocation }
131+ }
132+
133+ return $results
134+ }
135+
136+ end {
137+
138+ # Test if there are any errors
139+ if (Test-PSFFunctionInterrupt ) { return }
140+
141+ Write-PSFMessage - Message " Finished retrieving clone(s)" - Level Verbose
142+
143+ }
144+
145+ }
0 commit comments