|
| 1 | +function Get-PSDCImage { |
| 2 | + <# |
| 3 | +.SYNOPSIS |
| 4 | + Get-PSDCImage get on or more clones |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | + Get-PSDCImage 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-PSDCImage -ImageName DB1_20180704220944, DB2_20180704221144 |
| 34 | +
|
| 35 | + Retrieve the images for DB1_20180704220944, DB2_20180704221144 |
| 36 | +
|
| 37 | +.EXAMPLE |
| 38 | + Get-PSDCImage -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-PSDCImage -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-PSDCConfiguration -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 | + $result = @() |
| 87 | + $results = Invoke-DbaSqlQuery -SqlInstance $pdcSqlInstance -Database $pdcDatabase -Query $query -As PSObject |
| 88 | + } |
| 89 | + catch { |
| 90 | + Stop-PSFFunction -Message "Could not execute query" -ErrorRecord $_ -Target $query |
| 91 | + } |
| 92 | + |
| 93 | + # Filter image id |
| 94 | + if ($ImageID) { |
| 95 | + $results = $results | Where-Object {$_.ImageID -in $ImageID} |
| 96 | + } |
| 97 | + |
| 98 | + # Filter image name |
| 99 | + if ($ImageName) { |
| 100 | + $results = $results | Where-Object {$_.ImageName -in $ImageName} |
| 101 | + } |
| 102 | + |
| 103 | + # Filter image location |
| 104 | + if ($ImageLocation) { |
| 105 | + $results = $results | Where-Object {$_.ImageLocation -in $ImageLocation} |
| 106 | + } |
| 107 | + |
| 108 | + # Filter database |
| 109 | + if ($Database) { |
| 110 | + $results = $results | Where-Object {$_.DatabaseName -in $Database} |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + # 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 |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + end { |
| 131 | + # Test if there are any errors |
| 132 | + if (Test-PSFFunctionInterrupt) { return } |
| 133 | + |
| 134 | + Write-PSFMessage -Message "Finished retrieving image(s)" -Level Verbose |
| 135 | + } |
| 136 | +} |
0 commit comments