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
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
}
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
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
}
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.