Backup directory on unauthorized remote share

46 views
Skip to first unread message

skinnedknuckles

unread,
Jun 3, 2016, 6:26:25 PM6/3/16
to Ansible Project
Control Node:
  • CentOS 7
  • Ansible 2.1
  • pywinrm version from May 19th, 2016
Remote Node:
  • Windows 7
  • Powershell 3
I have to make a copy of directory on a share to another location on that same share.  The account I use for connecting does not have access to the share so I am trying to use the rights of another account namely ADS-6999.  I wrote the following powershell script which works perfectly.

#copies contents of a share directory to another share directory
$uncServer="\\10.1.123.123"
$uncSrc="\shareName$\dir1\dir2\dir3" 
$uncDest="\shareName$\dir4\6999"
$userName="ADS-6999"
$password="p@ssw0rd"
$uncFullSrc = "$uncServer$uncSrc"
$uncFullDest = "$uncServer$uncDest"
net use $uncServer $password /USER:$userName
Copy-Item $uncFullSrc $uncFullDest -Recurse
net use $uncServer /delete

So I modified the script above to be called from ansible on the control node and saved it as "copyShare2Share.ps1 shown below:

#copies contents of a share directory to another share directory
param(
    [string]$uncServer,
    [string]$uncSrc,
    [string]$uncDest,
    [string]$userName,
    [string]$password
)
$uncFullSrc = "$uncServer$uncSrc"
$uncFullDest = "$uncServer$uncDest"
net use $uncServer $password /USER:$userName
Copy-Item $uncFullSrc $uncFullDest -Recurse
net use $uncServer /delete


And I call the script from my ansible playbook as follows:

---
- name: deploy new software version
  hosts: windows
  vars:
    currentDate: ""
    machineId: "none"
    source: "\\shareName$\\dir1\\dir2\\dir3"
    destination: "\\shareName$\\dir4"
    machineId: "6999"
    uncServer: "\\\\10.1.123.123"  - name: BACKUP FILES
    tasks:
    - name: BACKUP USER FILES
      script: copyShare2Share.ps1 {{uncServer}} {{source}} "{{destination}}\{{machineId}}" "ADS-6999" "adsads"


Here is what I get for output:

<ADS-6999> EXEC &  'C:\Users\ansibleAdmin.ADS-6999\AppData\Local\Temp\ansible-tmp-1464986197.84-138031458928067\copyShare2Share.ps1' \\10.1.123.123 \shareName$\dir1\dir2\dir3\ "\shareName$\dir4\\6999" "ADS-6999" "p@ssw0rd"
<ADS-6999> WINRM EXEC 'PowerShell' ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted', '-EncodedCommand', '



"stderr": "The user context supplied is invalid.\r\n\r\nMore help is available by typing NET HELPMSG 3775.\r\n\r\n#< CLIXML\r\nThe network connection could not be found.\r\n\r\nMore help is available by typing NET HELPMSG 2250.\r\n\r\n<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><S S=\"Error\">Copy-Item : Access is denied_x000D__x000A_</S><S S=\"Error\">At C:\\Users\\ansibleAdmin.ADS-6999\\AppData\\Local\\Temp\\ansible-tmp-1464986197.84-_x000D__x000A_</S><S S=\"Error\">138031458928067\\copyShare2Share.ps1:12 char:1_x000D__x000A_</S><S S=\"Error\">+ Copy-Item $uncFullSrc $uncFullDest -Recurse_x000D__x000A_</S><S S=\"Error\">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D__x000A_</S><S S=\"Error\">    + CategoryInfo          : PermissionDenied: 



PLAY RECAP *********************************************************************
ADS-6999                   : ok=4    changed=2    unreachable=0    failed=0   




skinnedknuckles

unread,
Jun 6, 2016, 1:30:59 PM6/6/16
to Ansible Project
SOLVED!!  Apparently it didn't like the following lines

$uncFullSrc = "$uncServer$uncSrc"
$uncFullDest = "$uncServer$uncDest"

because it finally worked after I changed them to

$uncFullSrc = $uncServer + $uncSrc
$uncFullDest = $uncServer + $uncDest

skinnedknuckles

unread,
Jun 6, 2016, 2:12:37 PM6/6/16
to Ansible Project
Oh, and I changed "userName" to "domain\userName"


On Friday, June 3, 2016 at 5:26:25 PM UTC-5, skinnedknuckles wrote:
Reply all
Reply to author
Forward
0 new messages