访问 UNC 路径共享文件夹不会让我

Accessing UNC path share folder WON'T let me

感谢您花时间研究此事并帮助我。我正在尝试访问受密码保护的共享驱动器。因此,当我手动转到资源管理器并键入 \server\filepath\ 时,它会提示此 window。这很棒,因为我可以输入我的凭据,然后我可以开始使用我的脚本来复制项目。

SO,当我尝试通过 Get-Credential domain\username 使用 Set-Location -Path \serverb\filepath 时,它告诉我该路径不存在。当我执行 test-path \serverb\ 时,它还表示该路径不存在。但只有当我在资源管理器中手动键入文件夹共享路径、映射网络驱动器或使用 运行 命令时,它才会存在,因为它会打开提示 window(如上面的提示)。我真正想做的就是使用 Powershell Copy-Item,即使我必须手动输入凭据。但似乎 Get-Credential 在访问受保护的网络共享时根本不起作用。下面的脚本将在 \server\filepath 上运行,因为我已经从 GUI 提示符输入了我的凭据。但是我无法在 \serverb\filepath\file.txt 上访问使用相同的脚本,因为它看不到共享存在。所以现在我只是手动去它现在脚本工作。但是,当我重新启动清除了缓存凭据的计算机时,我又回到了原点。有什么想法吗?

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

根据您的反馈,似乎必须首先确保对文件共享的访问,您可以通过 New-PSDrive 使用合适的 -Credential 参数来映射(特定于会话或persistent) drive(您实际上不需要使用基于该驱动器的路径 - 成功的映射甚至允许通过 UNC 路径访问)。

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