New-PSDrive:找不到网络路径

3

我在使用New-PSDrive连接远程服务器时遇到了问题。 远程服务器是基于Windows的,只有userA有写入权限。

具体来说,以下代码会抛出“拒绝访问”错误:

第3行无法访问路径“$remoteServerPath”

代码:

New-PSDrive -Name remote -Root $remoteServerPath -PSProvider FileSystem 
$destination = [IO.Path]::Combine('remote:', $fileName)
Copy-Item -Path $source -Destination $destination -Force

现在,我正在尝试包含凭据信息,但是出现了不同的错误!

第3行出现“网络路径未找到”的错误。

$secpass = ConvertTo-SecureString 'myPassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('domain\userA', $secpass)
New-PSDrive -Name remote -Root $remoteServerPath-PSProvider FileSystem -Credential $cred
$destination = [IO.Path]::Combine('remote:', $fileName)
Copy-Item -Path $source -Destination $destination -Force

请问有人能帮助我吗?Powershell版本5


请在您的问题中展示$remoteServerPath的赋值。 - Slogmeister Extraordinaire
1个回答

2
为什么要为这个任务创建一个PSDrive?
& NET USE Z: \\server\path /user:domain\UserA 'PASSWORD'
Copy-Item -Path $Source -Destination 'Z:\' -Force
& NET USE Z: /D

如果您有他们的明文密码,这应该可以正常工作。

我遇到了以下错误: “文件系统提供程序仅在New-PSDrive cmdlet上支持凭据。请再次执行操作,不要指定凭据。” - Adrian
@Adrian直接使用这个示例吗? - Maximilian Burszley
看起来只有New-PSDrive支持凭证,而copy-item不支持。这是因为我的PowerShell版本吗?(5) - Adrian
@Adrian 当然不是。你可以在控制台中使用 help command 来验证这些内容并查看语法。你是想以不同于当前登录用户的身份映射网络驱动器吗? - Maximilian Burszley
@Adrian 如果你想在目标位置更改文件名,只需将字符串编辑为 Z:\Filename.ext - Maximilian Burszley
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接