简体   繁体   中英

Windows PowerShell out of disk space script

This is a modified part of some larger script that removes old VHD images when there is not enough disk space on F drive to fit an image from 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
}

if ($fdisk -gt $cdiskfree) {
write-host "Lot's of space on F: Drive"
}
diskspace

Despite it shows there is more than 1.1 TB on F drive - it does not show my message.

PS C:\Users\Marek> C:\backup2.ps1
1153,06732559204
221,418087005615

What's wrong ?

First, I would suggest using a different variable name here:

$fdisk = $fdisk.freespace/1073741824

Instead of reusing $fdisk.

Second, in this code:

if ($fdisk -gt $cdiskfree) {
write-host "Lot's of space on F: Drive"
}

It looks like you are trying to access variables that are a) only available inside the function and b) in the function that you have not actually called yet.

Third, as a style issue, you can use 1GB instead of 1073741824.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM