jborean93 / omi

Open Management Infrastructure
Other
108 stars 13 forks source link

Issue with cmdlet "Copy-Item" using remote PSSession between Linux and Windows #74

Closed InsideFR closed 5 months ago

InsideFR commented 5 months ago
SUMMARY

When I use the cmdlet "Copy-Item" using a PSSession, sometimes I get an error very regularly. It seems this cmdlet is not stable. You can use this code below executed from powershell 7.4 on GNU/Linux to reproduce the problem.

PowerShell script used :

#!/usr/bin/pwsh

#================================================================================================================
# Name : Copy-Item_Issue.ps1
# Usage : pwsh -c Copy-Item_Issue.ps1 $WINRM_IPV4 $WINRM_USERNAME $WINRM_USERPASS
# Version : 0.1
# Encodage : UTF-8 | Unix (LF)
#================================================================================================================

Param 
(
    [Parameter(Mandatory=$true,Position=1,HelpMessage="Renseignez l'adresse IPv4 du service WinRM distant")]
    [ValidateNotNullOrEmpty()]
    [string]$WINRM_IPV4,

    [Parameter(Mandatory=$true,Position=2,HelpMessage="Renseignez un compte de l'utilisateur du servie WinRM distant")]
    [ValidateNotNullOrEmpty()]
    [string]$WINRM_USERNAME,

    [Parameter(Mandatory=$true,Position=3,HelpMessage="Renseignez un mot de passe valide associé à l'utilisateur du service WinRM distant")]
    [ValidateNotNullOrEmpty()]
    [string]$WINRM_USERPASS 
)

#=========================================================================================================
# Manage variables
#=========================================================================================================
$WINRM_PORT = 5985
$LOCAL_PATH = Invoke-Expression "pwd"
$LOCAL_FILE = "$LOCAL_PATH/random_file-1.txt"
$REMOTE_PATH = 'C:'
$REMOTE_FILE = "$REMOTE_PATH\random_file-1.txt"

#=========================================================================================================
# Main
#=========================================================================================================

Try
{
    ### 01) Opening PSSession remote from Debian to Microsoft Windows computer (WS2019)

    # Prepare credential
    $WINRM_SECUREPASS = ConvertTo-SecureString $WINRM_USERPASS -AsPlainText -Force
    $WINRM_CREDENTIAL = New-Object System.Management.Automation.PSCredential($WINRM_USERNAME,$WINRM_SECUREPASS)

    # Create remote PSSession from Linux (Powershell 7.4.1) to Windows (WinRM)
    $WINRM_SESSION = New-PSSession -ComputerName $WINRM_IPV4 -Credential $WINRM_CREDENTIAL -Authentication Negotiate -ErrorAction Stop

    ### 02) Test file copy in PSSession

    # Loop to test stability of cmdlet "Copy-Item"
    For($INDEX=1;$INDEX -le 100;$INDEX++)
    {
        # LOG
        Write-Verbose "===== ROUND [$INDEX/100] =====" -Verbose

        # Copy file from Linux to Windows inside remote WinRM session
        Copy-Item -Path $LOCAL_FILE -Destination $REMOTE_FILE -ToSession $WINRM_SESSION -ErrorAction Stop
    }
}

Catch
{
    Write-Verbose "[ERROR]$_" -Verbose
}

File "random_file-1.txt" used (Endoding : UTF-8 with CRLF) :

m@VPCGycDgwzEJFEB6+UP9yDuUgl9tDoU/hN6crOM4JiSyMLKF9xdBgtQsuZAnkjTiWIC8OyPCi1Aq1/iLSDBfks64@OZJozUzFJTdjS8KZStv-zHyQ5MXZN@DSO-a
QfnZhb2sufB+M7xJ+L_PgA-z4/R8MqCMY8_TEKn9OpdLEMl8qEDgTv-pm4\kA-XeT6rU/UfGxca7wy\r2ZSh3v9C@VHQPdIUJ+8OuH\Mz@ZQvd949UySsPry/6XsnF
7uLwyxHx/u4gZ6Mth_2TY\V6C_YKSF1Uy-gFe=WMxLXe5l7C2UZ2GKY\PW-EP/WIJkY6ZhoippwnoRbrewZ=X=n4ml6zes@A5pLUEbTUFQNqnKX=UTzsoY1L/-TOr5
d@s/y+fx4dTeJhC@QSE8WC9VlhEaYKyfDRNQ@rmY-x6wPjpUi+ubjAjSfN1rLeQtVOS8L2+zFW@yMMgc818Ypw/cI_LBG_n5YMRbIxjwa_MDC1fJHZz0n8ezSPAQrP
9LIjRa_eKLRLYv1JRX6b-YMDS7IsA-@KfBcb\tkfPCp5jSjZOj@T0qVl=eI1ISVQNhGwsZNyN1DHJYuaXZVr\u06X8c71AN/YPrbl3H5MQg+3nTq1HdZ@EVtxRQBWX
NmnG0-Ht5-s6Vvo1cJp2guw4+t-=VLnfj5d-2xEZ0ITjHiZzeNOhm-Rt6AGHaJOuXnW7su=VfGcPSnx8KL-i/g9po1dC7/Yr-yCtC9g1O9bW8cSTcP/FPGxRuHTIqN
5iZ1Wt1dFSEjLdemxljBzsasTDb2+_pPKae-@hxy8+kubG\tNikHz6oMhGisr+eLD6PK+kpq51JziXrmHYo71ZpV3I2I=eg9\Q8m8CuC9_SPw5EBo/1J1n0UQ-YHzX
r1UQEJ5fufE3=6gRy@eAtctOiY6nM@U4dlU8_WA\MjSWf=p_O+L0D=EoHgIWTSJxBR03moWTNi4jBOIkJ_PNC_AeYFA5nVs_RuuEoCZzt9
mulu3-OymZd\_HqWeX47

Result 1 :

VERBOSE: ===== ROUND [1/100] =====
VERBOSE: ===== ROUND [2/100] =====
VERBOSE: ===== ROUND [3/100] =====
VERBOSE: ===== ROUND [4/100] =====
VERBOSE: [ERROR]Starting a command on the remote server failed with the following error message : Internal error parsing Wsman fault message For more information, see the about_Remote_Troubleshooting Help topic.

Result 2 :

VERBOSE: ===== ROUND [1/100] =====
VERBOSE: ===== ROUND [2/100] =====
VERBOSE: ===== ROUND [3/100] =====
VERBOSE: ===== ROUND [4/100] =====
VERBOSE: ===== ROUND [5/100] =====
VERBOSE: ===== ROUND [6/100] =====
VERBOSE: ===== ROUND [7/100] =====
VERBOSE: ===== ROUND [8/100] =====
VERBOSE: ===== ROUND [9/100] =====
VERBOSE: ===== ROUND [10/100] =====
VERBOSE: [ERROR]Processing data for a remote command failed with the following error message: ERROR_INTERNAL_ERROR: Une erreur interne s’est produite.  For more information, see the about_Remote_Troubleshooting Help topic.

Result 3 :

VERBOSE: ===== ROUND [1/100] =====
VERBOSE: ===== ROUND [2/100] =====
VERBOSE: ===== ROUND [3/100] =====
VERBOSE: ===== ROUND [4/100] =====
VERBOSE: ===== ROUND [5/100] =====
VERBOSE: ===== ROUND [6/100] =====
VERBOSE: ===== ROUND [7/100] =====
VERBOSE: ===== ROUND [8/100] =====
VERBOSE: [ERROR]Starting a command on the remote server failed with the following error message : Internal error parsing Wsman fault message For more information, see the about_Remote_Troubleshooting Help topic.

Resume : Sometimes the copy is working and sometimes it's not working (I get internal error from WinRM service of the remote computer). I tried to undestand the issue with wireshark analysing. I catched the last HTTP frame (http code 500 'Internal error' from remote computer) and when I decrypt the stream "application/http-spnego-session-encrypted, i saw the WSMan fault : "The input/output operation was aborted due to thread termination or application request". When I used the same powershell script between 2 windows computers always is OK, I managed to make 100 copies. I don't understand where is the issue, could you help me?

MODULE VERSION
ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.3.1                 PSWSMan                             {Disable-WSManCertVerification, Enable-WSManCertVerification, Get-WSManVersion, Install-WSMan…}
OS / ENVIRONMENT
**OS : Debian 10.13 (x86_64)
PowerShell : 7.4.1**

Output "hostnamectl"

Static hostname: CMT
Icon name: computer-vm
Chassis: vm
Machine ID: 3902e0b6fca2466e823f16b22847a7ed
Boot ID: 94c2ce60ad4d4f848f5b97b37c9fe6aa
Virtualization: microsoft
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-26-amd64
Architecture: x86-64

Output "pwsh -version" PowerShell 7.4.1

Output "Get-Host"

Name : ConsoleHost
Version : 7.4.1
InstanceId : a708d102-8524-4f5a-86f1-67c0a79258b9
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : fr-FR
CurrentUICulture : fr-FR
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
jborean93 commented 5 months ago

This fork is the best I can really do with the existing code, I can't really improve it any more without some massive changes and I have no plan to do so. Overall I'm not surprised that copying a file doesn't work, it does push the limits of the WinRM protocol.

InsideFR commented 5 months ago

Thank you Jordan for taking time to respond to me. I understand. Well done for your fork. Best regards.