Copying file from a remote windows share

Hi
I’m trying to write a PowerShell script which, in part, copies a file from a remote windows share (via UNC path) which I am calling from Ansible (with the “script” module) but I’m getting “The network path was not found” errors. The path does exist and the Ansible user has read/write access…

I’m using Ansible 2.4.0.0 and letting it sort out the Kerberos ticket - I think I’m hitting the “2nd Hop” credential issue…

I’ve tried a couple of workarounds:

  • Use “become” with become_method: runas etc. but I’m struggling to get the right syntax (any examples of “become” with Windows?)
  • Map a drive in the PowerShell script by explicitly supplying credentials and then copying the file from that mapped drive
    Any thoughts?

Kinda managed to sort my own problem… I used this snippet in my PowerShell script:

`

Super-secret credentials…

$network_user = “domain\user”
$network_password = “monkey123”

Where do we want to connect to - note: do not use a trailing \

$remotepath = “\server\path”

Convert to a secure string…

$PWord = ConvertTo-SecureString $network_password -AsPlainText -Force

… and create a credential object

$myCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $network_user,$PWord

Map a drive to the network path…

New-PSDrive -Name Z -PSProvider FileSystem -Root $remotepath -Credential $myCreds

Query the remote path - note we’re NOT using the mapped drive…

Get-ChildItem $remotepath

Call other commands against $remotepath…

`

What’s neat about this is that you don’t actually need to use the mapped drive - I guess you could even remove it straight after creating it. I think this works because you’re creating an authenticated connection to the $remotepath which persists throughout the script.

Would still be interesting in getting “become” working but that’s for another day.

Any path outside of the computer you are connecting with will not be accessible unless

  • You are using credential delegation (CredSSP or Kerberis with ansible_winrm_kerberos_delegation: true)
  • You use become
  • You use a scheduled task
  • You manually map the drive and copy the files

Looks like you have worked out the last option where the drive is mapped using New-PSDrive for that session which is good. Become is fairly new and I only just started documenting it, you can see the latest guide here https://github.com/jborean93/ansible/blob/45ce16177b5839ebc12f0f6ba046969c67d50ec0/docs/docsite/rst/become.rst#become-and-windows. If that page becomes unaccessible it will mean it has been merged into the devel branch and should be viewed on the actual ansible site. Hopefully that guide helps you with become and you can get it working if you are interested.

Thanks

Jordan