Powershell Join-Path 在结果中显示 2 个目录而不是 1 - 意外脚本/函数输出 [英] Powershell Join-Path showing 2 dirs in result instead of 1 - accidental script/function output

查看:14
本文介绍了Powershell Join-Path 在结果中显示 2 个目录而不是 1 - 意外脚本/函数输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建增量目录结构,出于某种原因,Join-Path 显示了 2 个目录.当我稍后将它与我发送到 copy-item 的文件一起加入时,它会导致错误,如下所示.我已经在 $to_loc_finalDT1 行的注释中显示了,我第一次看到这两个目录:

I am constructing incremental directory structures, and for some reason Join-Path is showing 2 dirs. When I later join that with a file I'm sending to copy-item, it causes an error, as shown below. I have shown in the comment for the $to_loc_finalDT1 line, where I first see these two dirs:

Copy-Item : Cannot find path '\T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv \T2DisasterBackupLoc_2019-03-08PrivilegesPrivileges_HH_Bak.csv' because it does not exist

所以这是相关的 powershell 脚本:

So this is the pertinent powershell script:

$T2 = "\T2DisasterBackupLoc" 
$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 = "\T1DisasterBackupLoc" 
$toLocParentDT1 = CreateDatedFolder $parentDirBaseNameDT1 
$to_loc_finalDT1 = Join-Path -Path $toLocParentDT1 -ChildPath "Privileges" #shows 2 dirs here in debugger: \T2DisasterBackupLoc_2019-03-08Privileges \T2DisasterBackupLoc_2019-03-08Privileges
#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 
}
   

我不确定如何让 Join-Path 只拥有一个目录,因为它应该.现在,我认为它被视为一个数组,这是不正确的.

I'm not sure how to get Join-Path to just have the one dir, as it should. 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.

更新

这里是 CreateDatedFolder 的代码:

Here's the code for 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
}

它的输出在返回时看起来很好.它将日期附加到 T2DisasterBackupLoc 上,但调试器只在那里显示一个目录,而不是一个数组或 2 个单独字符串的目录.

The output for that looks fine when it's returned. It appends the date onto the T2DisasterBackupLoc, but the debugger only shows one dir there, not an array or 2 dirs that are separate strings.

推荐答案

正如之前正确推断的 T-Me您发布了 CreateDatedFolder 源,问题是函数无意中输出了 2 个对象,并且 Join-Path 接受一个父路径的array 来连接每个子路径.

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.

具体来说,是 New-Item 调用在您的 return $datedDir 调用之前意外创建了一个附加 输出对象.

Specifically, it is the New-Item call that accidentally creates an additional output object, just before your return $datedDir call.

New-Item 输出一个代表新创建目录的 [System.IO.DirectoryInfo] 实例,并且由于 PowerShell 的 implicit 输出行为,该实例成为函数输出的一部分 - 脚本/函数中返回未捕获或重定向的值的任何命令或表达式变成部分输出.

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.

为了防止这种情况,抑制输出:

To prevent that, suppress the output:

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

this answer中讨论了其他抑制输出的方法,其中还讨论了设计原理 用于 PowerShell 的隐式输出行为.

Other ways to suppress output are discussed in this answer, which also discusses the design rationale for PowerShell's implicit output behavior.

请注意,您永远需要 return 在 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:

return $datedDir 

是语法糖:

$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

有关 PowerShell 隐式输出行为的更多信息,请参阅此答案.

For more information about PowerShell's implicit output behavior, see this answer.

这篇关于Powershell Join-Path 在结果中显示 2 个目录而不是 1 - 意外脚本/函数输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆