DSCResources/DSC_OpticalDiskDriveLetter/DSC_OpticalDiskDriveLetter.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules' # Import the Storage Common Module. Import-Module -Name (Join-Path -Path $modulePath ` -ChildPath (Join-Path -Path 'StorageDsc.Common' ` -ChildPath 'StorageDsc.Common.psm1')) Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common') # Import Localization Strings. $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' <# .SYNOPSIS This helper function returns a hashtable containing the current drive letter assigned to the optical disk in the system matching the disk number. If the drive exists but is not mounted to a drive letter then the DriveLetter will be empty, but the DeviceId will contain the DeviceId representing the optical disk. If there are no optical disks found in the system an exception will be thrown. .PARAMETER DiskId Specifies the optical disk number for the disk to return the drive letter of. .NOTES The Caption and DeviceID properties are checked to avoid mounted ISO images in Windows 2012+ and Windows 10. The device ID is required because a CD/DVD in a Hyper-V virtual machine has the same caption as a mounted ISO. Example DeviceID for a virtual drive in a Hyper-V VM - SCSI\CDROM&VEN_MSFT&PROD_VIRTUAL_DVD-ROM\000006 Example DeviceID for a mounted ISO in a Hyper-V VM - SCSI\CDROM&VEN_MSFT&PROD_VIRTUAL_DVD-ROM\2&1F4ADFFE&0&000002 #> function Get-OpticalDiskDriveLetter { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [System.String] $DiskId ) $driveLetter = $null Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.UsingGetCimInstanceToFetchDriveLetter -f $DiskId) ) -join '' ) # Get the optical disk matching the Id $opticalDisks = Get-CimInstance -ClassName Win32_CDROMDrive | Where-Object -FilterScript { -not ( $_.Caption -eq 'Microsoft Virtual DVD-ROM' -and ($_.DeviceID.Split('\')[-1]).Length -gt 10 ) } if ($opticalDisks) { <# To behave in a similar fashion to the other xStorage resources the DiskId represents the number of the optical disk in the system. However as these are returned as an array of 0..x elements then subtract one from the DiskId to get the actual optical disk number that is required. #> $opticalDisk = $opticalDisks[$DiskId - 1] if ($opticalDisk) { try { # Make sure the current DriveLetter is an actual drive letter $driveLetter = Assert-DriveLetterValid -DriveLetter $opticalDisk.Drive -Colon } catch { # Optical drive exists but is not mounted to a drive letter $driveLetter = '' Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.OpticalDiskNotAssignedDriveLetter -f $DiskId) ) -join '' ) } $deviceId = $opticalDisk.Drive } } if ([System.String]::IsNullOrEmpty($deviceId)) { # The requested optical drive does not exist in the system Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.OpticalDiskDriveDoesNotExist -f $DiskId) ) -join '' ) } return @{ DriveLetter = $driveLetter DeviceId = $deviceId } } <# .SYNOPSIS Returns the current drive letter assigned to the optical disk. .PARAMETER DiskId Specifies the optical disk number for the disk to assign the drive letter to. .PARAMETER DriveLetter Specifies the drive letter to assign to the optical disk. Can be a single letter, optionally followed by a colon. This value is ignored if Ensure is set to Absent. #> function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [System.String] $DiskId, [Parameter(Mandatory = $true)] [System.String] $DriveLetter ) $ensure = 'Absent' # Get the drive letter assigned to the optical disk $currentDriveInfo = Get-OpticalDiskDriveLetter -DiskId $DiskId if ([System.String]::IsNullOrEmpty($currentDriveInfo.DeviceId)) { $currentDriveLetter = '' } else { $currentDriveLetter = $currentDriveInfo.DriveLetter if ([System.String]::IsNullOrWhiteSpace($currentDriveLetter)) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.OpticalDiskNotAssignedDriveLetter -f $DiskId) ) -join '' ) } else { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.OpticalDiskAssignedDriveLetter -f $DiskId, $DriveLetter) ) -join '' ) $ensure = 'Present' } } $returnValue = @{ DiskId = $DiskId DriveLetter = $currentDriveLetter Ensure = $ensure } return $returnValue } # Get-TargetResource <# .SYNOPSIS Sets the drive letter of an optical disk. .PARAMETER DiskId Specifies the optical disk number for the disk to assign the drive letter to. .PARAMETER DriveLetter Specifies the drive letter to assign to the optical disk. Can be a single letter, optionally followed by a colon. This value is ignored if Ensure is set to Absent. .PARAMETER Ensure Determines whether a drive letter should be assigned to the optical disk. Defaults to 'Present'. #> function Set-TargetResource { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [System.String] $DiskId, [Parameter(Mandatory = $true)] [System.String] $DriveLetter, [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present' ) # Allow use of drive letter without colon $DriveLetter = Assert-DriveLetterValid -DriveLetter $DriveLetter -Colon # Get the drive letter assigned to the optical disk $currentDriveInfo = Get-OpticalDiskDriveLetter -DiskId $DiskId $currentDriveLetter = $currentDriveInfo.DriveLetter if ([System.String]::IsNullOrWhiteSpace($currentDriveLetter)) { <# If the current drive letter is empty then the volume must be looked up by DeviceId The DeviceId in the volume will show as \\?\Volume{bba1802b-e7a1-11e3-824e-806e6f6e6963}\ So we need to change the currentDriveLetter to match this value when we set the drive letter #> $deviceId = $currentDriveInfo.DeviceId $volume = Get-CimInstance ` -ClassName Win32_Volume ` -Filter "DeviceId = '\\\\?\\$deviceId\\'" } else { $volume = Get-CimInstance ` -ClassName Win32_Volume ` -Filter "DriveLetter = '$currentDriveLetter'" } # Does the Drive Letter need to be added or removed if ($Ensure -eq 'Absent') { if (-not [System.String]::IsNullOrEmpty($currentDriveInfo.DeviceId)) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.AttemptingToRemoveDriveLetter -f $diskId, $currentDriveLetter) ) -join '' ) $volume | Set-CimInstance -Property @{ DriveLetter = $null } } } else { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.AttemptingToSetDriveLetter -f $diskId, $currentDriveLetter, $DriveLetter) ) -join '' ) $volume | Set-CimInstance -Property @{ DriveLetter = $DriveLetter } } } # Set-TargetResource <# .SYNOPSIS Tests the disk letter assigned to an optical disk is correct. .PARAMETER DiskId Specifies the optical disk number for the disk to assign the drive letter to. .PARAMETER DriveLetter Specifies the drive letter to assign to the optical disk. Can be a single letter, optionally followed by a colon. This value is ignored if Ensure is set to Absent. .PARAMETER Ensure Determines whether a drive letter should be assigned to the optical disk. Defaults to 'Present'. #> function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter(Mandatory = $true)] [System.String] $DiskId, [Parameter(Mandatory = $true)] [System.String] $DriveLetter, [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present' ) $desiredConfigurationMatch = $true # Allow use of drive letter without colon $DriveLetter = Assert-DriveLetterValid -DriveLetter $DriveLetter -Colon # Get the drive letter assigned to the optical disk $currentDriveInfo = Get-OpticalDiskDriveLetter -DiskId $DiskId $currentDriveLetter = $currentDriveInfo.DriveLetter if ($Ensure -eq 'Absent') { if (-not [System.String]::IsNullOrEmpty($currentDriveInfo.DeviceId)) { # The Drive Letter should be absent from the optical disk if ([System.String]::IsNullOrWhiteSpace($currentDriveLetter)) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DriveLetterDoesNotExistAndShouldNot -f $DiskId) ) -join '' ) } else { # The Drive Letter needs to be dismounted Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DriveLetterExistsButShouldNot -f $DiskId, $currentDriveLetter) ) -join '' ) $desiredConfigurationMatch = $false } } } else { # Throw an exception if the desired optical disk does not exist if ([System.String]::IsNullOrEmpty($currentDriveInfo.DeviceId)) { New-InvalidArgumentException ` -Message ($script:localizedData.NoOpticalDiskDriveError -f $DiskId) ` -ArgumentName 'DiskId' } if ($currentDriveLetter -eq $DriveLetter) { # The optical disk drive letter is already set correctly Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DriverLetterExistsAndIsCorrect -f $DiskId, $DriveLetter) ) -join '' ) } else { # Is a desired drive letter already assigned to a different drive? $existingVolume = Get-CimInstance ` -ClassName Win32_Volume ` -Filter "DriveLetter = '$DriveLetter'" if ($existingVolume) { # The desired drive letter is already assigned to another drive - can't proceed New-InvalidOperationException ` -Message $($script:localizedData.DriveLetterAssignedToAnotherDrive -f $DriveLetter) } else { # The optical drive letter needs to be changed Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DriverLetterExistsAndIsNotCorrect -f $DiskId, $currentDriveLetter, $DriveLetter) ) -join '' ) $desiredConfigurationMatch = $false } } } return $desiredConfigurationMatch } Export-ModuleMember -Function *-TargetResource |