如何使用 Powershell DSC 和模块 xHyper-V 创建 Hyper-V VM?

如何使用 Powershell DSC 和模块 xHyper-V 创建 Hyper-V VM?

我一直在尝试使用模块从我的 Windows 10 工作站在我的 Hyper-V 2016 主机上创建虚拟机虚拟化并没有什么快乐。

我的 Hyper-V 主机称为 Lithium,我的 DSC 脚本如下。

Configuration EndToEndXHyperV_RunningVM
{
    param
    (
        [string[]]$NodeName = 'lithium',

        [Parameter(Mandatory)]
        [string]$VMName,

        [Parameter(Mandatory)] 
        [string]$StartupMemory,

        [Parameter(Mandatory)]
        [string]$MinimumMemory,

        [Parameter(Mandatory)]
        [string]$MaximumMemory,

        [Parameter(Mandatory)]
        [String]$SwitchName,

        [Parameter(Mandatory)]
        [Uint32]$ProcessorCount,

        [ValidateSet('Off','Paused','Running')]
        [String]$State = 'Off',

        [Switch]$WaitForIP
    )

    Import-DscResource –ModuleName 'PSDesiredStateConfiguration'
    Import-DscResource -module xHyper-V

    Node $NodeName
    {
        $NewSystemVHDPath = "\\lithium\VHDs-SSD\$($VMName)-System.vhdx"

        # Copy VHD File - hard coded to Windows 2016 Core Eval for now
        File SystemDisk {
            SourcePath = "\\lithium\Templates\Windows 2016 Core Template\System.vhdx"
            DestinationPath = $NewSystemVHDPath      
            Type = "File"
            Ensure = "Present"
        }

        # create the generation 2 testVM out of the vhd.
        xVMHyperV NewVM
        {
            Ensure          = 'Present'
            Name            = $VMName
            VhdPath         = $NewSystemVHDPath
            SwitchName      = $SwitchName
            State           = $State
            Path            = $Path
            Generation      = 2
            StartupMemory   = $StartupMemory
            MinimumMemory   = $MinimumMemory
            MaximumMemory   = $MaximumMemory
            ProcessorCount  = $ProcessorCount
            RestartIfNeeded = $true
            WaitForIP       = $WaitForIP 
            DependsOn       = "[File]SystemDisk"
        }
    }
}

我已授予 Machine 帐户对共享的访问权限,文件复制工作正常,但添加虚拟硬盘时出现错误。有什么想法吗?

PS C:\Repos\Infrastructure\DSC> Start-DscConfiguration -Path .\EndToEndXHyperV_RunningVM -Wait -Verbose -Force 
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namesp
aceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer COWSLIP with user sid S-1-5-21-1075642099-280362434-2919291742-1105.
VERBOSE: [LITHIUM]: LCM:  [ Start  Set      ]
VERBOSE: [LITHIUM]: LCM:  [ Start  Resource ]  [[File]SystemDisk]
VERBOSE: [LITHIUM]: LCM:  [ Start  Test     ]  [[File]SystemDisk]
VERBOSE: [LITHIUM]:                            [[File]SystemDisk] The system cannot find the file specified.
VERBOSE: [LITHIUM]:                            [[File]SystemDisk] The related file/directory is: \\lithium\VHDs-SSD\test-System.vhdx.
VERBOSE: [LITHIUM]:                            [[File]SystemDisk] Building file list from cache.
VERBOSE: [LITHIUM]: LCM:  [ End    Test     ]  [[File]SystemDisk]  in 0.0510 seconds.
VERBOSE: [LITHIUM]: LCM:  [ Start  Set      ]  [[File]SystemDisk]
VERBOSE: [LITHIUM]:                            [[File]SystemDisk] The system cannot find the file specified.
VERBOSE: [LITHIUM]:                            [[File]SystemDisk] The related file/directory is: \\lithium\VHDs-SSD\test-System.vhdx.
VERBOSE: [LITHIUM]:                            [[File]SystemDisk] Building file list from cache.
VERBOSE: [LITHIUM]:                            [[File]SystemDisk] Copying file \\lithium\Templates\Windows 2016 Core Template\System.vhdx to \\lithium\VHDs-SSD\test-Sy
stem.vhdx.
VERBOSE: [LITHIUM]: LCM:  [ End    Set      ]  [[File]SystemDisk]  in 17.2540 seconds.
VERBOSE: [LITHIUM]: LCM:  [ End    Resource ]  [[File]SystemDisk]
VERBOSE: [LITHIUM]: LCM:  [ Start  Resource ]  [[xVMHyperV]NewVM]
VERBOSE: [LITHIUM]: LCM:  [ Start  Test     ]  [[xVMHyperV]NewVM]
VERBOSE: [LITHIUM]: LCM:  [ End    Test     ]  [[xVMHyperV]NewVM]  in 1.0200 seconds.
VERBOSE: [LITHIUM]: LCM:  [ Start  Set      ]  [[xVMHyperV]NewVM]
VERBOSE: [LITHIUM]:                            [[xVMHyperV]NewVM] Checking if VM 'test' exists ...
VERBOSE: [LITHIUM]:                            [[xVMHyperV]NewVM] VM 'test' does not exist.
VERBOSE: [LITHIUM]:                            [[xVMHyperV]NewVM] Creating VM 'test' ...
Failed to add device 'Virtual Hard Disk'.
The Machine Account 'DUCK\LITHIUM$' or the user initiating the VM management operation or both do not have the required access to the file share 
'\\lithium\VHDs-SSD\test-System.vhdx'.  Please ensure that the computer machine account and the user initiating the VM management operation have full access to the 
file share as well as the file system folder backing the file share.  Error: 'General access denied error'
'test' failed to add device 'Virtual Hard Disk'. (Virtual machine ID 4DE01C38-E027-4366-B56A-85B527BB34CB)
'test': The Machine Account 'DUCK\LITHIUM$' or the user initiating the VM management operation or both do not have the required access to the file share 
'\\lithium\VHDs-SSD\test-System.vhdx'.  Please ensure that the computer machine account and the user initiating the VM management operation have full access to the 
file share as well as the file system folder backing the file share.  Error: 'General access denied error' (0x80070005). (Virtual machine ID 
4DE01C38-E027-4366-B56A-85B527BB34CB)
    + CategoryInfo          : PermissionDenied: (:) [], CimException
    + FullyQualifiedErrorId : AccessDenied,Microsoft.HyperV.PowerShell.Commands.NewVM
    + PSComputerName        : lithium

答案1

问题是我使用文件共享来引用 VHD,而我本应该使用本地路径。因此,驱动器的设置现在是:

    $NewSystemVHDPath = "E:\VHDs\$($VMName)-System.vhdx"

    # Copy VHD File - hard coded to Windows 2016 Core Eval for now
    File SystemDisk {
        SourcePath = "D:\Templates\Windows 2016 Core Template\System.vhdx"
        DestinationPath = $NewSystemVHDPath      
        Type = "File"
        Ensure = "Present"
    }

工作正常。

相关内容