Simple script to import/export quotas from File Server Resource Manager


Ok, because the guys at Microsoft are to lazy to make a script for importing/exporting quotas (or at least I couldn’t find any) I’ve decided to make a little batch script that should do the trick.

File1: create_quota_list.bat

@echo off

dirquota q l > all_quotas.txt
echo > quota_path.txt
for /f “tokens=1,2,3″ %%L in (all_quotas.txt) do (
if /I %%M equ Path: echo %%N >> quota_path.txt
)

echo REM Run this file to restore quotas > run_quota.bat

@for /f “skip=1 tokens=1″ %%T in (quota_path.txt) do (
generate_quota.bat “%%T”
)

End of file 1

File 2: generate_quota.bat

@echo off

dirquota q l /path:%1 /list-notifications > temp.txt
for /f “skip=2 tokens=1,2,3″ %%A in (temp.txt) do (

if /I %%A equ Limit: (
set lmt=%%B
set tlmt=%%C
)

if /I %%B equ To: set ml=%%C

)

for /f “tokens=1 delims=@” %%X in (“%ml%”) do set fname=%%X
for /f “tokens=1 delims=.” %%Z in (“%lmt%”) do set lmt=%%Z
set lmt=%lmt%%tlmt%

echo Notification=m> “%fname%.txt”
echo To=”%ml%”>> “%fname%.txt”
echo From=FSRM@yourdomain.com>> “%fname%.txt”
echo Subject=Quota limit exceeded>> “%fname%.txt”
echo Message= Dear user, \>> “%fname%.txt”
echo \>> “%fname%.txt”
echo You have exceeded the limit for the quota on [Quota Remote Paths] on server [Server]. The quota limit is [Quota Limit MB] MB, and [Quota Used MB] MB currently is in use ([Quota Used Percent]%%^ of limit). \>> “%fname%.txt”
echo \>> “%fname%.txt”
echo Please be aware that at 100%%^ of your storage capacity your access to the share will be blocked until you reduce the size of the data stored. \>> “%fname%.txt”
echo If you need more disk space, kindly reduce the size of your data or remove old data. \>> “%fname%.txt”
echo \>> “%fname%.txt”
echo Thank you in advance for understanding. \>> “%fname%.txt”

echo dirquota q a /path:%1 /Limit:%lmt% /Type:Hard /Add-Threshold:80 /Add-Threshold:90 /Add-Threshold:100 /Add-notification:80,m,”%fname%.txt” /Add-notification:90,m,”%fname%.txt” /Add-Notification:100,m,”%fname%.txt” >> run_quota.bat

End of file 2.

You just copy the text above in a .bat file and run it like this filename.bat dir_with_quota_path. It will create 2 files: the notification config_file and the batch file to put the quota back. This is just a rough version and you should tune it to your needs. Have phun.

L.E. Now there are 2 files and I’ve changed it a bit. You should run the first file and make sure the second file is names as in the first file and everything it’s gonna be ok.




Tags: , , , , ,

Daca vrei sa vezi ce-i mai trece prin cap suricatei in continuarea, aboneaza-te la RSS.

Information and Links

Other Posts

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Thanks alot for the script, it saved me lots of time!

this script is very useful but I have been having a problem, when I try to import with run_quota.bat I get an error, “the specific limit is invalid” the problem is the “,” for example 100,00MB or 1,50GB
Have you some ideas to solve it on the script?
I thought to convert all the quotas manually :-( as MB and then cut the part ,00 or ,50 this is hard form me, I have an hundred of folders

hey big,

first of all open all_quotas.txt file and look at one of the Limit fields. If the separator is , (1,5GB) then you need to change this line:
for /f “tokens=1 delims=.” %%Z in (“%lmt%”) do set lmt=%%Z
to
for /f “tokens=1 delims=,” %%Z in (“%lmt%”) do set lmt=%%Z

Otherwise leave it as it is.

Now, dirquota doesn’t like values like 1.5Gb or 1,5GB and that’s why you are getting that error. I’ve managed to cook up something, let me know if it works for you. Now on the second file (generate_quota.bat) you need to replace everything from @echo off till set lmt=%lmt%%tlmt% with this code:

@echo off

dirquota q l /path:%1 /list-notifications > temp.txt
for /f “skip=2 tokens=1,2,3″ %%A in (temp.txt) do (

if /I %%A equ Limit: (
set lmt=%%B
set tlmt=%%C

)

if /I %%B equ To: set ml=%%C

)

for /f “tokens=1,2 delims=.” %%J in (“%lmt%”) do (
set lmt=%%J
set /A lmt1=%%K / 10
)

if /I %tlmt% equ GB (
set /A lmt *= 1024
set /A lmt1 *= 1024 / 10
set /A lmt += lmt1
set tlmt=MB
Goto :SKIPMB
)

if /I %tlmt% equ MB (
set /A lmt *= 1024
set /A lmt1 *= 1024 / 10
set /A lmt += lmt1
set tlmt=KB

)

:SKIPMB

for /f “tokens=1 delims=@” %%X in (“%ml%”) do set fname=%%X

set lmt=%lmt%%tlmt%

Again, if your delimiter is , you need to change the delims=. to delims=,

Let me know how it goes :)

Ok it seems it works! very good!!
Many Thanks
just one thing, I copied your new part of script but there a typing error, you used “ instead of ” :-)

I also tryed to modify the quotas but the file run_quota.bat must be cleaned (all the data unmodified must be deleted) but this isn’t a script problem.

Hi,

I just need to save and restore quota, i don’t need notifications, can i use just the first file ?

thanks

Hi,

You still need to use both files but you can delete from the second file starting at:

echo Notification…
until ……
echo Thank you….

i cant work when execute generate_quota.bat ,

it will show “The specified path is invalid.” in TEMP.txt

Will this work on Windows 2008 standard Server?

We’re planning to migrate our file server to another server with Windows 2008. Our current server is in Windows 2003 R2.

Thanks,

Dear Goja,

Thanks for the script. I save us a lot of time migrating and applying the quota templates to our new File Cluster.

We came from an 2003 R2 file cluster environment and migrated everything to a new Server 2008 R2 file cluster.

First we ran an export of the current quota’s on the old cluster. After migrating the to the new cluster we applied the newly created quota templates to the existing folder. This all went perfectly.

Many thanks!

Hi Goja,

Congratulations for you good script! It´s very useful.

But, today, when a run it on a Windows 2003R2 I get the follow error:

C:\q>create_quota_list
GB was unexpected at this time.

Any ideas?

Thanks in advance,

Chola

Hi Rogerio,

This usually happens when you use comma (,) as a delimiter for digits. You can try to change the line below in the script and see if it works.

for /f “tokens=1 delims=,” %%Z in (“%lmt%”) do set lmt=%%Z

If you want to be sure of the delimiter you use in your environment run this command on your fileserver and look at the Limit: line:

dirquota q l /path:”path_to_a_shared_folder”

Hi Goja,

Thanks very much for your help. I´ll try this and post the results. Another issue that I found is abou “spaces” in the path. If the path of any cota contains a space character, the script cut the path name at the first space char found. Example: the path “test test” will be only “test” in the script.

Thanks again!

Chola

Here’s a script for migrating from 2003 to 2012 boxes:

Script to generate path and templates, to run on the 2003 box, and that creates a CSV file:
$Quotas = New-Item -type file Quotas.csv -Force

$AllPath = dirquota q l | Where-Object { $_.Contains(“Quota Path:”)}

foreach ($line in $AllPath)
{
$Path = $line.Substring(24)
$Template = dirquota q l /path:$Path | Where-Object { $_.Contains(“Source Template:”)}
$Template = $Template.Substring(24)
$Template = $Template.Replace(” (Does not match template)”,”")
add-content $Quotas “$Path;$Template”
}

Script to recreate quota, based on templates that must have been migrated before, to run on the 2012 box:
$Quotas = Get-Content Quotas.csv

foreach($Line in $Quotas)
{
$Array = $Line.Split(“;”)
$Path = $Array[0]
$Template = $Array[1]
write “New-FsrmQuota -path $Path -Template “”$Template”"”
}

Hi Goja,

May I know, both batch files have to run on source server? How about the destination server? Do we need to run the same batch files on the destination server?

Hey aije,

Create_quota_list and generate_quota must be run on the source server. In return the generate_quota.bat will generate another batch file and some txt files for each share that you copy on the destination server and run them there.

Hi Goja,

Thanks for the direction.
Btw, I did send you an email requesting some guide from you.
I email it to mail@goja.ro

best regards.

Hi Guys,
Thanks for the script.
I am facing problem as my quota folder names are with space (like Corp test). Seeking your help.

Thanks
Ayan