Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpticalDiskDriveLetter: Fix finding drive on Azure Windows Server 2022 #275

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Added support for changing optical disk drive letter on Windows Server 2022 - Fixes [Issue #274](https://github.com/dsccommunity/StorageDsc/issues/274)

## [5.1.0] - 2023-02-22

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ 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 the expected max length of the Win32_CDROMDrive
DeviceId, based on the BuildId of the operating system.

.NOTES
Pre Windows Server 2022 the expected max length is 10,
from Windows Server 2022 onwards this is 20.
#>
function Get-OpticalDeviceIdMaxLength
{
[CmdletBinding()]
[OutputType([System.Int32])]
param ()

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.UsingGetCimInstanceToFetchDeviceIdMaxLength -f $DiskId)
) -join '' )

if ((Get-CimInstance -ClassName WIN32_OperatingSystem).BuildNumber -ge 20348)
{
$Length = 20
}
else
{
$Length = 10
}
return $Length
}

<#
.SYNOPSIS
This helper function returns a hashtable containing the current
Expand Down Expand Up @@ -59,7 +90,7 @@ function Get-OpticalDiskDriveLetter
Where-Object -FilterScript {
-not (
$_.Caption -eq 'Microsoft Virtual DVD-ROM' -and
($_.DeviceID.Split('\')[-1]).Length -gt 10
($_.DeviceID.Split('\')[-1]).Length -gt (Get-OpticalDeviceIdMaxLength)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ConvertFrom-StringData @'
UsingGetCimInstanceToFetchDeviceIdMaxLength = Using Get-CimInstance to get the expected max length of optical disk {0}'s DeviceId.
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.
Expand Down
65 changes: 65 additions & 0 deletions tests/Unit/DSC_OpticalDiskDriveLetter.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ try
Id = 'W:'
}

$script:mockedOSPreServer2022 = [pscustomobject] @{
BuildNumber = 17763
}

$script:mockedOSServer2022 = [pscustomobject] @{
BuildNumber = 20348
}

$script:opticalDeviceIdMaxLengthPreServer2022 = 10
$script:opticalDeviceIdMaxLengthServer2022 = 20

function Set-CimInstance
{
Param
Expand All @@ -106,6 +117,60 @@ try
)
}

Describe 'DSC_xOpticalDiskDriveLetter\Get-OpticalDeviceIdMaxLength' {
Context 'When OS is pre Windows Server 2022' {
Mock `
-CommandName Get-CimInstance `
-ParameterFilter {
$ClassName -eq 'WIN32_OperatingSystem'
} `
-MockWith {
$script:mockedOSPreServer2022
} `
-Verifiable

It 'Should not throw an exception' {
{
$script:result = Get-OpticalDeviceIdMaxLength
} | Should -Not -Throw
}

It "Optical DeviceId max length should be $($script:opticalDeviceIdMaxLengthPreServer2022)" {
$script:result | Should -Be $script:opticalDeviceIdMaxLengthPreServer2022
}

It 'Should call all the Get mocks' {
Assert-VerifiableMock
}
}

Context 'When OS is Windows Server 2022' {
Mock `
-CommandName Get-CimInstance `
-ParameterFilter {
$ClassName -eq 'WIN32_OperatingSystem'
} `
-MockWith {
$script:mockedOSServer2022
} `
-Verifiable

It 'Should not throw an exception' {
{
$script:result = Get-OpticalDeviceIdMaxLength
} | Should -Not -Throw
}

It "Optical DeviceId max length should be $($script:opticalDeviceIdMaxLengthServer2022)" {
$script:result | Should -Be $script:opticalDeviceIdMaxLengthServer2022
}

It 'Should call all the Get mocks' {
Assert-VerifiableMock
}
}
}

Describe 'DSC_xOpticalDiskDriveLetter\Get-OpticalDiskDriveLetter' {
Context 'When a single optical disk drive is present and assigned a drive letter' {
Mock `
Expand Down