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

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:

$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:

$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!

You’re running into the so called double hop issue.

Please read the following help topics:

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

1 Like

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

$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?

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

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 …

$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

Olaf,
That worked, awesome!

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

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

What is that 1st $ doing?

… search for subexpression operator

I spoke to soon .

When I run this code:

$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 :frowning:

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

The error I get is:

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.

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:

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

I finally got this working like this:

$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!

Great. I’m glad to hear. :+1:t4: I’ll answer to some of your questions anyway. :wink:

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

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

Yes, there is.

Please always read the help including the examples for the cmdlets you’re about to use. :point_up_2:t4:

The error complains about Copy-Item - not New-Item :thinking:

Usually we do something like this this way:

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