Rundeck powershell robocopy : the hell ;-)

92 views
Skip to first unread message

ledufAkaDemy ledufAkaDemy

unread,
Oct 24, 2021, 4:32:38 PM10/24/21
to rundeck-discuss
Script is working , doing the job ... !!!
Robocopy is mirroring ...
BUT .. i c'an t achieve to get error  code working properly .
So always "Failed" result  :-(
(Node executor = Linux, node target windows)

here is the script :

#ROBOCOPY AMC - Rundeck

$source='"\\srv1\Modèles de documents"'
$dest='"\\nassyno1\AMC-FRANCE\Modèles de documents"'

$what = @("/MIR")
$options = @("/NFL","/NDL","/nc","/ns","/np","/R:3","/W:3")
$cmdArgs = @($source,$dest,$what,$options)
#Write-Host "Args="@cmdArgs

robocopy @cmdArgs
if ($lastexitcode -gt 7)
{
      #write-host "Robocopy failed with exit code:" $lastexitcode
      #write-host "See : https://ss64.com/nt/robocopy-exit.html"
      #exit $LastExitCode
      exit 1
}


Do you have any idea ?

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 7:57:53 AM10/25/21
to rundeck-discuss
Rundeck is very strange ...  the robocopy do the job, but Rundeck is unable to manage exit code or i don't know:
i try this.

Rundeck call : (

$SourceUNC=$args[0]
$DestinationUNC=$args[1]
$Command="C:\Windows\system32\robocopy.exe"
$Arguments=@("/MIR","/NFL","/NDL","/NJH","/NJS","/nc","/ns","/np","/R:2","/W:3")

Start-Sleep -s 2

Write-Host "$Command ""$SourceUNC"" ""$DestinationUNC"" $Arguments"
$LastExitCode=0

#robocopy.exe "$SourceUNC" "$DestinationUNC" $Arguments | Out-Null
& $Command "$SourceUNC" "$DestinationUNC" $Arguments
$returnCode=$LastExitCode
if ($returnCode -gt 7)
{
      Write-Host "Robocopy failed with exit code: " $returnCode
      write-Host "See : https://ss64.com/nt/robocopy-exit.html"
      #[system.environment]::Exit($returnCode)
      exit $returnCode
}
else
{
      Write-Host "Robocopy Ok: " $returnCode
      #[system.environment]::Exit(0)
      exit 0
}



      <command>
        <description>srv1 POWERSHELL (script file)</description>
        <fileExtension>ps1</fileExtension>
             <scriptargs>'\\srv1\Modèles de documents' '\\nassyno1\AMC-FRANCE\Modèles de documents"'</scriptargs>
            <scriptfile>/var/lib/rundeck/projects/AD/scripts/robocopy-rundeck-v2.ps1</scriptfile>
      </command>

Exit Script Behaviou is enable.

What must i must do again ?

rac...@rundeck.com

unread,
Oct 25, 2021, 8:12:33 AM10/25/21
to rundeck-discuss
Hi,

That's is an inline script based job? Or you're calling a script from the command step? Could you share a basic reproducible example to test? Rundeck version? You're using the pywinrm plugin to connect to a windows node?

Greetings.

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 9:59:43 AM10/25/21
to rundeck-discuss
hello rac ...@

Rundeck is running on linux debian 11 server :
- Linux node : working cool with Ansible, all good.
- Managing Windows remote Node (WinRM cerdssp etc) .... just weird things

i'm trying to integrated Robocopy in powershell code for Rundeck (please note that this was done before with Planned sheduled task of windows).

That's is an inline script based job?  : no, "Script file or URL - Execute a local script file or a script from a URL"
Or you're calling a script from the command step? no from Script File, defined with two args  and file extension .ps1
Could you share a basic reproducible example to test?
Node def.  (srvfile1 is the node where we launch robocopy to hunded synlogy NAS , smb share etc):

<node name="srvfiler1 description="Serveur Fichiers" tags="filer" osFamily="windows" osArch="amd64" osName="Windows Server 2016 Standard" hostname="srvfile1" username="svcrundecknode" file-copier-add-utf8-bom="true"/>

Node step :
     <command>
        <description>srv1 POWERSHELL (script file)</description>
        <fileExtension>ps1</fileExtension>
             <scriptargs>'\\srv1\Modèles de documents' '\\nassyno1\AMC-FRANCE\Modèles de documents"'</scriptargs>
            <scriptfile>/var/lib/rundeck/projects/AD/scripts/robocopy-rundeck-v2.ps1</scriptfile>
      </command>

i'm stucking with this code , i can't achieve to have exit code working in Rundeck : $lastexitcode is always 0 ... so always succes !!!
But robocopy is doing the job : this is weird.

Rundeck version? the laste one 3.4.5,
You're using the pywinrm plugin to connect to a windows node? yes, buitl in .WinrRM node executor Python.


New : Code

# Arguments
$SourceUNC=$args[0]
$DestinationUNC=$args[1]

# Constant

$Command="C:\Windows\system32\robocopy.exe"
$Arguments=@("/MIR","/NFL","/NDL","/nc","/ns","/np","/R:2","/W:3")
$RundeckLogPath="C:\Rundeck\Log"

# log
New-Item -ItemType Directory -Force -Path $RundeckLogPath |Out-Null
$DestURI=New-Object System.Uri($DestinationUNC)
$DestHost=$DestURI.Host
$LogFile="$RundeckLogPath\Robocopy-$DestHost.log"

# Display for Rundeck

Write-Host "$Command ""$SourceUNC"" ""$DestinationUNC"" $Arguments"

# Running
$LastExitCode=0
#& $Command "$SourceUNC" "$DestinationUNC" $Arguments /LOG:$LogFile
robocopy.exe "$SourceUNC" "$DestinationUNC" $Arguments /LOG:$LogFile
$CommandRetCode=$LastExitCode

# Erro management
if ($CommandRetCode -gt 7)
{
      Write-Host "Robocopy failed with exit code: " $CommandRetCode
      write-Host "See : https://ss64.com/nt/robocopy-exit.html"
      exit $CommandRetCode
}
else
{
      Write-Host "Robocopy Ok: " $CommandRetCode  #=====>>>> here we have a problem , always returning 0 ...even exitcode is 0 , 1 or 2 or 3 !!!
      exit 0
}

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 10:08:52 AM10/25/21
to rundeck-discuss
Responding to myself ;-)

the issue seems to be :

$LastExitCode=0 ...this is not working properly ,

need this instead :
$global:LASTEXITCODE = $null


cheers !!

But if someone can critisize the code below , he is welcome.

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 11:11:13 AM10/25/21
to rundeck-discuss
pfffffffffff : WinRM , ....

Now the problem is with Synology NAS .... no issue with Windows fiel server (remote) ... but issue with synology , slow link.

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 11:15:05 AM10/25/21
to rundeck-discuss
each step of a worklfow are not working identicaly  with WinRM ...
 try to go production : 108 steps , launch step by step the script  ....
- Ok (windows remote fiel server)
- Failed .... (synology nas server)

this is really boring an painfull

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 11:37:49 AM10/25/21
to rundeck-discuss
Now Rundeck is stucl on this third step ...


[DEBUG  ]  Starting server public key response verification (credssp.py:152)[requests_credssp.credssp]
[DEBUG  ]  SPNEGO step input: oRswGaADCgEAoxIEEAEAAACi2k4HCQ+3RQAAAAA= (_negotiate.py:109)[spnego._negotiate]
[DEBUG  ]  SPNEGO step output:  (_negotiate.py:135)[spnego._negotiate]
[DEBUG  ]  Sending encrypted credentials (credssp.py:165)[requests_credssp.credssp]
-------------------------------------------------------------------------------
   ROBOCOPY   ::   Copie de fichiers robuste pour Windows     
-------------------------------------------------------------------------------



[WinRMPython]: result code: 1, success: false
[WinRMPython] execCommand started, command: del C:\WINDOWS\TEMP\442-1989-svfic1-robocopy-rundeck.ps1.PS1
[WinRMPython] executing: [[${config.interpreter}, -u, /var/lib/rundeck/libext/cache/py-winrm-plugin-2.0.14/winrm-exec.py, ${node.hostname}]]

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 11:41:35 AM10/25/21
to rundeck-discuss
Linux rundeck side

everything is installed :

Requirement already satisfied: pywinrm[credssp] in /usr/lib/python3/dist-packages (0.3.0)
Requirement already satisfied: requests-credssp>=0.0.1 in /home/gestsvcansible/.local/lib/python3.9/site-packages (from pywinrm[credssp]) (1.2.0)
Requirement already satisfied: pyasn1>=0.3.1 in /home/gestsvcansible/.local/lib/python3.9/site-packages (from requests-credssp>=0.0.1->pywinrm[credssp]) (0.4.8)
Requirement already satisfied: pyOpenSSL>=16.0.0 in /home/gestsvcansible/.local/lib/python3.9/site-packages (from requests-credssp>=0.0.1->pywinrm[credssp]) (21.0.0)
Requirement already satisfied: requests>=2.0.0 in /usr/lib/python3/dist-packages (from requests-credssp>=0.0.1->pywinrm[credssp]) (2.25.1)
Requirement already satisfied: cryptography in /usr/lib/python3/dist-packages (from requests-credssp>=0.0.1->pywinrm[credssp]) (3.3.2)
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from requests-credssp>=0.0.1->pywinrm[credssp]) (1.16.0)
Requirement already satisfied: pyspnego in /home/gestsvcansible/.local/lib/python3.9/site-packages (from requests-credssp>=0.0.1->pywinrm[credssp]) (0.2.0)

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 11:43:46 AM10/25/21
to rundeck-discuss
Launch job in DEBUG  is absoletly unsefull : error or not always the same thing ...

rac...@rundeck.com

unread,
Oct 25, 2021, 12:29:49 PM10/25/21
to rundeck-discuss
Hi,

Which errors are you seeing in the log output?

Regards.

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 1:03:19 PM10/25/21
to rundeck-discuss
always the same , there is only one ! lol.

NonZeroResultCode: [WinRMPython] Result code: 1

rac...@rundeck.com

unread,
Oct 25, 2021, 1:11:17 PM10/25/21
to rundeck-discuss
Have you defined the python3 path/executable on the Project Default node executor? Project Settings > Edit Configuration > Default Node Executor > Python Interpreter textbox (available selecting  "WinRM Node Executor Python"), same for "Default File Copier" tab.

rac...@rundeck.com

unread,
Oct 25, 2021, 1:16:05 PM10/25/21
to rundeck-discuss

Also, define powershell.exe in “Invocation String” and .ps1 in “File Extension” (these options are available by clicking on the script step “Advanced” button).

ledufAkaDemy ledufAkaDemy

unread,
Oct 25, 2021, 4:05:09 PM10/25/21
to rundeck-discuss
yes : /usr/bin/python3 (debian 11)

rac...@rundeck.com

unread,
Oct 25, 2021, 4:51:11 PM10/25/21
to rundeck-discuss
According to discussed in the IRC, you're facing a Powershell double-hop issue, take a look at this, this, and this. That's quite difficult on the Pywinrm plugin.

Also, take a look at this.

Greetings.

ledufAkaDemy ledufAkaDemy

unread,
Oct 31, 2021, 1:16:59 PM10/31/21
to rundeck-discuss
nope, this is not a double hope issue. Because script (robocopy is doing the job)
After 5 days of searching i found the solution.


the invocation string need absolutely -"file", if not there is issue with accent or space in arguments .

PowerShell.exe -ExecutionPolicy ByPass -File


And here is the final script which is working perfectly :

$SourceUNC=$args[0]
$DestinationUNC=$args[1]

#Write-Host "SRC="$SourceUNC
#Write-Host "DST="$DestinationUNC

# Constants

$Command="C:\Windows\system32\robocopy.exe"
$Arguments=@("/MIR","/NFL","/NDL","/NP","/NS","/NC","/R:2","/W:3")

$RundeckLogPath="C:\Rundeck\Log"

# log
New-Item -ItemType Directory -Force -Path $RundeckLogPath |Out-Null
$DestURI=New-Object System.Uri($DestinationUNC)
$DestHost=$DestURI.Host
$LogFile="$RundeckLogPath\Robocopy-$DestHost.log"

# Display for Rundeck
Write-Host "$Command ""$SourceUNC"" ""$DestinationUNC"" $Arguments"

# Execution command
#$global:LASTEXITCODE = $null

robocopy.exe "$SourceUNC" "$DestinationUNC" $Arguments /LOG:$LogFile
$CommandRetCode=$LastExitCode

# Error management

if ($CommandRetCode -gt 7)
{
      Write-Host "Robocopy failed with exit code: " $CommandRetCode
      write-Host "See : https://ss64.com/nt/robocopy-exit.html"
      exit $CommandRetCode
}
else
{
      Write-Host "Robocopy Ok: " $CommandRetCode
      write-Host "See : https://ss64.com/nt/robocopy-exit.html"
      exit 0
}

rac...@rundeck.com

unread,
Oct 31, 2021, 7:36:37 PM10/31/21
to rundeck-discuss
Great!

Thanks for publishing the final solution.

Kudos to you.

Greetings.

SieuweG

unread,
Nov 24, 2021, 12:29:55 PM11/24/21
to rundeck-discuss
I'm having the same issue and used your script to troubleshoot my issue, but I'm not able to connect.
I'm still getting ' Access is denied.' when robocopy tries to access the destination.

Op maandag 1 november 2021 om 00:36:37 UTC+1 schreef rac...@rundeck.com:

SieuweG

unread,
Nov 25, 2021, 3:32:19 AM11/25/21
to rundeck-discuss
Reply all
Reply to author
Forward
0 new messages