powershell - PS 中 "Access is denied"调用期间出现 "New-Item"错误

标签 powershell

我正在尝试使用 powershell 在服务器上创建一个文件夹。该脚本如下所示:

$pass = ConvertTo-SecureString "myPW" -AsPlainText -Force
$cred = new-object System.Management.Automation.PSCredential("myUser", $pass)
$session = New-PSSession -ComputerName "localhost" -Credential $cred
Invoke-Command -session $session -ScriptBlock {
        New-Item -Path "\\myServer\myShare\" -Name "myFolder" -ItemType directory -Force
    }

我收到以下错误:

Access is denied
    + CategoryInfo          : PermissionDenied: (\\myServer\myShare:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
    + PSComputerName        : localhost 

Access to the path 'myShare' is denied.
    + CategoryInfo          : PermissionDenied: (\\myServer\myShare:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
    + PSComputerName        : localhost

Access to the path 'myFolder' is denied.
    + CategoryInfo          : PermissionDenied: (\\myServer\myShare\myFolder:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
    + PSComputerName        : localhost

起初,我认为该消息非常明确,我使用的用户缺少权限。但事情是这样的:如果我转到资源管理器中的共享并使用同一用户登录,我可以毫无问题地创建一个文件夹。除此之外,该用户是域管理员。

我做错了什么?

最佳答案

这是一个双跃点,您正在连接到 PSSession,然后尝试访问该文件夹。

作为一项安全措施,PowerShell 不允许您远程到一台计算机,然后从那里远程到另一台计算机(即使其中两台计算机是同一台计算机)。确切的推理很复杂,并且很大程度上涉及到如何在 session 中使用凭据,我自己并不完全理解,所以我不会尝试解释,但基本上是为了防止凭据被盗。

您可以阅读一下 (this looks like a good resource)但尝试找出另一种方法会简单得多。您只是远程连接到本地计算机,以便您可以启动另一个 PS 进程。

关于powershell - PS 中 "Access is denied"调用期间出现 "New-Item"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42439229/

相关文章:

powershell - 如何从 PowerShell 中的引擎事件处理程序获取返回值?

powershell - Lync - PowerShell - 用户状态

windows - 杀死特定的 powershell 窗口

powershell - 如何单独获取 registryKey 名称而不是完整的注册表键路径?

powershell - 卸载以特定字符串开头的所有软件

在 word 中查找和更改超链接的 Powershell 脚本保存 doc 并创建一个新副本作为 PDF

powershell - 将setCaseSensitiveInfo递归应用于所有文件夹和子文件夹

wpf - 如何使 Powershell WPF 表单在加载时运行一个函数(并显示自身)?

wcf - 使用 PowerShell 的 New-WebServiceProxy 访问 net.tcp 端点

powershell - 如何使用 PowerShell 替换文件中的多个字符串