【问题标题】:Powershell Join-Path showing 2 dirs in result instead of 1 - accidental script/function outputPowershell Join-Path 在结果中显示 2 个目录而不是 1 - 意外脚本/函数输出
【发布时间】:2019-07-30 15:28:54
【问题描述】:

我正在构建增量目录结构,出于某种原因Join-Path 显示了 2 个目录。当我稍后将它与我发送到 copy-item 的文件一起加入时,它会导致错误,如下所示。我已经在$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

所以这是相关的 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 
}
   

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

我尝试搜索相关问题,但没有看到类似的内容。

更新

这里是 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
}

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

【问题讨论】:

  • CreateDatedFolder 是一个函数吗?它有什么作用?在我看来,CreateDatedFolder 创建了两个存储在$toLocParentDT1 中的输出,Join-Path 处理它们。
  • @T-Me - 我将 CreateDatedFolder 添加到我的问题的更新中。它将日期附加到给定的文件夹中。输出只正确显示了一个文件夹,而不是两个。

标签: powershell return output return-value multiple-return-values


【解决方案1】:

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

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

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

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

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

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

请注意,你永远需要return在PowerShell中输出结果 - 但你可能需要它流控制,提前退出函数:

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 隐式输出行为的更多信息,请参阅this answer

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多