Skip to content

Commit

Permalink
Merge pull request #235 from PlagueHO/Issue-194
Browse files Browse the repository at this point in the history
OpticalDiskDriveLetter: Suppress No Drive Exception - Fixes #194
  • Loading branch information
PlagueHO authored May 3, 2020
2 parents d1339bf + 2566824 commit 6b289c0
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 182 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Change Azure DevOps Pipeline definition to include `source/*` - Fixes [Issue #231](https://github.com/dsccommunity/StorageDsc/issues/231).
- Updated pipeline to use `latest` version of `ModuleBuilder` - Fixes [Issue #231](https://github.com/dsccommunity/StorageDsc/issues/231).
- Merge `HISTORIC_CHANGELOG.md` into `CHANGELOG.md` - Fixes [Issue #232](https://github.com/dsccommunity/StorageDsc/issues/232).
- OpticalDiskDriveLetter:
- Suppress exception when requested optical disk drive does not exist
and Ensure is set to `Absent` - Fixes [Issue #194](https://github.com/dsccommunity/StorageDsc/issues/194).

## [4.9.0.0] - 2019-10-30

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ function Get-OpticalDiskDriveLetter
)
}

$deviceId = ''

if ($opticalDisks)
{
<#
Expand Down Expand Up @@ -91,15 +89,18 @@ function Get-OpticalDiskDriveLetter
$($script:localizedData.OpticalDiskNotAssignedDriveLetter -f $DiskId)
) -join '' )
}

$deviceId = $opticalDisk.Drive
}
}

if ($null -eq $driveLetter)
if ([System.String]::IsNullOrEmpty($deviceId))
{
New-InvalidArgumentException `
-Message ($script:localizedData.NoOpticalDiskDriveError -f $DiskId) `
-ArgumentName 'DiskId'
# The requested optical drive does not exist in the system
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.OpticalDiskDriveDoesNotExist -f $DiskId)
) -join '' )
}

return @{
Expand Down Expand Up @@ -140,26 +141,34 @@ function Get-TargetResource

# Get the drive letter assigned to the optical disk
$currentDriveInfo = Get-OpticalDiskDriveLetter -DiskId $DiskId
$currentDriveLetter = $currentDriveInfo.DriveLetter

if ([System.String]::IsNullOrWhiteSpace($currentDriveLetter))
if ([System.String]::IsNullOrEmpty($currentDriveInfo.DeviceId))
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.OpticalDiskNotAssignedDriveLetter -f $DiskId)
) -join '' )
$currentDriveLetter = ''
}
else
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.OpticalDiskAssignedDriveLetter -f $DiskId, $DriveLetter)
) -join '' )
$currentDriveLetter = $currentDriveInfo.DriveLetter

$Ensure = 'Present'
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 += @{
$returnValue = @{
DiskId = $DiskId
DriveLetter = $currentDriveLetter
Ensure = $ensure
Expand Down Expand Up @@ -234,13 +243,16 @@ function Set-TargetResource
# Does the Drive Letter need to be added or removed
if ($Ensure -eq 'Absent')
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.AttemptingToRemoveDriveLetter -f $diskId, $currentDriveLetter)
) -join '' )
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
$volume | Set-CimInstance -Property @{
DriveLetter = $null
}
}
}
else
Expand Down Expand Up @@ -304,27 +316,38 @@ function Test-TargetResource

if ($Ensure -eq 'Absent')
{
# The Drive Letter should be absent from the optical disk
if ([System.String]::IsNullOrWhiteSpace($currentDriveLetter))
if (-not [System.String]::IsNullOrEmpty($currentDriveInfo.DeviceId))
{
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 '' )
# 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
$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ConvertFrom-StringData @'
UsingGetCimInstanceToFetchDriveLetter = Using Get-CimInstance to get the drive letter of optical disk {0} in the system.
OpticalDiskAssignedDriveLetter = The optical disk {0} is currently assigned drive letter '{1}'.
OpticalDiskNotAssignedDriveLetter = The optical disk {0} is not currently assigned a drive letter.
OpticalDiskDriveDoesNotExist = The optical disk {0} could not be found in the system.
NoOpticalDiskDriveError = The optical disk {0} could not be found in the system, so this resource has nothing to do. This resource does not change the drive letter of mounted ISOs.
AttemptingToSetDriveLetter = The optical disk {0} drive letter is '{1}', attempting to set to '{2}'.
Expand Down
43 changes: 43 additions & 0 deletions tests/Integration/DSC_OpticalDiskDriveLetter.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\Co

try
{
# Locate an optical disk in the system to use for testing
$opticalDisk = Get-CimInstance -ClassName Win32_CDROMDrive |
Where-Object -FilterScript {
-not (
Expand Down Expand Up @@ -175,6 +176,48 @@ try
$current.DriveLetter | Should -Be ''
}
}

Context 'Assign a Drive Letter to an optical drive that does not exist' {
It 'Should compile and apply the MOF without throwing' {
{
# This is to pass to the Config
$configData = @{
AllNodes = @(
@{
NodeName = 'localhost'
DiskId = 2
DriveLetter = $driveLetter
Ensure = 'Present'
}
)
}

& "$($script:dscResourceName)_Config" `
-OutputPath $TestDrive `
-ConfigurationData $configData

Start-DscConfiguration `
-Path $TestDrive `
-ComputerName localhost `
-Wait `
-Verbose `
-Force `
-ErrorAction Stop
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$current = Get-DscConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq "$($script:dscResourceName)_Config"
}
$current.DiskId | Should -Be 2
$current.DriveLetter | Should -BeNullOrEmpty
}
}
}
}
finally
Expand Down
Loading

0 comments on commit 6b289c0

Please sign in to comment.