regex - 在服务器上创建多个文件夹,然后为每个文件夹创建网络共享
问题描述
我正在尝试使用我想创建的文件夹名称列表循环遍历一个文本文件。然后还要为该文件夹创建一个网络共享。
我能够创建文件夹,但在创建网络共享时遇到了困难。
$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
}
}
解决方案
如果我对 uncroutes.txt 的假设正确,并说它在每一行都包含一个 unc 路径,那么您可以使用以下代码获取共享名称:
$ShareName = ($route | sls -pattern "([0-9a-zA-Z-_ ]+)$").matches.value
然后传入$ShareName
你的-name
参数。
推荐阅读
- google-cloud-platform - 当我们在 Google 云中从 Fine grain 切换到 Uniform 时,ServiceAccount 会发生什么
- javascript - 我无法使用 npm 安装 Parcel - 无法解析 package.json 数据
- java - 是否可以使用可选参数制作通用功能接口?
- java - 从 firebase 数据库中检索 ratingbar 数据以显示在帖子中
- c# - Linq2DB 创建动态表模型
- azure - 试图将秘密从一个 Kv 存储到另一个
- python - 应用函数从数据框的每一行中选择特定的单词
- python - Pyspark 中的卡夫卡“partition.assignment.strategy”
- python - Django 字段未出现在经过验证的数据中,但在请求中
- javascript - 我有一个包含两个选项的下拉菜单,“已批准”、“已拒绝”。我需要计算每个选项