Powershell &引用;“访问被拒绝”;“在”期间出错;新项目;请进PS

Powershell &引用;“访问被拒绝”;“在”期间出错;新项目;请进PS,powershell,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-Co

我正在尝试使用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不允许您先远程到一台计算机,然后再从那里远程到另一台计算机(即使其中两台是同一台计算机)。确切的推理是复杂的,涉及到如何在会话中使用凭据,我自己也不完全理解,所以我不会试图解释,但基本上是为了防止凭据被盗


你可以读一些关于它的书,但如果你能想出另一种方法,那就简单多了。您只需远程处理到本地计算机,例如,您可以启动另一个PS进程。

如果您只是使用PSSession作为备用凭据来访问共享,则使用
新PSDrive
可能是另一种方式:

$pass = ConvertTo-SecureString "myPW" -AsPlainText -Force
$cred = new-object System.Management.Automation.PSCredential("myUser", $pass)

New-PSDrive -Name myShare -PSProvider FileSystem -Root "\\myServer\myShare\" -Credential $cred
New-Item -Path "myShare:\" -Name "myFolder" -ItemType directory -Force

我想你正面临一个“双跳”的问题,读这篇文章,我想知道这是否是双跳的问题。听起来可能就是这个问题。我将研究使用其他凭据调用它的其他方法,并查看它是否有效。如果.NET copy类中有任何一个支持凭据,则您可能可以使用该类,但我以前广泛地遇到过此问题,从未发现任何整洁的方法。如果您运气好,一定要告诉我=)我可以将
开始作业
-Credential
参数一起使用。现在很好用。非常感谢!:-)因为作业没有在新会话中运行。不适用于我的情况,但很好,你找到了一个简单的答案=)谢谢你的建议,但是脚本块中有更多的命令我需要凭据。这只是我问题的相关部分。