HyperVDsc icon indicating copy to clipboard operation
HyperVDsc copied to clipboard

vhdFile not copying from host to vhdx file

Open mvthul opened this issue 2 years ago • 0 comments

Problem description

Seems that files are not copied to vhdx I got a vhdx with windows server already installed on it.

When i run the DSC configuration seems okee but files are not found on the VHDX

Resources to be copied are located on Hyper v host C:\DSCConfigs
Need to be copied inside VHDX C:\Temp

Verbose logs

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' =
MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer HOST01 with user sid S-1-5-21-3317971597-1648635444-1937067796-500.
VERBOSE: [HOST01]: LCM:  [ Start  Set      ]
VERBOSE: [HOST01]: LCM:  [ Start  Resource ]  [[VhdFile]FileCopy]
VERBOSE: [HOST01]: LCM:  [ Start  Test     ]  [[VhdFile]FileCopy]
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Alias
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] C
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Cert
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] D
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Env
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Function
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] HKCU
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] HKLM
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Variable
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] WSMan
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] :\
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] SourcePath => C:\DSCConfigs\*
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Type => Directory
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Ensure => False
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Content =>
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] DestinationPath => C:\Temp
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Force => True
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Attributes =>
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Recurse => True
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Testing the file with relative VHD destination C:\Temp
VERBOSE: [HOST01]:                            [[VhdFile]FileCopy] Test returned True
VERBOSE: [HOST01]: LCM:  [ End    Test     ]  [[VhdFile]FileCopy]  in 0.8860 seconds.
VERBOSE: [HOST01]: LCM:  [ Skip   Set      ]  [[VhdFile]FileCopy]
VERBOSE: [HOST01]: LCM:  [ End    Resource ]  [[VhdFile]FileCopy]
VERBOSE: [HOST01]: LCM:  [ End    Set      ]
VERBOSE: [HOST01]: LCM:  [ End    Set      ]    in  1.0770 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 1.107 seconds

DSC configuration

<#
    .DESCRIPTION
        Not yet written.
#>
Configuration Example
{
    param
    (
        [Parameter()]
        $vhdPath = 'D:\VMDisks\VM-DC01-Disk.vhdx',

        [Parameter()]
        $itemToCopy = 'C:\DSCConfigs\*',

        [Parameter()]
        $relativeDestinationPath = 'C:\Temp'
    )

    Import-DscResource -ModuleName 'HyperVDsc'

    VhdFile FileCopy
    {
        VhdPath       = $vhdPath
        FileDirectory = DSC_FileDirectory
        {
            SourcePath      = $itemToCopy
            DestinationPath = $relativeDestinationPath
            Force = $true
            Type = 'Directory'
            Recurse = $true
        }
    }
}

Example

Suggested solution

Working copy to vhdx to function to eventualy do:

`Configuration VM-DC01 { param ( [Parameter()] $VirtualHardDiskPath = 'D:\VMDisks\disk.vhdx',

    [Parameter()]
    $VirtualMachinePath = 'D:\VMs',

    [Parameter()]
    $VirtualMachineTemplate = 'D:\ISO\template.vhdx',
    
    [Parameter()]
    $Name = 'VM-DC01-Disk'
)

Import-DscResource -ModuleName xPendingReboot
Import-DscResource -ModuleName PSDesiredStateConfiguration
Import-DscResource -ModuleName ComputerManagementDsc 
Import-DscResource -ModuleName CertificateDsc 
Import-DscResource -ModuleName NetworkingDsc 
Import-DscResource -ModuleName HyperVDsc
Import-DscResource -ModuleName xHyper-V

Node localhost
{
    # Configure LCM to allow Windows to automatically reboot if needed. Note: NOT recommended for production!
    LocalConfigurationManager
    {
        # Set this to $true to automatically reboot the node after a configuration that requires reboot is applied. Otherwise, you will have to manually reboot the node for any configuration that requires it. The default (recommended for PRODUCTION servers) value is $false.
        RebootNodeIfNeeded = $true
        # The thumbprint of a certificate used to secure credentials passed in a configuration.
        CertificateId = $node.Thumbprint
    }

    # Install Windows Feature "Hyper-V Services".
    WindowsFeature HyperV
    {
        Ensure = 'Present'
        Name   = 'Hyper-V'
    }

    WindowsFeature HyperVPowerShell
    {
        Ensure = 'Present'
        Name   = 'Hyper-V-PowerShell'
    }

    # Dynamically build the 'DependsOn' array for the 'VMHyperV' feature
    # based on the number of virtual switches specified
    $VMHyperVDependsOn = @('[WindowsFeature]HyperV','[WindowsFeature]HyperVPowerShell')
    
    # Create a switch to be used by the VM
    VMSwitch switch
    {
        Name   = 'Test-Switch'
        Ensure = 'Present'
        Type   = 'Internal'
    }

    # Create new VHD file.
    File NewVHD1
    {
       SourcePath = $VirtualMachineTemplate
       DestinationPath = Join-path (Split-Path $VirtualHardDiskPath) "$name.vhdx"
       Type = 'File'
       Ensure = 'Present'
    }

    #Vhd NewVHD1
    #{
    #    Ensure     = 'Present'
    #    Name       = $name
    #    Path       = (Split-Path $VirtualHardDiskPath)
    #    Generation = 'Vhdx'
    #    ParentPath = $VirtualHardDiskPath
    #}

    # Customize VHD by copying a folders/files to the VHD before a VM can be created
    # Example below shows copying unattended.xml before a VM can be created
    VhdFile CopyDSCConfigPFX
    {
        VhdPath       = Join-path (Split-Path $VirtualHardDiskPath) "$name.vhdx"
        FileDirectory =  @(

            # Pending.mof
            DSC_FileDirectory {
                SourcePath = 'C:\DSCConfigs\HADC\DC01\Localhost.mof'
                DestinationPath = "Temp\Pending.mof"
                Ensure = 'Present'
            }

            # meta.mof
            DSC_FileDirectory {
                SourcePath = 'C:\DSCConfigs\HADC\DC01\localhost.meta.mof'
                DestinationPath = "Windows\System32\Configuration\MetaConfig.mof"
                Type = 'File'
                Ensure = 'Present'
            }

            # xPFX
            DSC_FileDirectory {
                SourcePath = 'C:\DSCConfigs\PFX'
                DestinationPath = "DSCConfigs\PFX" 
                type    = 'Directory'
                Recurse = $True
                Ensure = 'Present'
            }

            # xModules
            DSC_FileDirectory {
                SourcePath = 'C:\DSCConfigs\Modules'
                DestinationPath = "DSCConfigs\Modules" 
                type    = 'Directory'
                Recurse = $True
                Ensure = 'Present'
            }
          )
          DependsOn = '[File]NewVHD1'
       }

    # create the testVM out of the vhd.
    VMHyperV TestVM
    {
        Name            = "$($name)_vm"
        SwitchName      = 'Test-Switch'
        VhdPath         = Join-path (Split-Path $VirtualHardDiskPath) "$name.vhdx"
        Path            = "$VirtualMachinePath" + "\" + "$($name)_vm" 
        ProcessorCount  = 2
        MaximumMemory   = 4GB
        MinimumMemory   = 1GB
        RestartIfNeeded = $true
        DependsOn       = '[VMSwitch]switch', '[File]NewVHD1', '[VhdFile]CopyDSCConfigPFX'
        State           = 'Running'
        Generation      = '2'
        SecureBoot      = $true 

    }
       
}

}`

Operating system the target node is running

Server 2022 standalone

PowerShell version and build the target node is running

Latest

HyperVDsc version

Latest

mvthul avatar Oct 12 '23 15:10 mvthul