微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

测试路径访问被拒绝错误 - 使用带有凭据的 powershell 检查共享驱动器/文件夹上是否存在目录

如何解决测试路径访问被拒绝错误 - 使用带有凭据的 powershell 检查共享驱动器/文件夹上是否存在目录

我正在尝试使用具有访问网络服务器的不同凭据的测试路径 powershell 脚本检查网络位置上是否存在文件夹,但出现访问被拒绝错误。下面是我试图在本地从我试图连接到网络服务器的地方执行的脚本:

$username="username"
$pass =  "@Passw0rd" | ConvertTo-securestring -AsPlainText -Force

$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$pass
$pathExists = Invoke-Command -ComputerName . -Credential $cred -ScriptBlock {
    Test-Path "\\serverName\Folder"
}
Write-Host $pathExists 

我收到以下错误


访问被拒绝 + CategoryInfo : PermissionDenied: (\serverName\Folder:String) [Test-Path],UnauthorizedAccessException + FullQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.TestPathCommand + PSComputerName : 本地主机


注意:该帐户具有管理员权限,用于访问网络路径和目录

任何帮助将不胜感激

解决方法

问题很可能是因为存储在 $cred 中的凭据在您的本地主机上没有管理员权限,因此无法模拟您的调用。

在这里,我正在尝试类似于您正在尝试的操作,存储在 $cred 中的用户是远程主机上的管理员,但在我的笔记本电脑上没有任何权限(出于显而易见的原因,我进行了混淆) :

PS C:\> icm remoteServer -Credential $cred {test-path \\remoteServer\c$\users}
True

PS C:\> icm remoteServer -Credential $cred {test-path \\localhost\c$\users}
True

PS C:\> icm -computername . -Credential $cred {test-path \\localhost\c$\users}
[localhost] Connecting to remote server localhost failed with the following error message : Access is denied. For more 
information,see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (localhost:String) [],PSRemotingTransportException
    + FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken

编辑:

这是您问题的另一种解决方案,Start-Process 使用远程凭据并将结果保存到文件中(再次混淆远程服务器的名称):

$argument="-c `"`$path='\\remoteServer\c$\users';'Attempting Test-Path '+`$path;'Result is: '+(Test-Path `$path)`""

$initHash=@{
    FilePath='powershell.exe'
    Credential=$cred
    ArgumentList=$argument
    RedirectStandardOutput="$HOME\Documents\testPath.txt"
    WindowStyle='Hidden'
}

Start-Process @initHash

PS C:\> gc "$HOME\Documents\testPath.txt"
Attempting Test-Path \\remoteServer\c$\users
Result is: True

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?