I need to copy files over the network PSSession . ( Firewall / DMZ / Etc. )

Hello

I need to copy files over the network PSSession . ( Firewall / DMZ / Etc. )

I have a script where I copy from my local server ( server1) to the remote server ( server2 ), but I cant not make script that will copy from the remote server to my local by my session. From server2 to server1

Script is as below ...:-)

HELP : ....

-----------------------------------------------------
winrm s winrm/config/client '@{TrustedHosts="SERVER2"}'

    $Source = "D:\test\ok.log"
      
    $Destination = "D:\test\ok.log"
    
    $session = New-PSSession -ComputerName SERVER2

Set-StrictMode -Version Latest

## Get the source file, and then get its content
$sourcePath = (Resolve-Path $source).Path
$sourceBytes = [IO.File]::ReadAllBytes($sourcePath)
$streamChunks = @()

## Now break it into chunks to stream
Write-Progress -Activity "Sending $Source" -Status "Preparing file"
$streamSize = 1MB
for($position = 0; $position -lt $sourceBytes.Length;
    $position += $streamSize)
{
    $remaining = $sourceBytes.Length - $position
    $remaining = [Math]::Min($remaining, $streamSize)

    $nextChunk = New-Object byte[] $remaining
    [Array]::Copy($sourcebytes, $position, $nextChunk, 0, $remaining)
    $streamChunks += ,$nextChunk
}

$remoteScript = {
    param($destination, $length)

    ## Convert the destination path to a full filesytem path (to support
    ## relative paths)
    $Destination = $executionContext.SessionState.`
        Path.GetUnresolvedProviderPathFromPSPath($Destination)

    ## Create a new array to hold the file content
    $destBytes = New-Object byte[] $length
    $position = 0

    ## Go through the input, and fill in the new array of file content
    foreach($chunk in $input)
    {
        Write-Progress -Activity "Writing $Destination" `
            -Status "Sending file" `
            -PercentComplete ($position / $length * 100)

        [GC]::Collect()
        [Array]::Copy($chunk, 0, $destBytes, $position, $chunk.Length)
        $position += $chunk.Length
    }

    ## Write the content to the new file
    [IO.File]::WriteAllBytes($destination, $destBytes)

    ## Show the result
    Get-Item $destination
    [GC]::Collect()
}

## Stream the chunks into the remote script
$streamChunks | Invoke-Command -Session $session $remoteScript `
    -ArgumentList $destination,$sourceBytes.Length

    Remove-PSSession -Session $session


January 30th, 2015 10:45am

Is there some reson you don't just use a copy comand?

You fail to expalini what your error is.

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 11:02am

Hi Soren,

sounds to me as if you are experiencing the Second Hop Issue. In order to work around this, add this parameter to your New-PSSession call:

-Authentication Credssp

To read up on CredSSP (and figure out how to configure your systems to accept it), try Get-Help Enable-WSManCredSSP -Detailed.

jrv has a point about using Copy-Item though. Why not use it?

Cheers,
Fred

January 30th, 2015 12:29pm

I are not allow by firewall, I im allowed only through https powershell ( Firewall / DMZ / Etc. )
Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 12:31pm

But have will the script look,  if i need to copy from From server2 to server1.

?????????????

My script copy from server1 to server2 and working, but I need server2 to server1.



January 30th, 2015 1:02pm

I are not allow by firewall, I im allowed only through https powershell ( Firewall / DMZ / Etc. )

Right, I might have wanted to read the header.

Anyway, open a Remote Session with CredSSP Authentication on the remote system, and run the same script you are running locally on the destination system (opening a second hop remote session back on your local system).

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 1:09pm

My script copy from server1 to server2 and working, but I need server2 to server1. :-)
January 30th, 2015 1:29pm

Hi Soren,

so ... what is keeping you from transfering the script itself to the remote session (which you opened with CredSSP Authentication) and run it from there?

 

Or for that matter, why not do this even simpler:

Just run a scriptblock on the remote server that returns the bytes of the file you want? Then compile them back to file locally ...

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 1:54pm

Okidoki, i will try make it to run from the server2. But the best solution, will be to run from server2 to server1, Is a part a lot there are happing on server2.
January 30th, 2015 2:36pm

You still havent; told us what the error is.

I knew from your firet post that you ad connectivity so that was not the issue.   Either you are gettiing an error or the code is hanging.  Please explain.

Do you know that BITS cando this very easily and canbe easily triggered to move a file.

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 7:35pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics