首页 > 解决方案 > 在服务器上创建多个文件夹,然后为每个文件夹创建网络共享

问题描述

我正在尝试使用我想创建的文件夹名称列表循环遍历一个文本文件。然后还要为该文件夹创建一个网络共享。

我能够创建文件夹,但在创建网络共享时遇到了困难。

$folder ='\\networkserver\D$'  #Root Directory to place the New folders in.
$routes = get-content 'C:\uncroutes.txt'



foreach ($routes in $routes) {
    $newpath = Join-Path "$folder\" -ChildPath $routes
    New-Item $newpath -type Directory
    foreach ($newpath in $newpath) {
        New-SmbShare -Name $newpath -Path $folder -FullAccess Administrator
    }
}

这是错误消息:

New-SmbShare -Name $newpath -Path $folder -FullAccess Adminis ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare) [新- SmbShare], CimException + FullyQualifiedErrorId : Windows 系统错误 5,New-SmbShare

在@Adminofthings 的帮助下得到它的帮助。这是工作代码。

    $folder ='\\networkserver\D$'  #Root Directory to place the New folders in.
$routes = get-content 'C:\uncroutes.txt' #list of folder Names


foreach ($route in $routes) {
    $newpath = Join-Path "$folder\" -ChildPath $route
    New-Item $newpath -type Directory
    foreach ($ShareName in $ShareNames) {
        $ShareName = ($route | sls -pattern "([0-9a-zA-Z-_ ]+)$").matches.value
        $serverpath = "d:\$route"
        New-SmbShare -Name $ShareName -Path $serverpath -FullAccess Administrator
    }
}

标签: regexwindowspowershellfor-loop

解决方案


如果我对 uncroutes.txt 的假设正确,并说它在每一行都包含一个 unc 路径,那么您可以使用以下代码获取共享名称:

$ShareName = ($route | sls -pattern "([0-9a-zA-Z-_ ]+)$").matches.value

然后传入$ShareName你的-name参数。


推荐阅读