Change share permissions with RMTSHARE + Powershell

It is possible that you need to Migrate shares between servers because it run out of space.
It is also possible that during migration you want to leave shares shared as READONLY and it is definitely possible that the number of shares is so huge that you need to find out a way to automate it.

With RMTSHARE utility (availabe in el NT4 Resource Kit) and a bit of Powershell we can automate the process.

I am taking for granted we have a list of shares to modify
$server="AE1345"
$shares=get-content("shares.txt")
Walk through all shares in the list and get the existing share permission for each one.
foreach ($share in $shares)
{
write-host $share
#get existing permissions
$val=rmtshare \\$servidor\$share
#go on...




As it is a commandline utility and not a cmdlet, getting the proper permission info is quite a craftwork.
$count=0
    foreach ($v in $val){if ($v -eq "Permissions:"){break}else{$count++}} #positio of Permissions,5
    for ($i=-2;$val[$i] -ne $val[$count];$i--) #-1 = The command completed successfully
    {
    $campo=$val[$i].split(":")
    $perm=$campo[0].trim()
    write-host "$($val[$i]) -> $perm"
#go on...
This is to say the line The command completed successfully is -1 so we walk upwards the -2, -3, -4 etc to reach the line with Permissions string and for every line inside we execute rmtshare again with the /GRANT <user>:READ parameter.
#change to readonly
    rmtshare \\$servidor\$share /GRANT "$perm":READ
    }#end permissions per share
}#end foreach shares
Here is the script:
$server="AE1345"
$shares="Compras","Sistemas","Finanzas","Desarrollo","Direccion"
foreach ($share in $shares)
{
write-host $share
#get existing permissions
$val=rmtshare \\$server\$share
$count=0
    foreach ($v in $val){if ($v -eq "Permissions:"){break}else{$count++}} #positio of Permissions,5
    for ($i=-2;$val[$i] -ne $val[$count];$i--) #-1 = The command completed successfully
    {
    $campo=$val[$i].split(":")
    $perm=$campo[0].trim()
    write-host "$($val[$i]) -> $perm"
    #change to readonly
    rmtshare \\$server\$share /GRANT "$perm":READ
    }#end permissions per share

}#end foreach shares

Comments