首页 > 解决方案 > windows 7 powershell v2.0创建共享文件夹后如何给不同用户共享权限

问题描述

首先,我想创建一个文件夹,然后将其设为共享文件夹,最后我想为不同的用户授予 Windows 7 PowerShell v2.0 上共享文件夹的不同权限。

其他功能正常,但添加共享权限功能不起作用。我查找并发现在创建共享文件夹时可以授予共享权限,但我也想添加其他用户。

另外,我查看了共享文件夹权限!

Powershell,如何为共享文件夹添加权限

使用 PowerShell 对共享文件夹的权限

文件夹的powershell共享权限级别

cls

function CreateSharedFolder{
    $FolderName = Read-Host "Enter Folder Name"
    $SharedFolderName = Read-Host "Enter Shared Folder Name[Default same as Folder Name]"
    if(!$SharedFolderName){
        $SharedFolderName = $FolderName
    }
    $CheckSharedFolderExists = Get-WmiObject Win32_Share -filter "name='$SharedFolderName'" -ErrorAction SilentlyContinue
    if($CheckSharedFolderExists){
        Write-Host "Duplicate Share. Shared Folder with this name already exists" -ForegroundColor Red
        break
    }
    else{
        Write-Host "No Existing Shared Folder Found with this name" -ForegroundColor Green
    }
    $FolderPath = Read-Host "Enter Folder Path[Default current path/location]"
    if(!$FolderPath){
        $ParentDirectoryPath = Get-Location
        $FolderPath = Join-Path $ParentDirectoryPath $FolderName
    }
    $CheckFolderExists = Test-Path -Path $FolderPath
    if($CheckFolderExists){
        Write-Host "Folder with this name already exists"
        $UserInput = Read-Host "Do you want to make shared Folder?  [y]Yes  [n]No[Default]"
        if($UserInput -ne "y"){
            Write-Host "You selected NO"
            break
        }
    }
    else{
        $NewFolder = New-Item -Path $FolderPath -type Directory
        Write-Host "Creating New folder ..." -ForegroundColor Green
    }
    $Shares = [WMICLASS]"Win32_Share"
    $createShare = $Shares.Create($FolderPath, $SharedFolderName, 0)
    switch($createShare.ReturnValue){
        0{
            Write-Host "Shared folder created successfully" -ForegroundColor Green
        }
        1{
            
        }
    }
    $SharedFolderExists = Get-WmiObject Win32_Share -filter "name='$SharedFolderName'" -ErrorAction SilentlyContinue
#    $sharedFolderExists
    if($SharedFolderExists){
        Write-Host "Shared folder exists" -ForegroundColor Green
    }else{
        Write-Host "Shared Folder does not exists " -ForegroundColor Red
    }

    
}


function Get-ListofSharedFolder{
    $SharedFolders = Get-WmiObject Win32_Share
    $SharedFolders
}

function CheckSharedFolderPermission{
    $SharedFolderName = Read-Host "Enter Shared Folder Name"
    $SharedFolder = Get-WmiObject -Class Win32_Share -Filter "name='$SharedFolderName'"
    if(!$SharedFolder){
        Write-Host "Shared Folder with this name DOES NOT EXISTS" -ForegroundColor Red
        break
    }
    $SharedFolder | Get-Acl
#    $SharedFolder | Get-Acl | Format-List *    
}

function AddSharedFolderPermission{
    $SharedFolderName = Read-Host "Enter Shared Folder Name"
    $SharedFolder = Get-WmiObject -Class Win32_Share -Filter "name='$SharedFolderName'"
    $SharedFolder
    $folderPath
    if(!$SharedFolder){
        Write-Host "Shared Folder with this name DOES NOT EXISTS" -ForegroundColor Red
        break
    }else{
        Write-Host "Shared Folder with thi name found" -ForegroundColor Green
    }
    $FolderPath = Read-Host "Enter Folder Path[Default current path/location]"
    if(!$FolderPath){
        $ParentDirectoryPath = Get-Location
        $FolderPath = Join-Path $ParentDirectoryPath $FolderName
    }
    $FolderPath
    $AccountName = Read-Host "Enter User/Group Name:
        Users
        Administrators
        Everyone
        or Any Other User/Group name(Custom Name)
    "
    $AccessRightUserInput = Read-Host "Enter Access Right Type:
        0. Full
        1. Read [Default]
        2. Change 
    "
    
    switch($AccessRightUserInput){
        0{
            $AccessRight = "FullControl"
        }
        1{
            $AccessRight = "Read"
        }
        2{
            $AccessRight = "Modify"
        }
        default{
            $AccessRight = "Read"
        }
    }
    switch($AccessRightUserInput){
        0{
            $ShareRight = "FULL"
        }
        1{
            $ShareRight = "Read"
        }
        2{
            $ShareRight = "Change"
        }
        default{
            $ShareRight = "Read"
        }
    }

    net share $SharedFolderName="$ParentDirectoryPath" /grant:$AccoutName,$ShareRight
#    Write-Host "Share Permission $ShareRight given to user/group $AccountName"
    #Give Access Permission
    $Acl = Get-Acl -Path $FolderPath
    $permission = "$AccountName", "$AccessRight", "ContainerInherit,ObjectInherit", "None", "Allow"
    $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($permission)
    $Acl.SetAccessRule($AccessRule)
    $Acl | Set-Acl $FolderPath
    Write-Host "$AccessRight given to user/group `"$AccountName`"" -ForegroundColor Green
}

#CreateSharedFolder
AddSharedFolderPermission

错误:


net : The syntax of this command is:
At C:\Users\Sheraram Prajapat\Desktop\shared folder win7.ps1:131 char:5
+     net share $SharedFolderName="$ParentDirectoryPath" /grant:$Accout ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (The syntax of this command is::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
NET SHARE
sharename
          sharename=drive:path [/GRANT:user,[READ | CHANGE | FULL]]
                               [/USERS:number | /UNLIMITED]
                               [/REMARK:"text"]
                               [/CACHE:Manual | Documents| Programs | BranchCache | None]
          sharename [/USERS:number | /UNLIMITED]
                    [/REMARK:"text"]
                    [/CACHE:Manual | Documents | Programs | BranchCache | None]
          {sharename | devicename | drive:path} /DELETE
          sharename \\computername /DELETE

此命令还创建一个共享文件夹并授予权限,它不能授予已创建或现有共享文件夹的权限

标签: powershellwindows-7shared-directory

解决方案


推荐阅读