![Azure VM 创建中用于驱动器号覆盖的自定义脚本扩展](https://linux22.com/image/783374/Azure%20VM%20%E5%88%9B%E5%BB%BA%E4%B8%AD%E7%94%A8%E4%BA%8E%E9%A9%B1%E5%8A%A8%E5%99%A8%E5%8F%B7%E8%A6%86%E7%9B%96%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E8%84%9A%E6%9C%AC%E6%89%A9%E5%B1%95.png)
我可以在虚拟机中运行以下脚本而不会出现任何错误
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 扩展配置中单击“浏览”时选择它。