Powershell 连接网络路径+;变量(文件夹的名称)

Powershell 连接网络路径+;变量(文件夹的名称),powershell,cmd,directory,Powershell,Cmd,Directory,如何将此网络路径与用户输入的此变量连接(它将是完整的网络路径) 因此,用户键入新文件夹名称,例如:folder-123(将存储在变量$pjname中) 存储在$pjname中的这个新文件夹应该有一个类似\\\SERVER\Work-3rd\+文件夹名的网络路径。例如\\\SERVER\Word-3rd\Folder-123 PowerShell找不到新文件夹的最终路径,因此未对其应用权限 我在一个测试区域进行尝试,发现以下问题: Folder has been renamed. Press a

如何将此网络路径与用户输入的此变量连接(它将是完整的网络路径)

因此,用户键入新文件夹名称,例如:folder-123(将存储在变量$pjname中)

存储在$pjname中的这个新文件夹应该有一个类似
\\\SERVER\Work-3rd\+文件夹名
的网络路径。例如
\\\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

您正在尝试在该共享路径中创建新目录,还是重命名目录?看起来您正在尝试重命名目录

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

$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

无论何时连接路径,特别是由最终用户提供的路径,都要坚持使用一个可以完成大部分繁重工作的实用程序。使用as字符串连接有几个陷阱,必须避免这些陷阱

[io.path]::combine('\\server\share', 'newfolder')
combine方法将路径部分作为一个数组,并构建正确的路径。请注意,如果路径存在,则不会验证此操作。它可以很好地处理拖尾路径分隔符。接下来的这些命令产生相同的结果

[io.path]::combine('\\server\share\', 'newfolder')
[io.path]::combine('\\server\share', 'newfolder') 
但请注意引导路径分隔符:

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


谢谢Matt&oze4!现在发生了一个奇怪的问题。我使用了oze4的溶液,有时有效,有时无效。是用户输入的字符串吗

当它工作时,文件夹名称为'MDX1111-XXXX Xxxxxxx xxxxxxxx'-32个字符。我使用文件夹名'MDX1112-Xxxxxx-Xxxxxx-xxxxxxxxx'-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
有什么想法吗


谢谢。

请查看cmdlet
Join-Path
Test-Path
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