Windows PowerShell out of disk space script -
this modified part of larger script removes old vhd images when there not enough disk space on f drive fit image 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 shows there more 1.1 tb on f drive - not show message.
ps c:\users\marek> c:\backup2.ps1 1153,06732559204 221,418087005615
what's wrong ?
first, suggest using different variable name here:
$fdisk = $fdisk.freespace/1073741824
instead of reusing $fdisk.
second, in code:
if ($fdisk -gt $cdiskfree) { write-host "lot's of space on f: drive" }
it looks trying access variables a) available inside function , b) in function have not called yet.
third, style issue, can use 1gb instead of 1073741824.
Comments
Post a Comment