Copy file to folder with alternate credentials

Welcome Forums General PowerShell Q&A Copy file to folder with alternate credentials

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
7 months, 3 weeks ago.

  • Author
    Posts
  • #96966

    Participant
    Points: 0
    Rank: Member

    I am writing a script that will copy a file to a folder on my local C: drive. The folder requires an Admin to write to. I cannot run the script from the ISE or using alternate credentials. So I need to prompt for credentials in the script. Copy-Item will not accept a credential. I have searched all over the internet and it seems the accepted method is to create a new PSDrive with your credentials and then copy the file. Unfortunately, I have not been able to get that to work either. I can create the drive, but I still get "Access Denied" when I try to copy the file.

    Any help would be greatly appreciated.

  • #96968

    Keymaster
    Points: 1,624
    Helping HandTeam Member
    Rank: Community Hero

    None of the -Item commands accept credentials; you're meant to use New-PSDrive to create a new drive (pointed to a UNC, not a local path), and THAT accepts credentials for the connection. The -Item commands just operate against whatever drive you need. So you're right on that track.

    If it's not working, then either (a) the credential you're using doesn't, in fact, have access or (b) something else is happening at a security layer. You'd need to go exploring in the Security event log and such to see what's happening, and make sure you're auditing for access failures and such, so that you're getting a troubleshooting record.

  • #96972

    Participant
    Points: 0
    Rank: Member

    Thanks, I got it to work by converting the PSDrive to a UNC path.
    '\\localhost\C$\Program Files (x86)\'
    Instead of:
    'C:\Program Files (x86)\'

    Now I just need to clean up the mess I made while tweaking different parts of the code.

The topic ‘Copy file to folder with alternate credentials’ is closed to new replies.