powershellnetwork-share

Accessing UNC path share folder WON'T let me


Thank you for taking the time to look into this and helping me out. I'm trying to access a share drive(s) that is password protected. So when I manually go to explorer and type \server\filepath\ it prompts this window. Which is great because I can enter my credential and then I can begin to use my script to copy-item.

enter image description here

SO, when I attempt to use Set-Location -Path \\serverb\filepath using Get-Credential domain\username it tells me that the path does not exist. When I do test-path \serverb\ it also says the path does not exist. But it will only exist when I manually type the folder share path in explorer, map a network drive or use the run command because it opens that prompt window (like the one from above). All I really want to do is use Powershell to Copy-Item even if I have to manually enter credentials. But it seems that the Get-Credential does not work at all when it comes to accessing protected network shares. The script below will work on \server\filepath because I have entered my credentials from the GUI prompt. But I cannot access use the same script on \serverb\filepath\file.txt because it doesn't see that the share exist. So now that I just manually went to it now the script works. But when I reboot my computer which cleared out the cache credentials I'm back to square one. Any thoughts?

enter image description here

Import-Module bitstransfer
$cred = Get-Credential
$sourcePath = "\\serverb\filepath\file.txt"
$destPath = "D:\some_folder"
Start-BitsTransfer -Source $sourcePath -Destination $destPath -Credential $cred

Solution

  • Based on your feedback, it seems that access to the file share must be ensured first, which you can achieve by New-PSDrive with a suitable -Credential argument to map a (session-specific or persistent) drive (you needn't actually use a path based on that drive - a successful mapping will allow access even by UNC path).

    Import-Module bitstransfer
    
    # Prompt for credentials
    $cred = Get-Credential
    
    $sourcePath = '\\serverb\filepath\file.txt'
    $destPath = 'D:\some_folder'
    
    # Use the credentials to map a (temporary) drive named dummy:
    # to the source file share, which will allow access to the latter, even 
    # via UNC paths.
    if (-not (Test-Path dummy:)) {
      New-PSDrive -Credential $cred dummy FileSystem \\serverb\filepath
    }
    
    Start-BitsTransfer -Credential $cred -Source $sourcePath -Destination $destPath