Skip to content

Vhd: Configuration fails when VHD directory does not exist yet, even with dependsOn ordering #225

@trevonmckay

Description

@trevonmckay

Problem description

During initial VM provisioning with DSC v3, a VHD resource is configured to create a disk in a directory that doesn't exist yet. A preceding Script resource creates the directory, and the VHD resource is ordered after it in the configuration. However, when dsc config set runs, the VHD resource's Test-TargetResource is invoked before the directory has been created and throws "D:\Hyper-V\Virtual Hard Disks\SQLTPAR101 does not exists"

This crashes the entire configuration run, preventing any resources from being applied, including the Script resource that would create the directory.

Verbose logs

2026-03-20T22:38:12.589584Z PID 14868:
  NativeErrorCode : Failed
  ErrorData       : MSFT_WmiError
  MessageId       : ProviderOperationExecutionFailure
  Message         : PowerShell DSC resource DSC_Vhd  failed to execute Test-TargetResource functionality with error message: D:\Hyper-V\Virtual Hard Disks\SQLTPAR101 does not exists

  2026-03-20T22:38:12.591394Z ERROR PID 14868: Exception: PowerShell DSC resource DSC_Vhd failed to execute Test-TargetResource functionality with error message: D:\Hyper-V\Virtual Hard Disks\SQLTPAR101 does not exists

DSC configuration

$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
  resources:
    - name: Run classic DSC on Windows
      type: Microsoft.Windows/WindowsPowerShell
      properties:
        resources:
          - name: VM_VhdFolder
            type: PSDesiredStateConfiguration/Script
            properties:
              GetScript: |
                @{ Result = (Test-Path 'D:\Hyper-V\Virtual Hard Disks\MyVM').ToString() }
              TestScript: |
                Test-Path 'D:\Hyper-V\Virtual Hard Disks\MyVM'
              SetScript: |
                New-Item -Path 'D:\Hyper-V\Virtual Hard Disks\MyVM' -ItemType Directory -Force

          - name: VM_Disk_OS
            type: HyperVDsc/VHD
            properties:
              Name: MyVM_OS.vhdx
              Path: 'D:\Hyper-V\Virtual Hard Disks\MyVM'
              Generation: Vhdx
              Type: Dynamic
              MaximumSizeBytes: 137438953472
              Ensure: Present
            dependsOn:
              - PSDesiredStateConfiguration/Script::VM_VhdFolder

Suggested solution

Replace the throws with return $false

Per Microsoft's DSC resource authoring guidelines:

In your implementation of Test-TargetResource, verify the current state of the system against the values specified in the parameter set. If the current state doesn't match the desired state, return $false. Otherwise, return $true.

A missing directory means the VHD cannot be in desired state. The desired response is $false, not an exception, so that execution can continue.

Operating system the target node is running

Windows Server 2022 Datacenter
Version: 10.0.26100.0

PowerShell version and build the target node is running

PSVersion: 5.1.26100.32522

HyperVDsc version

Name       Version Path
----       ------- ----
HyperVDsc  4.0.0   C:\Program Files\WindowsPowerShell\Modules\HyperVDsc\4.0.0\HyperVDsc.psd1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions