【开发】Windows批量文件共享脚本

263 阅读4分钟

更新说明

2020年9月20日更新:由于exe在不同的环境测试时出现错误,暂时停止下载编译后文件。

脚本用途

批量创建多个Windows文件共享,并给每个共享文件夹增加个人权限,同时隔离用户。

脚本环境

语言:Python 辅助工具:Excel

使用说明

由于是使用Windows的SMB进行共享,同时需要为每个文件夹隔离,因此需要创建多个Windows用户。So需要在Excel文件中设置要创建的用户名、密码以及用户组,脚本将会自动读取文件内的用户信息,自动创建用户组、用户以及共享文件夹。

Excel模板文件说明: Excel模板文件说明

Excel模板:下载

部分手动操作说明

这个脚本目前还有部分操作需要我们手动来完成,所幸操作的步骤不多,下面直接上图。

1、当脚本显示“请手动设置文件权限,设置完成后再继续操作!(Y/N)”时,进入 D 盘,找到 share 文件夹并右键文件夹。 操作步骤1

2、右键选择 share 文件夹的属性操作步骤2

3、先选择上面的“安全”选项,再选择右下角的“高级”。 操作步骤3

4、进入高级页面先点击左下角的“禁用继承”,此时会有警告弹窗,选择“从此对象中删除已继承的权限”。 操作步骤4

5、再点击坐下的“添加”按钮。 操作步骤5

6、在弹出的窗口中点击左上的“选择主体”,在红色输入框中输入,Excel文件中填入的用户组! 用户组! 用户组! 切记是用户组不是用户!然后点击确定,在share的权限项目窗口再次点击确定。 操作步骤6

7、在share属性窗口 操作步骤7

8、选择刚刚在第6步所添加的用户组,权限只给“列出文件夹内容”,并点击确定操作步骤8

9、最后再选择 Administrator ,给“完全控制”权限,选择完全控制,剩下的权限他都会自动勾选。最后点击下方确定,在share属性窗口再次点击确定即可。(此步骤可有可无,主要是为了管理员能够全局管理,请自行权衡) 操作步骤9

分割线

脚本代码

代码下载:Download

import os
import sys
import xlrd

# 读取用户数据表
def read_xlsx(path):
    data = xlrd.open_workbook(path)
    table = data.sheet_by_index(0)
    hang = int(table.nrows)
    lie = int(table.ncols)
    list_ls = [] 
    for i in range(hang):
        dic = {"name":"null","password":"null","group":"null"}
        dic["name"] = str(table.cell_value(i,0))
        dic["password"] = str(table.cell_value(i,1))
        dic["group"] = str(table.cell_value(i,2))
        list_ls.append(dic)
    return list_ls

# 创建用户
def creat(user, password):
	command = "net user %s %s /add" %(user, password)
    os.system(command)

# 设置隶属组 
def set_group(user, group):
    command1 = "net localgroup %s %s /add" %(group, user)
    os.system(command1)
    command2 = "net localgroup Users %s /del" %(user)
    os.system(command2)

# 创建用户文件夹及设置权限
def creat_file(file_dict):
    for i in file_dict:
        name = i["name"]
        # 新建文件夹
        command1 = "md d:\share\%s" %(name)
        os.system(command1)
        # 设置用户权限
        command2 = "Cacls d:\share\%s /t /e /c /g %s:F" %(name, name)
        os.system(command2)

# 设置主文件夹共享
def share_file():
    print("请输入共享名称", end=' ')
    file_name = input()
    command = "net share %s=d:\share" %(file_name)
    os.system(command)

def main(file_path):
	# 用户列表
    user_dict = read_xlsx(file_path) 
    for i in user_dict:
        username = i["name"]
        password = i["password"]
        groupname = i["group"]
        # 创建用户
        creat(username, password)
        # 设置用户隶属组
        set_group(username,groupname)
    # 创建用户文件夹
    command = "md d:\share"
    os.system(command)
    tip(user_dict)    
    share_file()

# 手动操作提示
def tip(user_dict):
    print("请手动设置文件权限,设置完成后再继续操作!!(Y/N):", end=' ')
    check_flag = input()
    if check_flag == "y" or check_flag == "Y":
        creat_file(user_dict)


print("\t批量文件共享脚本")
print("温馨提示:使用前请检查杀毒软件是否关闭。(Y/N):",end=' ')
flag = input()
if flag == "Y" or flag == "y" :
    print("是否需要新建用户组?(Y/N):",end=' ')
    flag1 = input()
    if flag1 == "Y" or flag1 =="y":
        print("请输入要新建的用户组名称:(需于excel文件内的用户组名称一致):",end=' ')
        flag2 = input()
        command = "net localgroup %s /add" %(flag2)
        os.system(command)
    print("输入用户表路径:")
    path = input()
    main(path)
else:
    print("请关闭杀毒软件后再次打开此脚本")
    print("脚本结束")
    sys.exit(0)    

报毒说明

由于脚本需要权限添加用户、用户组以及创建文件夹和设置共享,所以需要用到管理员权限,任何杀毒软件此时都要将进程拦截下来,所以使用前请一定一定要关闭电脑里的所有杀毒软件

编译后文件

如果你只是想临时用这个来给自己创建一个多用户的共享文件夹,我也将Py文件打包成了exe文件,只需要改动Excel模板内容即可使用。 释放文件:下载地址1 下载地址2