38 lines
1.8 KiB
PowerShell
38 lines
1.8 KiB
PowerShell
[CmdletBinding()]
|
||
param(
|
||
[string]$VmName = "tplus-sql2008r2-test",
|
||
[string]$Root = "D:\work\app\tplus-vm",
|
||
[string]$WindowsIso = ""
|
||
)
|
||
Set-StrictMode -Version Latest
|
||
$ErrorActionPreference = "Stop"
|
||
$rootPath = [IO.Path]::GetFullPath($Root)
|
||
if (-not $rootPath.StartsWith("D:\work\app\", [StringComparison]::OrdinalIgnoreCase)) {
|
||
throw "虚拟机目录必须位于 D:\work\app 下。"
|
||
}
|
||
if ($WindowsIso -and -not (Test-Path -LiteralPath $WindowsIso -PathType Leaf)) {
|
||
throw "Windows 安装介质不存在。"
|
||
}
|
||
if (Get-VM -Name $VmName -ErrorAction SilentlyContinue) {
|
||
throw "同名虚拟机已存在,拒绝覆盖。"
|
||
}
|
||
if ((Get-PSDrive D).Free -lt 40GB) { throw "D 盘剩余不足 40GB,暂不创建测试环境。" }
|
||
New-Item -ItemType Directory -Path $rootPath -Force | Out-Null
|
||
$disk = Join-Path $rootPath "sqlserver2008r2.vhdx"
|
||
if (Test-Path -LiteralPath $disk) { throw "测试磁盘已存在,拒绝覆盖。" }
|
||
New-VHD -Path $disk -SizeBytes 64GB -Dynamic | Out-Null
|
||
$vm = New-VM -Name $VmName -Generation 1 -MemoryStartupBytes 4GB `
|
||
-VHDPath $disk -Path $rootPath
|
||
Set-VM -VM $vm -ProcessorCount 2 -AutomaticStartAction Nothing `
|
||
-AutomaticStopAction ShutDown -CheckpointType Disabled `
|
||
-SnapshotFileLocation $rootPath -SmartPagingFilePath $rootPath
|
||
# 来宾系统尚未安装,先断开网卡,不改 Docker/WSL 的交换机。
|
||
Get-VMNetworkAdapter -VM $vm | Disconnect-VMNetworkAdapter
|
||
if ($WindowsIso) {
|
||
Add-VMDvdDrive -VM $vm -Path $WindowsIso | Out-Null
|
||
}
|
||
Get-VM -Name $VmName | Select-Object Name, State, Generation, Path
|
||
Get-VMHardDiskDrive -VMName $VmName | Select-Object Path
|
||
Write-Host "仅创建了未启动的虚拟机和动态磁盘,未安装 Windows 或 SQL Server。"
|
||
Write-Host "需授权 Windows x64 和 SQL Server 2008 R2 安装介质,不能用 Express 承载此大账套。"
|