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