2022-01-21

"Get ChildItem : Cannot find path" showing path I did not supply

I inherited a script that should simply move files from -source to -target. I am prompted for both, and after supplying the paths, it tells me that it cannot find the path while showing a path that I absolutely did not submit, but can't figure out how it's arriving there.

    [Parameter(
        Mandatory = $true,
        Position = 0,
        HelpMessage = "Root of the folders or share to archive"
    )]
    [String] $source,
 
    [Parameter(
        Mandatory = $true,
        Position = 1,
        HelpMessage = "Path of the folder or share of archive"
    )]
    [string] $target,
 
    [Parameter(
        Mandatory = $false,
        Position = 3
    )]
    [int] $days = 30
)
 
# Get all the files from the source path, that are not shortcuts (*.lnk) and older than the days set
Get-ChildItem $source -Recurse |  
        Where-Object {!$_.psiscontainer -and ((get-date) - $_.lastwritetime).totaldays -gt $days -and $_.extension -ne ".lnk"} |
            ForEach-Object {

# For each file build the destination path 
                $dest = $_.fullname -replace ([regex]::escape($source)), $target
 
# Move the files into the destination
                Move-Item -Path $_.fullname -Destination $dest -ErrorAction silentlycontinue
}

The log says "Cannot find path '\\appserver\abc$\Automation\Daily\Archive\appserver\abc$\Storage' because it does not exist" - see how it starts repeating itself? \\appserver\abc$\Automation\Daily\Archive\ is the location of the script, whereas \\appserver\abc$\Storage\ is what I am entering as -source. So I have no idea why it is looking at the path to the script, then appending the source path concurrently.

EDIT: This is how I am calling the script (from within a different application):

SHELL PowerShell \\appserver\abc$\Automation\Daily\Archive\ArchiveFiles.ps1 -source \\appserver\abc$\Dataport\dpdata -target \\appserver\abc$\Dataport\archived -days 30



from Recent Questions - Stack Overflow https://ift.tt/3KvSOag
https://ift.tt/eA8V8J

No comments:

Post a Comment