因公司觉得将VDI 磁盘由原来的100GB 扩容至200GB,VDI 管理员可以通过Citrix 管理控制台完成容量增加工作,但windows的扩容还需要EUS 手动处理, 于是乎想到通过脚本扩容, 貌似还真行
1、调用执行bat

点击查看代码
@echo off 
pushd %~dp0
powershell.exe -command  "& {set-executionpolicy Unrestricted -Scope Process; .'.\ResizePartition.ps1' }" -windowstyle hidden 
popd

点击查看代码 v1.0

$expectPLabel="Q"   #期望扩容的分区Label
$regPath="HKLM:\SYSTEM\HardwareConfig\DiskInfo"  # 用于扩容后记录磁盘信息
$diskSzie=107374182400# 当前分区所在磁盘容量(100GB)
[System.UInt64]$expectPSize= 38GB #期望分区扩容后的大小

#判断注册表是否存放硬盘空间容量,如果有则跳过,如果没有则创建
function set_RegRecord 
{
    param($regPath="" , $expectPLabel="" ,$PSize="")

    $check_Res= Test-Path $regPath
   if(Test-Path $regPath )
        {
            if (Get-ItemProperty $regPath $expectPLabel )
                { set-ItemProperty $regPath -name $expectPLabel -value $PSize }

            else
               { New-ItemProperty $regPath -name $expectPLabel -value $PSize -propertyType dword}
        }
   else
       {
        #在注册表创建目录
        New-Item -type Directory $regPath
        New-ItemProperty -path $regPath -name $expectPLabel -value $PSize -propertyType dword
       }
}

#获取分区所在的磁盘盘符,及当前分区的大小
$PartitionInfo=Get-Partition -DriveLetter $expectPLabel |Select-Object DiskNumber,size
$diskInfo=Get-Disk -Number $PartitionInfo.DiskNumber
if($diskInfo.Size -eq $diskSzie -and $PartitionInfo.size -lt $diskInfo.Size  )
    {
    if((Get-Partition -DriveLetter $expectPLabel ).Size -lt $expectPSize )
        {
            Resize-Partition -DriveLetter $expectPLabel -Size ( $expectPSize )
            
            $PSize=(Get-Partition -DriveLetter $expectPLabel ).Size/1024/1024/1024  #获取当前分区大小

            set_RegRecord -regPath $regPath -expectPLabel $expectPLabel   -PSize $PSize
        
        }
    }
else
    {
    $PSize=(Get-Partition -DriveLetter $expectPLabel ).Size/1024/1024/1024  #获取当前分区大小
    set_RegRecord -regPath $regPath -expectPLabel $expectPLabel   -PSize $PSize
    }

点击查看代码
$expectPLabel="Q"   #期望扩容的分区Label
$regPath="HKLM:\SYSTEM\HardwareConfig\DiskInfo"  # 用于扩容后记录磁盘信息
#$diskSzie=107374182400# 当前分区所在磁盘容量(100GB)
$diskSzie=214748364800# 当前分区所在磁盘容量(200GB)

[System.UInt64]$expectPSize= 57GB #期望分区扩容后的大小

#判断注册表是否存放硬盘空间容量,如果有则跳过,如果没有则创建
function set_RegRecord 
{
    param($regPath="" , $expectPLabel="" ,$PSize="")
    $expSuc=0       #磁盘符合扩容 等1  否则等于0
    if (  $PSize -ge $expectPSize  )
        {
        $expSuc=1    
        }
    
    $check_Res= Test-Path $regPath
    if(Test-Path $regPath )
        {
            if (Get-ItemProperty $regPath $expectPLabel )
                { set-ItemProperty $regPath -name $expectPLabel -value $PSize }

            else
               { New-ItemProperty $regPath -name $expectPLabel -value $PSize -propertyType dword}
        }
   else
       {
        #在注册表创建目录
        New-Item -type Directory $regPath
        New-ItemProperty -path $regPath -name $expectPLabel -value $PSize -propertyType dword
       }
}

#获取分区所在的磁盘盘符,及当前分区的大小
$PartitionInfo=Get-Partition -DriveLetter $expectPLabel |Select-Object DiskNumber,size
$diskInfo=Get-Disk -Number $PartitionInfo.DiskNumber
if($diskInfo.Size -eq $diskSzie -and $PartitionInfo.size -lt $diskInfo.Size  )
    {
    if((Get-Partition -DriveLetter $expectPLabel ).Size -lt $expectPSize )
        {
            Resize-Partition -DriveLetter $expectPLabel -Size ( $expectPSize )
            
            $PSize=(Get-Partition -DriveLetter $expectPLabel ).Size  #获取当前分区大小

            set_RegRecord -regPath $regPath -expectPLabel $expectPLabel   -PSize $PSize
        
        }
    }
else
    {
    $PSize=(Get-Partition -DriveLetter $expectPLabel ).Size  #获取当前分区大小
    set_RegRecord -regPath $regPath -expectPLabel $expectPLabel   -PSize $PSize
    }

点击查看代码 查看分区扩容情况
#文档说明
#用于判断分区大小是否符合预期,如果符合则注册表写入1,否则注册表写入0
[System.UInt64]$expectPSize= 199GB #期望分区大小
$expectPLabel="Q"   #期望查询的分区驱动器号

$ResKey=$expectPLabel+"_Resize_Res"
$regPath="HKLM:\SYSTEM\HardwareConfig\DiskInfo"  # 用于扩容后记录磁盘信息


#判断注册表是否存放硬盘空间容量,如果有则跳过,如果没有则创建
function set_RegRecord 
{
    #参数
    param($regPath="" , $expectPLabel="" ,$PSize="")
    #扩容结果判断
    if($PSize -ge $expectPSize  )
        { $PResize_Res=1} #1 表示扩容成功
    else
        { $PResize_Res=0} #0 表示扩容失败

    $check_Res= Test-Path $regPath
    if(Test-Path $regPath )
        {
            try
                { 
                Get-ItemProperty $regPath $ResKey -ErrorAction Stop
                set-ItemProperty $regPath -name $ResKey -value $PResize_Res
                }
            catch
                {
                 New-ItemProperty $regPath -name $ResKey -value $PResize_Res -propertyType dword
                }

        }
    else
       {
        #在注册表创建目录
        New-Item -type Directory $regPath
        New-ItemProperty -path $regPath -name  $ResKey -value $PResize_Res -propertyType dword
       }
}

#获取分区所在的磁盘盘符,及当前分区的大小

Try
{    
$PSize=(Get-Partition -DriveLetter $expectPLabel -ErrorAction Stop |Select-Object size).size 
}
Catch
{   
$PSize = 0
}

set_RegRecord -regPath $regPath -expectPLabel $expectPLabel   -PSize $PSize

posted on 2021-12-23 16:59  vmsky  阅读(209)  评论(0编辑  收藏  举报