File Server: Add Quotas in Windows Server 2012 R2 with Powershell

Recently I had to assign quotas to a hefty number of resources in Windows Server 2012 R2, once added the File Server role it includes the powershell module to set quotas and deprecate the dirquota.exe command.
import-module FileServerResourceManager

In my case there were shares of different sizes, well, I didn't even know the precise size and wanted to increase the quota 3GB the real size.
A good (and fast) way to know a resource size is to create a SOFT quota with whatever size:
new-fsrmquota -path $ruta -size 1GB -softlimit
We can then query the information of the quota and increase it in 3GB:
$sizeinbytes=(get-fsrmquota -path $ruta).usage
$newsizeinbytes=$sizeinbytes+(3 * 1073741824)
Now we can convert the quota to HARD type and establish the final size:
set-fsrmquota -path $ruta -size $newsizeinbytes -softlimit:$false
I've made a script with a menu to generate Quotas easily:
Function generate-soft()
{
$ruta=read-host "Path to add Quota?"
$currentquota=get-fsrmquota -path $ruta -erroraction silentlycontinue
 if ($currentquota -ne $null)
 {
 write-host "A Quota for that path already exist!" -fore yellow
 $currentquota|ft PSComputerName,path,disabled,softLimit
 }
 else
 {
 new-fsrmquota -path $ruta -size 1GB -softlimit
 }
}#end generate-soft
Function generate-softfromshares()
{
$arrayshares=gwmi Win32_share|?{$_.path.length -gt 3}
$arrayQuotas=(get-fsrmquota).path

#list existing shares with no quota defined
$selected=$arrayshares|?{$arrayQuotas -notcontains $_.path}|select PSComputername,name,path|out-gridview -passthru -title "Select SOFT QUOTAS to generate"
 if ($selected -eq $null){write-host "None selected" -fore yellow}
 else{$selected|%{new-fsrmquota -path $_.path -size 1GB -softlimit}}
}#end generate-softfromshares

Function generate-hard()
{
$array=get-fsrmquota |?{$_.softlimit -eq $true}

$selected=$array|select PSComputerName,path,usage|out-gridview -passthru -title "Select Quotas to activate"
 if ($selected -eq $null){write-host "None selected" -fore yellow}
 else
 {
 $selected|%{
  $newsizeinbytes=$_.usage+(3 * 1073741824)# increase de 3 GB real size
  set-fsrmquota -path $_.path -size $newsizeinbytes -softlimit:$false  
  #check changes
  get-fsrmQuota -path $_.path|%{  
  $sizeGBactual = "{0:0.0}GB" -f ($_.size/1GB)
  write-host "Current Quota of $($_.path) is $sizeGBactual" -fore cyan
  }
  }
 }
}#end generate-hard

### main ###
import-module FileServerResourceManager
do
{
write-host "THE QUOTA MAKER" -fore cyan
write-host "  1. CREATE NEW SOFT QUOTA MANUALLY" -fore cyan
write-host "  2. CREATE NEW SOFT QUOTA FROM SHARE'S LIST" -fore cyan
write-host "  3. MIGRATE SOFT QUOTA TO HARD QUOTA" -fore cyan
write-host "  0. Exit" -fore cyan
$val=read-host "Choose an option"
 switch ($val)
 {
 1{generate-soft}
 2{generate-softfromshares}
 3{generate-hard}
 }
}
while ($val -ne "0")

Comments