如何将该网络路径与用户输入的变量连接起来(它将是完整的网络路径)?
因此,用户键入新的文件夹名称,例如:Folder-123(将存储在变量$ pjname中)
Copy-Item "\\SERVER\Work_3rd\R Drive Structure\Project No\MDCXXXX" -Destination "\\SERVER\Work_3rd" -Recurse
write-host "Folder has been created. Press any key to continue..."
[void][System.Console]::ReadKey($true)
Write-Host "Please enter the project name: "
$pjname = Read-Host
Write-Output "New Folder will be: $pjname"
Rename-Item -Path "\\SERVER\Work_3rd\MDCXXXX" -NewName $pjname
write-host "Folder has been renamed. Press any key to continue..."
[void][System.Console]::ReadKey($true)
$pathToTemplate = '\\SERVER\Work_3rd\R Drive Structure\Project No\MDCXXXX'
$rootPath2 = '\\SERVER\Work_3rd\'
$rootPath = -join ($rootPath2, $pjname) # this concatenates the new project
name on to the root folder path**
# $rootPath += $pjname # this concatenates the new project name on to the
root folder path
If(Test-Path $rootPath)
{
$CurrentACL = (Get-Item $pathToTemplate).GetAccessControl('Access')
$CurrentACL | Set-Acl -Path $rootPath
}
此存储在$ pjname中的新文件夹应具有类似\\\SERVER\Work-3rd\ + FOLDER NAME
的网络路径。例如\\\SERVER\Word-3rd\Folder-123
PowerShell找不到新文件夹的最终路径,因此未对其应用权限。
我正在测试区域中尝试此问题,
Folder has been renamed. Press any key to continue...
Get-Acl : Cannot find path '\\SERVER\test-area\Test-123' because it does not exist.
At C:\Users\felipe.sa\Desktop\Script\NewProjectFolder\NewProject-WP_-
_ProductionV3.ps1:279 char:8
+ $acl = Get-Acl $NewNetworkPath
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException
+ FullyQualifiedErrorId :
GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand
You cannot call a method on a null-valued expression.
At C:\Users\felipe.sa\Desktop\Script\NewProjectFolder\NewProject-WP_-
_ProductionV3.ps1:282 char:1
+ $acl.SetAccessRule($rule)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
答案 0 :(得分:0)
您是要在该共享路径中创建新目录还是重命名目录?看来您正在尝试重命名目录。
我猜这是行不通的,因为您在路径中缺少结尾的“ \”。以下是一些示例代码,可将用户提供的变量添加到网络路径:
$MyRootPath = "\\SomeServer\Dir1\Dir2\"
Write-Host "Enter Dir Name"
$myAnswer = Read-Host
在提示输入新目录时,我键入了“ hello”。
$finalAnswer = $myAnswer.Trim()
$NewNetworkPath = ("{0}{1}" -f $MyRootPath, $finalAnswer)
$NewNetworkPath
返回:
\\SomeServer\Dir1\Dir2\hello
答案 1 :(得分:0)
无论何时连接路径(尤其是最终用户提供的路径),都坚持使用可以完成大部分任务的实用程序。使用combine method,因为字符串串联有几个陷阱,这些陷阱必须加以缓解。
[io.path]::combine('\\server\share', 'newfolder')
combin方法将路径部分作为数组,并构建适当的路径。注意这不会验证路径是否存在。它可以很好地处理尾随路径分隔符。接下来的这些命令将产生相同的结果。
[io.path]::combine('\\server\share\', 'newfolder')
[io.path]::combine('\\server\share', 'newfolder')
尽管要注意领先路径分隔符:
如果随后的路径之一是绝对路径,则合并操作将从该绝对路径开始重置,并丢弃所有先前的合并路径。
答案 2 :(得分:-1)
感谢Matt和oze4!现在发生了一个奇怪的问题。我使用了oze4的解决方案,有时它可以工作,有时却不能。是用户输入的字符串吗?
工作时,文件夹名称为“ MDX1111-XXXX Xxxxxxx Xxxxxxxxx”-32个字符。我使用文件夹名称'MDX1112-Xxxxxx Xxxxxxx Xxxxxxxxxx'-35个字符再次运行了代码,并在下面得到此错误:
public CompletableFuture<StatusResult> sendEmailAndSaveStatus(String content, String address) {
CompletableFuture<Boolean> sendEmail = myService.sendEmail(content, address);
CompletableFuture<StatusResult> result = new CompletableFuture<>();
sendEmail.exceptionally(e -> {
LOGGER.info("Exception during send email ");
myService.saveStatus(e.getMessage(), address).thenApply(x -> result.complete(x));
return false;
});
sendEmail.thenCompose(x -> myService.saveStatus("good", address)).thenApply(x -> result.complete(x));
return result;
}
有什么想法吗?
谢谢。