#Once enough free space has been created on F: this function creates the latest VHD
Function CreateVHD {
$filename = (get-date -uformat %d%b%y) + "_" + $Env:COMPUTERNAME + ".vhd"
$filename = "F:\"+$Filename
write-host .\disk2vhd 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 D: as required and then creates the VHD when there is sufficient disk space freed up.
Do {
if ($fdisk -lt $Cdiskused) {
diskspace
write-host "Not enough free space on D: Drive. Deleting oldest image"
DelVHD
}
}
until ($fdisk -gt $Cdiskused)
#This function is called repeatedly until the space on the D: drive is greater than the used space on the C: drive
function DiskSpace() {
$Cdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$Fdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='F:'" | Select-Object Size,FreeSpace
$CdiskCapacity = $Cdisk.size/1073741824
$CdiskFree = $Cdisk.freespace/1073741824
$CdiskUSed = $CdiskCapacity-$CDiskFree
$fdisk = $fdisk.freespace/1073741824
return $Fdisk, $cdiskUsed
}
diskspace
PS C:\Users\Marek> powershell -noexit -command C:\test.ps1
82,8459358215332
221,111248016357
#This function is called repeatedly until the space on the F: drive is greater than the used space on the C: drive
function DiskSpace() {
$Cdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$Fdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='F:'" | Select-Object Size,FreeSpace
$CdiskCapacity = $Cdisk.size/1073741824
$CdiskFree = $Cdisk.freespace/1073741824
$CdiskUSed = $CdiskCapacity-$CDiskFree
$fdisk = $fdisk.freespace/1073741824
return $Fdisk, $cdiskUsed
}
#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
}
#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 {
if ($fdisk -lt $Cdiskused) {
diskspace
write-host "Not enough free space on F: Drive. Deleting oldest image"
DelVHD
}
}
until ($fdisk -gt $Cdiskused)
diskspace
Directory: F:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-ar-- 2015-04-15 20:49 528 MediaID.bin
d---- 2015-05-02 19:22 WindowsImageBackup6
d---- 2015-05-06 13:37 WindowsImageBackup7
d---- 2015-05-12 12:00 WindowsImageBackup8
d---- 2015-05-17 12:00 WindowsImageBackup9
d---- 2015-05-22 20:51 WindowsImageBackup10
d---- 2015-05-24 13:53 WindowsImageBackup11
d---- 2015-06-01 12:00 WindowsImageBackup12
d---- 2015-06-03 15:07 WindowsImageBackup13
d---- 2015-06-08 12:18 WindowsImageBackup14
d---- 2015-06-15 12:01 WindowsImageBackup15
d---- 2015-06-19 12:01 WindowsImageBackup16
d---- 2015-06-26 12:01 WindowsImageBackup17
d---- 2015-07-10 12:47 WindowsImageBackup18
d---- 2015-07-23 12:01 WindowsImageBackup19
d---- 2015-08-03 12:01 WindowsImageBackup20
d---- 2015-08-08 13:22 WindowsImageBackup21
d---- 2015-08-11 12:01 WindowsImageBackup22
d---- 2015-09-02 22:35 WindowsImageBackup23
d---- 2015-09-16 16:58 WindowsImageBackup
-a--- 2015-09-16 16:55 2311530024 MACPRO--2015-9-16.VHD
96
-a--- 2015-09-21 14:10 2311530024 MACPRO--2015-9-21.VHD
96
#This function will simply delete the oldest file in the root of the F: drive
function test {
Get-ChildItem F:\ | Sort CreationTime | Select -First 1 | Remove-Item -force
}
#This function will simply delete the oldest file in the root of the F: drive
function test {
Get-ChildItem F:\ | Sort CreationTime | Select -First 1 | Remove-Item -force
}
test
#This function will simply delete the oldest file in the root of the F: drive
function test {
Get-ChildItem F:\ | Sort CreationTime | Select -First 1 | Remove-Item -force -Recurse
}
test
#This function is called repeatedly until the space on the F: drive is greater than the used space on the C: drive
function DiskSpace() {
$Cdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$Fdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='F:'" | Select-Object Size,FreeSpace
$CdiskCapacity = $Cdisk.size/1073741824
$CdiskFree = $Cdisk.freespace/1073741824
$CdiskUSed = $CdiskCapacity-$CDiskFree
$fdisk = $fdisk.freespace/1073741824
return $Fdisk, $cdiskUsed
}
#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
}
DelVHD
#This function is called repeatedly until the space on the F: drive is greater than the used space on the C: drive
function DiskSpace() {
$Cdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$Fdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='F:'" | Select-Object Size,FreeSpace
$CdiskCapacity = $Cdisk.size/1073741824
$CdiskFree = $Cdisk.freespace/1073741824
$CdiskUSed = $CdiskCapacity-$CDiskFree
$fdisk = $fdisk.freespace/1073741824
return $Fdisk, $cdiskUsed
}
#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
}
Do {
if ($fdisk -lt $Cdiskused) {
diskspace
write-host "Not enough free space on D: Drive. Deleting oldest image"
DelVHD
}
}
until ($fdisk -gt $Cdiskused)
Do {
if ($fdisk -lt $Cdiskused) {
diskspace
write-host "Not enough free space on D: Drive. Deleting oldest image"
DelVHD
}
}
until ($fdisk -gt $Cdiskused)
0.267337799072266
107.147037506104
Not enough free space on F: Drive. Deleting oldest image
0.697784423828125
107.147037506104
Not enough free space on F: Drive. Deleting oldest image
1.12823104858398
107.147037506104
Not enough free space on F: Drive. Deleting oldest image
1.55867767333984
107.147037506104
Not enough free space on F: Drive. Deleting oldest image
1.9891242980957
107.147037506104
$CdiskUSed = $CdiskCapacity-$CDiskFree
if ($fdisk -lt $Cdiskused) {
until ($fdisk -gt $Cdiskused)
#This function is called repeatedly until the space on the F: drive is greater than the used space on the C: drive
function DiskSpace() {
$Cdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$Fdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='F:'" | Select-Object Size,FreeSpace
$CdiskCapacity = $Cdisk.size/1073741824
$CdiskFree = $Cdisk.freespace/1073741824
$CdiskUSed = $CdiskCapacity-$CDiskFree
$fdisk = $fdisk.freespace/1073741824
return $Fdisk, $cdiskUsed
}
#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
}
#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.
if ($fdisk -gt $Cdiskused) {
write-host "Not enough free space on F: Drive. Deleting oldest image"
}
diskspace
PS C:\Users\Marek> C:\backup2.ps1
1153,06732559204
221,418174743652
#This function is called repeatedly until the space on the F: drive is greater than the used space on the C: drive
function DiskSpace() {
$Cdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$Fdisk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='F:'" | Select-Object Size,FreeSpace
$CdiskCapacity = $Cdisk.size/1073741824
$CdiskFree = $Cdisk.freespace/1073741824
$CdiskUSed = $CdiskCapacity-$CDiskFree
$fdisk = $fdisk.freespace/1073741824
return $Fdisk, $cdiskUsed
}
#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
}
#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.
diskspace
if ($fdisk -gt $Cdiskused) {
write-host "Not enough free space on F: Drive. Deleting oldest image"
}
diskspace
PS C:\Users\Marek> C:\backup2.ps1
1153,06732559204
221,421562194824
1153,06732559204
221,421562194824
# 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
}
#Once enough free space has been created on F: this function creates the latest VHD
Function CreateVHD {
$filename = (get-date -uformat %d%b%y) + "_" + $Env:COMPUTERNAME + ".vhd"
$filename = "F:\"+$Filename
.\disk2vhd 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 ($fdisk -gt $Cdiskused)
write-host "Enough free space on F:\ found...creating latest VHD"
CreateVHD
# 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
}
#Once enough free space has been created on F: this function creates the latest VHD
Function CreateVHD {
$filename = (get-date -uformat %d%b%y) + "_" + $Env:COMPUTERNAME + ".vhd"
$filename = "F:\"+$Filename
.\disk2vhd 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.
write-host "Enough free space on F:\ found...creating latest VHD"
CreateVHD
until ($fdisk -gt $Cdiskused)
until ($f -gt $c)
if ($f -lt $c) {
if ($f -gt $c) {
# Null all the values
$Cdisk = $null
$Fdisk = $null
$CdiskCapacity = $null
$CdiskFree = $null
$CdiskUSed = $null
$Hidden = $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
}
#Actually create the VHD
Function CreateVHD($Hidden) {
$filename = (get-date -uformat %d%b%y) + "_" + $Env:COMPUTERNAME + ".vhd"
$filename = "F:\"+$Filename
If ($Hidden -eq $True){.start-process -WindowStyle Hidden .\disk2vhd.exe c: $filename } # The .\ assumes disk2vhd is in the same folder as this script;
ElseIf ($Hidden -eq $False){.\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 #$True to hide the process; $False to have it run visible
PS C:\> C:\backup.ps1
Enough free space on F:\ found...creating latest VHD
.start-process : The term '.start-process' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, v
erify that the path is correct and try again.
At C:\backup.ps1:34 char:28
+ If ($Hidden -eq $True){.start-process -WindowStyle Hidden .\disk2vhd.exe c: ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.start-process:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\> C:\backup.ps1
Enough free space on F:\ found...creating latest VHD
Start-Process : A positional parameter cannot be found that accepts argument 'F:\23wrz15_MACPRO.vhd'.
At C:\backup.ps1:34 char:28
+ If ($Hidden -eq $True){start-process -WindowStyle Hidden .\disk2vhd.exe c: $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
# 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 .\disk2vhd.exe -argumentlist 'c:', $filename # The .\ assumes disk2vhd is in the same folder as this script;
}
ElseIf ($Hidden -eq $False){.\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
It's possibly not the most graceful solution, if I'm honest, but it should work.
I put the /1073741824 in whilst I was testing as it made for easier numbers on the eye. There's no functional reason for it to remain unless you wanted to pipe some of the variables out.
Open in new window