Get-Childitem using shared folders

373 views Asked by At

I have a script to copy some files and child items from a network computer to a shared drive and I am using this:

#(ComputerName) Not real name, for security reasons.
#(ParentFolderName) Not real name, for security reasons.
#(IPAddress) Not real name, for security reasons.

$Source = "\\(ComputerName)\measurements\"
$Destination = "G:\Shared drives\(ParentFolderName)\TSTATION-BCKUP\K4\KNA01788\"

Get-ChildItem $Source -Recurse | ForEach {
    $ModifiedDestination = $($_.FullName).Replace("$Source","$Destination")
    If ((Test-Path $ModifiedDestination) -eq $False) {
        Copy-Item $_.FullName $ModifiedDestination
        }
    }

And it is working perfectly, but when I replace the $Source line for:

$Source = "\\(IPAddress)\data\measurements\"

It got the next error:

Get-ChildItem : Cannot find path '\\10.1.x.x\data\m' because it does not exist.
At C:\Users\us1adavila\Documents\Scripts\Batchs\TStation Backup\TStationBackup - US250216.ps1:4 char:1
+ Get-ChildItem "\\10.1.82.112\data\measurements\" -Recurse | ForEach {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\10.1.82.112\data\m:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Any suggestion?

If I Ping the ip address it works. I tied to add: Get-ChildItem $Source -Recurse -literalpath, but it does not work.

1

There are 1 answers

0
stim-ca On

I think the solution will be path validation. What you have provided has two different source directories. One with data and one without.

$Source = "\\(ComputerName)\measurements\"
$Source = "\\(IPAddress)\data\measurements\"

Open up a PowerShell console and try:

Test-Path '\\(ComputerName)\measurements\'
Test-Path '\\(ComputerName)\data\measurements\'
Test-Path '\\(IPAddress)\measurements\'
Test-Path '\\(IPAddress)\data\measurements\'

You'll get a result of true or false.

You could also just use the gui to verify that path as well.

You might also want to remove your PII from the error log.

Please update the thread with the solution.