我正在使用 Disk2VHD 这样的简单备份脚本。我想稍微修改一下,以便检查 C 盘驱动器的使用情况并将其与 F 盘驱动器进行比较,这样只有当 F 盘上没有足够的可用磁盘空间时才会删除旧映像。
当前,当已创建超过 4 个图像时,它会删除旧图像。
REM
REM A simple backup system using disk2vhd
REM
REM version 1.3, by J.E.B., 2011-02-22
REM
REM requires 'disk2vhd.exe' to be in the path
REM
setLocal EnableDelayedExpansion
REM "DRIVES" can be one drive identifier with colon, multiple separated by spaces,
REM or asterisk for all.
REM "DEST" can be a drive letter or a UNC.
SET DRIVES="C:"
SET DEST="F:"
REM Keep most recent 4 VHD files in DEST, delete the rest
for /f "skip=4 tokens=* delims= " %%a in ('dir/b/o-d %DEST%\*.VHD') do (
del %DEST%\%%a
)
REM Backup to VHD
C:
cd \
DISK2VHD %DRIVES% %DEST%\%COMPUTERNAME%--%date:~-10,3%%date:~-7,2%%date:~-4,4%.VHD
EXIT /B n
答案1
这就是——一个可以工作的脚本!
# Null all the values
$Cdisk = $null
$Fdisk = $null
$CdiskCapacity = $null
$CdiskFree = $null
$CdiskUSed = $null
#This function is called to determine the used space on the C: drive
function UsedDiskSpace() {
$Cdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$CdiskCapacity = $Cdisk.size/1073741824
$CdiskFree = $Cdisk.freespace/1073741824
$CdiskUSed = $CdiskCapacity-$CDiskFree
return $cdiskUsed
}
#This function is called repeatedly until the space on the F: drive is greater than the used space on the C: drive
function FreeDiskSpace() {
$Fdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='F:'" | Select-Object Size,FreeSpace
$Fdisk = $Fdisk.freespace/1073741824
return $Fdisk
}
#This function will simply delete the oldest file in the root of the F: drive
Function DelVHD {
Get-ChildItem F:\ | Sort CreationTime | Select -First 1 | Remove-Item -force -Recurse
}
#Create the VHD - pass $true to run the Disk2VHD program hidden; $False for visible. First run should be false to allow agreement of EULA.!
Function CreateVHD($Hidden) {
$filename = (get-date -uformat %d%b%y) + "_" + $Env:COMPUTERNAME + ".vhd"
$filename = "F:\"+$Filename
If ($Hidden -eq $True){
start-process -WindowStyle Hidden c:\disk2vhd.exe -argumentlist 'c:', $filename # The .\ assumes disk2vhd is in the same folder as this script;
}
ElseIf ($Hidden -eq $False){c:\disk2vhd.exe c: $filename # The .\ assumes disk2vhd is in the same folder as this script;
}
}
#This loop kicks everything off - it gets the free disk space, deletes files one at a time on F: as required and then creates the VHD when there is sufficient disk space freed up.
Do {
$c = useddiskspace
$f = freediskspace
if ($f -lt $c) {
write-host "Space used on C: = "$c
write-host "Free space on F: = "$f
write-host "Not enough free space on F: Drive. Deleting oldest image"
DelVHD
}
}
until ($f -gt $c)
write-host "Enough free space on F:\ found...creating latest VHD"
CreateVHD $true #NOTE - Run with $False so the Disk2VHD window is visible at least the first time in order to accept the EULA