Azure VM 创建中用于驱动器号覆盖的自定义脚本扩展

Azure VM 创建中用于驱动器号覆盖的自定义脚本扩展

我可以在虚拟机中运行以下脚本而不会出现任何错误

gwmi win32_pagefilesetting
$pf=gwmi win32_pagefilesetting
$pf.Delete()
Restart-Computer –Force
Get-Partition -DriveLetter "D" | Set-Partition -NewDriveLetter "T"
$TempDriveLetter = "T"
$TempDriveLetter = $TempDriveLetter + ":"
$drive = Get-WmiObject -Class win32_volume -Filter “DriveLetter = '$TempDriveLetter'”
#re-enable page file on new Drive
$drive = Get-WmiObject -Class win32_volume -Filter “DriveLetter = '$TempDriveLetter'”
Set-WMIInstance -Class Win32_PageFileSetting -Arguments @{ Name = "$TempDriveLetter\pagefile.sys"; MaximumSize = 0; }
Restart-Computer -Force

当我尝试通过自定义脚本扩展运行该脚本时,出现以下错误: 错误截图

脚本参考:https://stackoverflow.com/questions/58015765/drive-letter-override-in-azure-vm-creation

答案1

您可以使用 PowerShell DSC 扩展来实现这一点。以下 DSC 代码将为第一个数据磁盘分配 T: 字母,设置页面文件并重新启动服务器。

Configuration SetPageFile {

Import-DscResource -ModuleName ComputerManagementDsc
Import-DscResource -ModuleName StorageDSC

Node localhost {

    LocalConfigurationManager {
        RebootNodeIfNeeded = $true
    }

    WaitforDisk Disk2
    {
        DiskId           = 2
        RetryIntervalSec = 30
        RetryCount       = 3
    }

    Disk Disk2 {
        DiskId      = 2
        DriveLetter = 'T'
        DependsOn   = '[WaitForDisk]Disk2'
    }

    VirtualMemory PagingSettings
    {
        Type        = 'CustomSize'
        Drive       = 'T'
        InitialSize = '2048'
        MaximumSize = '2048'
        DependsOn   = '[Disk]Disk2'
    }
}

}

将扩展添加到虚拟机并指定以下设置

在此处输入图片描述

您可以找到 zip 文件在我的 GitHub 仓库中. 将其上传到存储帐户,然后在 VM 扩展配置中单击“浏览”时选择它。

相关内容