Powershell Join-Path显示结果中的2个目录而不是1个-意外的脚本/函数输出 [英] Powershell Join-Path showing 2 dirs in result instead of 1 - accidental script/function output
问题描述
我正在构建增量目录结构,由于某种原因,Join-Path显示了2个目录.稍后,当我将其与要发送到复制项目的文件一起加入时,它会导致错误,如下所示.我在$ 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 '\\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脚本:
So this is the pertinent powershell script:
$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仅具有一个目录.现在,我认为它被视为数组,这是不正确的.
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.
更新
Update
这是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
}
返回的输出看起来不错.它将日期附加到\ T2 \ DisasterBackup \ Loc上,但调试器仅在其中显示一个目录,而不显示数组或2个单独的字符串.
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.
推荐答案
正确地推断出 T-Me 您发布了 CreateDatedFolder
源,问题是函数意外输出了 2 对象,而Join-Path
接受了数组到每个子路径的父路径.
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的隐式输出行为,该实例成为函数输出太-脚本/函数中返回未捕获或重定向的值的任何命令或表达式都将成为输出的一部分.
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
请注意,您永远不需要在PowerShell中 return
来输出结果-但您可能需要流程控制,以过早退出功能:
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屋!