我正在尝试使用 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/