As much as I try to avoid it, sometimes I need to use Windows servers, and windows .bat
files aren’t exactly the pinnacle of scripting languages. This post is about bulk-sharing home directories with consistent permissions.
I decided to solve this by writing a PHP script to generate a batch file, rather than use VB script.
This crude script will make a batch file to share every subdirectory (in this case, hundreds of users’ home directories), and also delete desktop.ini
from each of them. Save the code below as magic.php
, run it, and then run tricks.bat
.
<?php
$stuff = whats_here();
$tricks = fopen("tricks.bat", "w");
foreach($stuff as $folder) {
$line = do_things($folder);
fwrite($tricks, $line);
}
fclose($tricks);
function do_things($folder) {
$things .= ":: $folder\r\n";
$things .= "net share $folder /DELETE\r\n";
$things .= "net share $folder=".getcwd()."\\$folder /GRANT:EVERYONE,FULL\r\n";
$things .= "del /Q $folder\desktop.ini\r\n\r\n";
return $things;
}
function whats_here() {
/* List directories in this one */
$here = opendir(getcwd());
$dir = array();
while($subdir = readdir($here)) {
if(is_dir($subdir) && $subdir != "." && $subdir != "..") {
$dir[] = $subdir;
}
}
closedir($here);
return $dir;
}
As a side note, /GRANT:EVERYONE,FULL
is safe in this environment because ACLs are used for access control,
I’ve heard that Windows PowerShell is useful once you get used to it, but it’s not a useful skill outside of Windows admin, so for now a couple of PHP scripts will have to do.