# Script works when run locally, but no when I try and run remotely

**URL:** <https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918>\
**Category:** PowerShell Help\
**Created:** [July 18, 2022, 7:13pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918 "2022-07-18T19:13:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [July 18, 2022, 7:13pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/1 "2022-07-18T19:13:56Z")

</div>

Hello Folks,  
I am developing a script to help server builds. When I run this snippet locally on the server, it works, it copies the files, no questions asked:

`Copy-Item "\\servername\e$\ECI_Server_Builds\*" -Destination "c:\temp\"`

However, I am trying to call that script(which does a bunch of other things) from this script:

```auto
$ComputerName = Read-Host -Prompt "Please enter the New system you want to run ECI script on"
Invoke-Command -filepath c:\tools\scripts\final\ECI-Additional-Setting_v7.ps1 -ComputerName $ComputerName

```

The bunch of other things seem to work, but that file copy just does not happen. Ive tried using this:

```auto
$hostname = hostname
$Session = New-PSSession -ComputerName $hostname -Credential "xxxx\xxxxxxx"

```

It prompts me for my creds, but still the copy does not happen.

Any ideas why?

Thanks!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 18, 2022, 7:25pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/2 "2022-07-18T19:25:48Z")

</div>

You’re running into the so called _double hop issue_.

Please read the following help topics:

> **[Making the second hop in PowerShell Remoting - PowerShell](https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-7.2)**
>
> This article explains the various methods for configuring second-hop authentication for PowerShell remoting, including the security implications and recommendations.

[https://4sysops.com/archives/solve-the-powershell-multi-hop-problem-without-using-credssp/](https://4sysops.com/archives/solve-the-powershell-multi-hop-problem-without-using-credssp/)

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [July 18, 2022, 9:02pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/3 "2022-07-18T21:02:48Z")

</div>

Yes I have read about the double hop issue. I thought passing in my creds would do it like this:

```auto
$cred = Get-Credential domain\username
$ComputerName = Read-Host -Prompt "Please enter the New system you want to run ECI script on"
Invoke-Command -ComputerName $ComputerName -credential $cred -filepath c:\tools\scripts\final\ECI-Additional-Setting_v7.ps1

```

But not working. I do not have a big security concern, just want to get the files copied. I am having trouble implementing CredSSP.

How would I do this?

Pass credentials inside an `Invoke-Command` script block

I use an Invoke command, but not using a Scriptblock. Can you have an empty Scriptblock?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 18, 2022, 9:29pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/4 "2022-07-18T21:29:30Z")

</div>

> [@matt1974](#):
>
> I thought passing in my creds would do it

No. It does not. The best way to circumvent the double hop issue is to avoid it. 😉

When you run this code from another computer anyway you can copy the needed files to the remote computer by using an UNC path.

Something similar to this should work …

```auto
$ComputerName = Read-Host -Prompt "Please enter the New system you want to run ECI script on"
Copy-Item '\\servername\e$\ECI_Server_Builds\*' -Destination "\\$($ComputerName)\c$\temp\"
invoke-Command -FilePath 'c:\tools\scripts\final\ECI-Additional-Setting_v7.ps1' -ComputerName $ComputerName

```

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [July 18, 2022, 9:47pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/5 "2022-07-18T21:47:34Z")

</div>

Olaf,  
That worked, awesome!

Can you shed some light on how this UNC is constructed?

`\\$($ComputerName)\c$\temp\`

What is that 1st $ doing?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 18, 2022, 9:56pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/6 "2022-07-18T21:56:50Z")

</div>

… search for subexpression operator

> **[about Operators - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.2#subexpression-operator--)**
>
> Describes the operators that are supported by PowerShell.

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [July 18, 2022, 10:54pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/7 "2022-07-18T22:54:02Z")

</div>

I spoke to soon .

When I run this code:

```auto
$ComputerName = Read-Host -Prompt "Please enter the New system you want to run ECI script on"
#New-Item -Path "C:\Temp" -Name "Agent v10.1.43" -ItemType "Directory"
#New-Item -Path "C:\Temp" -Name "certs" -ItemType "Directory"
Copy-Item '\\servername\e$\ECI_Server_Builds\certs\*' -Destination "\\$($ComputerName)\c$\temp\certs"

Copy-Item '\\servername\e$\ECI_Server_Builds\Agent_v10.1.43\amp*' -Destination "\\$($ComputerName)\c$\temp\Agent_v10.1.43\"
Invoke-Command -filepath c:\tools\scripts\final\ECI-Additional-Setting_v7.ps1 -ComputerName $ComputerName

```

I end up with just a file called certs in the destination directory. I was aiming for a folder called certs, and the 2 certs in it, with another dir called Agentxx with the agent in there.

There is no -Recursive switch for Copy-item ☹

I tried creating those dirs 1st, but thats not working either.

The error I get is:

```auto
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
At C:\tools\scripts\Final\Remote-execution_eci_additional_settings.ps1:5 char:1
+ Copy-Item '\\servername\e$\ECI_Server_Builds\Agent_v10.1.43\amp*' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemComm

```

I was thinking of a different approach using Get-ChildItem…but then Im not sure how to sue that with Invoke-Command.

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [July 18, 2022, 11:07pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/8 "2022-07-18T23:07:53Z")

</div>

It seems like in my testing, that if I have the folders already created on the destination, then it works as expected and copies the files.

My challenge now is how do I get those folders created In my first script?

I need to get this executed on a remote computer…I think:

```auto
#New-Item -Path "C:\Temp" -Name "Agent v10.1.43" -ItemType "Directory"
#New-Item -Path "C:\Temp" -Name "certs" -ItemType "Directory"

```

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [July 18, 2022, 11:50pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/9 "2022-07-18T23:50:00Z")

</div>

I finally got this working like this:

```auto
$ComputerName = Read-Host -Prompt "Please enter the New system you want to run ECI script on"

Invoke-Command $ComputerName -ScriptBlock {
        $setupfolder = "C:\temp\Agent_v10.1.43\"
        $setupfolder1 = "C:\temp\certs\"
        New-Item -Path $setupfolder -type directory -Force
        New-Item -Path $setupfolder1 -type directory -Force
        }

Copy-Item '\\servername\e$\ECI_Server_Builds\certs\*' -Destination "\\$($ComputerName)\c$\temp\certs"
Copy-Item '\\servername\e$\ECI_Server_Builds\Agent_v10.1.43\amp*' -Destination "\\$($ComputerName)\c$\temp\Agent_v10.1.43\"
Invoke-Command -filepath c:\tools\scripts\final\ECI-Additional-Setting_v7.ps1 -ComputerName $ComputerName

```

Olaf - Thanks for your help as usual!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 19, 2022, 7:20am UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/10 "2022-07-19T07:20:20Z")

</div>

> [@matt1974](#):
>
> I finally got this working like this:

Great. I’m glad to hear. 👍🏽 I’ll answer to some of your questions anyway. 😉

> [@matt1974](#):
>
> I was aiming for a folder called certs, and the 2 certs in it, with another dir called Agentxx with the agent in there.

If you want to copy a folder you should provide the path of the folder - not the path of something inside it. 😉 So it should be …

```auto
Copy-Item '\\servername\e$\ECI_Server_Builds\certs' -Destination "\\$($ComputerName)\c$\temp -Recurse"

```

> [@matt1974](#):
>
> There is no -Recursive switch for Copy-item ☹

Yes, there is.

> **[Copy-Item (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.2)**
>
> The Copy-Item cmdlet copies an item from one location to another location in the same namespace. For instance, it can copy a file to a folder, but it can't copy a file to a certificate drive. This cmdlet doesn't cut or delete the items being copied....

Please always read the help including the examples for the cmdlets you’re about to use. 👆🏽

> [@matt1974](#):
>
> I tried creating those dirs 1st, but thats not working either.
> 
> The error I get is:

The error complains about `Copy-Item` - not `New-Item`🤔

> [@matt1974](#):
>
> It seems like in my testing, that if I have the folders already created on the destination, then it works as expected and copies the files.
> 
> My challenge now is how do I get those folders created In my first script?

Usually we do something like this this way:

```auto
$FolderPath = "\\$($ComputerName)\c$\temp\certs"
if (-not (Test-Path -Path $FolderPath)) {
    New-Item -Path $FolderPath -ItemType Directory | Out-Null
}

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/script-works-when-run-locally-but-no-when-i-try-and-run-remotely/19918/11 "2024-05-16T20:17:44Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


