简体   繁体   English

Powershell Join-Path 在结果中显示 2 个目录而不是 1 个 - 意外的脚本/函数输出

[英]Powershell Join-Path showing 2 dirs in result instead of 1 - accidental script/function output

I am constructing incremental directory structures, and for some reason Join-Path is showing 2 dirs.我正在构建增量目录结构,出于某种原因, Join-Path显示 2 个目录。 When I later join that with a file I'm sending to copy-item, it causes an error, as shown below.当我稍后将它与我发送到 copy-item 的文件连接时,它会导致错误,如下所示。 I have shown in the comment for the $to_loc_finalDT1 line, where I first see these two dirs:我在$to_loc_finalDT1行的评论中显示,我第一次看到这两个目录:

Copy-Item : Cannot find path '\\T2\DisasterBackup\Loc_2019-03-08\Privileges\Privileges_HH_Bak.csv \\T2\DisasterBackup\Loc_2019-03-08\Privileges\Privileges_HH_Bak.csv' because it does not exist

So this is the pertinent powershell script:所以这是相关的powershell脚本:

$T2 = "\\T2\DisasterBackup\Loc" 
$toLocParentDT2 = CreateDatedFolder $parentDirBaseNameDT2 
$to_loc_finalDT2 = Join-Path -Path $toLocParentDT2 -ChildPath "Privileges" 
#create sub-folder location 
if(-Not (Test-Path $to_loc_finalDT2 )) 
{
   write-output  " Creating folder $to_loc_finalDT2 because it does not exist " 
   New-Item -ItemType directory -Path $to_loc_finalDT2 -force 
}


#second dir save files to
$parentDirBaseNameDT1 = "\\T1\DisasterBackup\Loc" 
$toLocParentDT1 = CreateDatedFolder $parentDirBaseNameDT1 
$to_loc_finalDT1 = Join-Path -Path $toLocParentDT1 -ChildPath "Privileges" #shows 2 dirs here in debugger: \\T2\DisasterBackup\Loc_2019-03-08\Privileges \\T2\DisasterBackup\Loc_2019-03-08\Privileges
#create sub-folder location 
if(-Not (Test-Path $to_loc_finalDT1 )) 
{
   write-output  " Creating folder $to_loc_finalDT1 because it does not exist " 
   New-Item -ItemType directory -Path $to_loc_finalDT1 -force 
}
   

I'm not sure how to get Join-Path to just have the one dir, as it should.我不知道如何让Join-Path只拥有一个目录,因为它应该。 Right now, I think it's being treated as an array, which is not correct.现在,我认为它被视为一个数组,这是不正确的。

I tried searching for related issues, but didn't see anything similar.我尝试搜索相关问题,但没有看到类似的内容。

Update更新

Here's the code for CreateDatedFolder:这是 CreateDatedFolder 的代码:

#create dated folder to put backup files in 
function CreateDatedFolder([string]$name){
   $datedDir = ""
   $datedDir = "$name" + "_" + "$((Get-Date).ToString('yyyy-MM-dd'))"
   New-Item -ItemType Directory -Path $datedDir -force
   return $datedDir
}

The output for that looks fine when it's returned.当它返回时,它的输出看起来很好。 It appends the date onto the \\T2\\DisasterBackup\\Loc, but the debugger only shows one dir there, not an array or 2 dirs that are separate strings.它将日期附加到 \\T2\\DisasterBackup\\Loc,但调试器只在那里显示一个目录,而不是一个数组或 2 个作为单独字符串的目录。

As T-Me correctly inferred before you posted the CreateDatedFolder source, the problem is that the function inadvertently outputs 2 objects , and Join-Path accepts an array of parent paths to each join with the child path.正如T-Me在您发布CreateDatedFolder源代码之前正确推断的那样,问题在于该函数无意中输出了2 个对象,而Join-Path接受一个父路径数组,每个连接都与子路径。

Specifically, it is the New-Item call that accidentally creates an additional output object, just before your return $datedDir call.具体来说,正是New-Item调用意外地创建了一个额外的输出对象,就在您return $datedDir调用之前。

New-Item outputs a [System.IO.DirectoryInfo] instance representing the newly created directory and, due to PowerShell's implicit output behavior , that instance becomes part of what the function outputs too - any command or expression inside a script / function that returns a value that isn't captured or redirected becomes part of the output . New-Item输出[System.IO.DirectoryInfo]实例代表新创建的目录,而且由于PowerShell的隐式输出行为,该实例的功能输出什么样的一部分-一个脚本/函数中的任何命令或表达式返回一个未被捕获或重定向的值成为输出的一部分

To prevent that, suppress the output:为了防止这种情况,请抑制输出:

$null = New-Item -ItemType Directory -Path $datedDir -force

Other ways to suppress output are discussed in this answer , which also discusses the design rationale for PowerShell's implicit output behavior.此答案中讨论了其他抑制输出的方法,其中还讨论了 PowerShell 隐式输出行为的设计原理

Note that you never need return in PowerShell in order to output a result - but you may need it for flow control , to exit a function prematurely:请注意,您永远不需要在 PowerShell 中return输出结果- 但您可能需要它用于流量控制,以过早退出函数:

return $datedDir 

is syntactic sugar for:是语法糖:

$datedDir # Implicitly output the value of $datedDir.
          # While you could also use `Write-Output $datedDir`,
          # that is rarely needed and actually slows things down.
return    # return from the function - flow control only

For more information about PowerShell's implicit output behavior, see this answer .有关 PowerShell 的隐式输出行为的更多信息,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM