如何解决测试路径访问被拒绝错误 - 使用带有凭据的 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 举报,一经查实,本站将立刻删除。