0

I have implemented a PS Script that deploys code on multiple servers at the same time. Here I need to copy some source file from one server to another. See the code below:

for ($i=1; $i -le 5; $i++) {
    $serverName="iwflO" + $i
    $sourceFile="\\iwdflO1\C$\Deploy\bin"
    $destination="\\$serverName\C$\Program Files (X86)\Shian\MyService\bin\"
    $Myblock = {
        Param{$sourceFile,$destination)
         Copy-Item -Force -Recurse $sourceFile -Destination $destination
    }
    $result = Invoke-Command -ComputerName $ServerName -Credential "shian" -ScriptBlock $Myblock -ArgumentList $sourceFile,$destination;
    $result;
}
cd c:\

It's working fine for iwflO1 which is the root server from where I'm running the script but for other servers it's giving me an error like

Cannot find Path "\iwdflO1\C$\Deploy\bin" because it does not exist.

But if I logged in to iwflO2 or any other server and hit the path manually its working fine.

Moerwald
  • 10,448
  • 9
  • 43
  • 83
Shian JA
  • 848
  • 4
  • 15
  • 52
  • 4
    This looks like the double-hop problem. Here's a [technet blog](https://blogs.technet.microsoft.com/ashleymcglone/2016/08/30/powershell-remoting-kerberos-double-hop-solved-securely/) with some work arounds. – BenH Feb 06 '17 at 16:02
  • Take a look at this and see if it is not similar http://stackoverflow.com/a/23625969/5233410 – Nkosi Feb 06 '17 at 16:12
  • how come thats a Double hop issue Ben? – Ranadip Dutta Feb 06 '17 at 16:29
  • 2
    @RanadipDutta localhost -> `Invoke-Command` -> first hop -> UNC path -> second hop – Ansgar Wiechers Feb 06 '17 at 17:06
  • I really do not think that its actually a Double hope logically. theoretically yes. Double hop normally means jumping to another computer from a remote computer(mostly with Invoke and pssession). Here its straight forward with admin shares. Anyways, I second you @AnsgarWiechers. :) – Ranadip Dutta Feb 06 '17 at 17:09
  • @AnsgarWiechers: But yes, that could be the issue what he is facing. – Ranadip Dutta Feb 06 '17 at 17:12

1 Answers1

0

I can see the mistake is with the block :

Instead of this:

$Myblock={param{$sourceFile,$destination)

copy-Item -Force -Recurse $sourceFile -Destination $destination
}

Do this:

$Myblock={param($sourceFile,$destination)

copy-Item -Force -Recurse $sourceFile -Destination $destination
}

This is working fine if I am hardcoding the server names(tested in my local)

Since you are using admin share, directly try this:

Copy-Item -Path \\serverA\c$\programs\temp\test.txt -Destination \\serverB\c$\programs\temp\test.txt;

Note: You have to specify the file. Else you get-childitem -recurse inside the source folder and put it directly in the destination .

Hope it helps.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • Thanks for reply !! Yeah m done with it but still facing the same issue – Shian JA Feb 06 '17 at 16:32
  • 1
    one more thing. `"\\iwdflO1\C$\Deploy\bin"` put a backslash. The location you are referring is underneath bin so `"\\iwdflO1\C$\Deploy\bin\"` That should solve your issue. PS is not able to pick the source path – Ranadip Dutta Feb 06 '17 at 16:35
  • it seems a logical thing. If you are using administrative share then why are you using invoke? Directly you can use copy-item right??. – Ranadip Dutta Feb 06 '17 at 16:38
  • 1
    @ShianJA: Made the change in the answer. Please check once and try to implement. You are not giving the file name as a result it is looking for a specific file and the result seems invalid path. – Ranadip Dutta Feb 06 '17 at 16:44
  • 3
    @ShianJA Please take a look at the link BenH provided in his comment to your question. Given the symptoms this is practically guaranteed to be a second-hop problem. – Ansgar Wiechers Feb 06 '17 at 17:04