windows配置多用户共享磁盘

需求

  1. 每个人都有一个单独的文件夹,每个文件夹单独配置账户
  2. 授权用户可查看自己的文件夹和公用文件夹,不可访问其他用户的文件夹
  3. 在其他机子上能临时性的快速访问自己的文件夹

方案

通过 windows 自带的 smb 共享协议,其他电脑可以轻松的通过资源管理器访问服务器上的文件,而且通过在服务器上为每个文件夹分配账户权限,可以实现多用户配置。

流程

思路

单位有 100 来号人,一个一个手动创建实在是太麻烦了,于是用 python 调用 cmd 指令进行批量操作。 主要流程如下:

  1. 创建系统用户,包括姓名、部门、默认密码
  2. 为每个用户在服务器磁盘上创建单独的文件夹
  3. 在文件夹上配置共享,并设置对应的权限

代码

代码如下:

#coding=utf-8
import os
import xlrd

path = 'F:\\服务器文件\\用户资料库'
book_path = path+"\\name.xls"
book = xlrd.open_workbook(book_path)
sheet = book.sheets()[0]

rows = sheet.nrows
for i in range(1,rows):
    username = sheet.cell_value(i,0)
    group = sheet.cell_value(i,1)
    password = "123456"

    # 创建用户并设置密码及禁止修改密码
    command = "net user %s %s /passwordchg:no /expires:never /FULLNAME:%s /add" %(username, password, username)
    os.system(command)

    # 设置密码永不过期
    command = "wmic useraccount where \"name='%s'\" set passwordexpires=false"%(username)
    os.system(command)

    # 设置属组
    command = "net localgroup %s %s /add" %(group, username)
    os.system(command)

    # 删除默认 Users 组
    command = "net localgroup Users %s /del" %(username)
    os.system(command)

    # 创建文件夹
    userpath = path+"\\"+username
    os.mkdir(userpath,777)

    # 配置安全权限
    command = "Cacls %s /t /e /c /g %s:F"%(userpath,username)
    os.system(command)

    # 配置共享权限
    command = "net share %s=%s /GRANT:%s,FULL"%(username,userpath,username)
    os.system(command)

“您的支持是我持续分享的动力”

微信收款码
微信
支付宝收款码
支付宝

目录