Scripting Windows Shares

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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.

Last updated on Aug 06, 2025 23:13 -0400