【问题标题】:Concatenate network path + variable (name of the folder)连接网络路径 + 变量(文件夹名称)
【发布时间】:2019-03-30 02:26:51
【问题描述】:

如何将此网络路径与用户输入的此变量连接起来(这将是一个完整的网络路径)?

所以用户键入新的文件夹名称,例如: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

【问题讨论】:

  • 查看 cmdlet Join-PathTest-Path

标签: powershell cmd directory


【解决方案1】:

您是要在该共享路径中创建一个新目录,还是重命名一个目录?您似乎正在尝试重命名目录。

我猜它不起作用,因为您缺少路径中的尾随“\”。下面是一些将用户提供的变量添加到网络路径的示例代码:

$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

【讨论】:

    【解决方案2】:

    当您连接路径时,尤其是最终用户提供的路径,请坚持使用可以完成大部分繁重工作的实用程序。使用combine method,因为字符串连接有几个必须被不必要地缓解的陷阱。

    [io.path]::combine('\\server\share', 'newfolder')
    

    combine 方法将路径部分作为一个数组,并构建一个正确的路径。请注意,这不会验证路径是否存在。它可以很好地处理尾随路径分隔符。接下来的这些命令产生相同的结果。

    [io.path]::combine('\\server\share\', 'newfolder')
    [io.path]::combine('\\server\share', 'newfolder') 
    

    但请注意前导路径分隔符:

    如果后续路径之一是绝对路径,则组合操作将从该绝对路径开始重置,丢弃所有先前的组合路径。

    【讨论】:

      【解决方案3】:

      感谢马特和 oze4!现在发生了一个奇怪的问题。我使用了 oze4 的解决方案,有时有效,有时无效。会不会是用户输入的字符串?

      当它工作时,文件夹名称是“MDX1111 - XXXX Xxxxxxx Xxxxxxxxx” - 32 个字符。我使用文件夹名称 'MDX1112 - Xxxxxx Xxxxxxx Xxxxxxxxxx' - 35 个字符再次运行代码,并在下面收到此错误:

      Get-Acl : Cannot find path '\\SERVER\Work_Block_A\MDX1112 - Xxxxxx Xxxxxxx 
      Xxxxxxxxxx' because it does not
      exist.
      At C:\Users\felipe.sa\Desktop\Script\NewProjectFolder\NewProject-WP_- 
      _ProductionV1.ps1:125 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_- 
      _ProductionV1.ps1:128 char:1
      + $acl.SetAccessRule($rule)
      + ~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
      

      有什么想法吗?

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-15
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多