PowerShell:尽管与 Windows Explorer 一起使用相同的进程,但 Copy-Item 失败
PowerShell: Copy-Item fails despite the same process working with Windows Explorer
我有一个脚本需要定期(每小时)复制文件。我可以使用 windows 资源管理器打开源文件夹和目标文件夹,然后毫无问题地复制文件。
但是,如果我在 PowerShell 中尝试同样的操作,我会收到“访问路径被拒绝”错误。我已经检查了共享权限,并且拥有完全访问权限。为什么这会通过 PowerShell 失败?
复制项目命令:
Copy-Item \idmststtrm2\tns_admin$\tnsnames.ora -Destination \bts13r2b\tnsnames -Force
错误:
Copy-Item : Access to the path '\bts13r2b\tnsnames\tnsnames.ora' is denied.
At line:1 char:1
+ Copy-Item \idmststtrm2\tns_admin$\tnsnames.ora -Destination \bts13r2b\tnsnames ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\idmststtrm2\tns_admin$\tnsnames.ora:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Access to the path '\bts13r2b\tnsnames\tnsnames.ora' is denied.
At line:1 char:1
+ Copy-Item \idmststtrm2\tns_admin$\tnsnames.ora -Destination \bts13r2b\tnsnames ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand
编辑:
当我对目标路径执行 Get-ChildItem
时,我能够看到文件夹内容。
获取项目的结果:
get-item \idmststtrm2\tns_admin$\tnsnames.ora
Directory: \idmststtrm2\tns_admin$
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/10/2017 8:49 AM 14143 tnsnames.ora
get-item \bts13r2b\tnsnames\tnsnames.ora
Directory: \bts13r2b\tnsnames
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/8/2017 9:51 AM 15991 tnsnames.ora
get-item \bts13r2b\tnsnames
Directory:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/21/2017 11:14 AM tnsnames
尝试使用 xcopy:
xcopy \idmststtrm2\tns_admin$\tnsnames.ora \bts13r2b\tnsnames\tnsnames.ora
Access is denied.
尝试以管理员身份打开 powershell,有时会导致此问题
你的想法是正确的,但是如果你尝试使用 'Administrative Share' 访问其他系统呢?
创建了一些标准变量:$Source
和 $Target
。现在我们使用 Get-ChildItem
和开关 -Path
来抓取我们需要的文件或目录。然后我们使用 Copy-Item
和开关 -Force
将文件发送到其他服务器。此方法应该有效,但将介绍另一种方法。
我假设它看起来像这样。
$Source = "\idmststtrm2\c$\app\oracle\product.2.0\dbhome_1\network\admin\tns_admin$\tnsnames.ora"
$Target="\bts13r2b\c$\app\oracle\product.2.0\dbhome_1\network\admin\tnsnames"
Get-ChildItem -Path $Source | Copy-Item -Destination $Target -Force
另一种选择是确保您首先对两个共享目录具有写入权限。一旦确认,我们 运行 以下内容:
$Source="\idmststtrm2\tns_admin$\tnsnames.ora"
$Target="\bts13r2b\tnsnames"
Get-ChildItem -Path $Source | Copy-Item -Destination $Target -Force
#(Get-Acl $Source).Access #Verify $Source Access
#(Get-Acl $Target).Access #Verify $Target Access
让我们知道这是否有效。
此问题是由于写入网络共享的权限所致。虽然正确设置了共享权限,但缺少 NTFS 权限。系统管理员需要确保两组权限都允许帐户写入文件夹。正确更新后,脚本能够执行到网络共享的复制。
我知道这已经过时了,但我在尝试获得预定工作(运行 作为 GMSA)以使用 Copy-Item 和得到相同的 "Access to the path ... is denied" 错误。我检查并仔细检查了远程共享的权限 - 共享权限和 NTFS 权限。我的登录 运行 成功,管理员登录 运行 成功。
最后,为了开心,我将共享权限从 "All Users" 更改为 "Everyone",它开始工作了!因此,GMSA 帐户似乎不属于 "All Users"。万万没想到!
希望这可以为某人节省 10 个小时的徒劳...
我有一个脚本需要定期(每小时)复制文件。我可以使用 windows 资源管理器打开源文件夹和目标文件夹,然后毫无问题地复制文件。
但是,如果我在 PowerShell 中尝试同样的操作,我会收到“访问路径被拒绝”错误。我已经检查了共享权限,并且拥有完全访问权限。为什么这会通过 PowerShell 失败?
复制项目命令:
Copy-Item \idmststtrm2\tns_admin$\tnsnames.ora -Destination \bts13r2b\tnsnames -Force
错误:
Copy-Item : Access to the path '\bts13r2b\tnsnames\tnsnames.ora' is denied.
At line:1 char:1
+ Copy-Item \idmststtrm2\tns_admin$\tnsnames.ora -Destination \bts13r2b\tnsnames ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\idmststtrm2\tns_admin$\tnsnames.ora:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Access to the path '\bts13r2b\tnsnames\tnsnames.ora' is denied.
At line:1 char:1
+ Copy-Item \idmststtrm2\tns_admin$\tnsnames.ora -Destination \bts13r2b\tnsnames ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand
编辑:
当我对目标路径执行 Get-ChildItem
时,我能够看到文件夹内容。
获取项目的结果:
get-item \idmststtrm2\tns_admin$\tnsnames.ora
Directory: \idmststtrm2\tns_admin$
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/10/2017 8:49 AM 14143 tnsnames.ora
get-item \bts13r2b\tnsnames\tnsnames.ora
Directory: \bts13r2b\tnsnames
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/8/2017 9:51 AM 15991 tnsnames.ora
get-item \bts13r2b\tnsnames
Directory:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/21/2017 11:14 AM tnsnames
尝试使用 xcopy:
xcopy \idmststtrm2\tns_admin$\tnsnames.ora \bts13r2b\tnsnames\tnsnames.ora
Access is denied.
尝试以管理员身份打开 powershell,有时会导致此问题
你的想法是正确的,但是如果你尝试使用 'Administrative Share' 访问其他系统呢?
创建了一些标准变量:$Source
和 $Target
。现在我们使用 Get-ChildItem
和开关 -Path
来抓取我们需要的文件或目录。然后我们使用 Copy-Item
和开关 -Force
将文件发送到其他服务器。此方法应该有效,但将介绍另一种方法。
我假设它看起来像这样。
$Source = "\idmststtrm2\c$\app\oracle\product.2.0\dbhome_1\network\admin\tns_admin$\tnsnames.ora"
$Target="\bts13r2b\c$\app\oracle\product.2.0\dbhome_1\network\admin\tnsnames"
Get-ChildItem -Path $Source | Copy-Item -Destination $Target -Force
另一种选择是确保您首先对两个共享目录具有写入权限。一旦确认,我们 运行 以下内容:
$Source="\idmststtrm2\tns_admin$\tnsnames.ora"
$Target="\bts13r2b\tnsnames"
Get-ChildItem -Path $Source | Copy-Item -Destination $Target -Force
#(Get-Acl $Source).Access #Verify $Source Access
#(Get-Acl $Target).Access #Verify $Target Access
让我们知道这是否有效。
此问题是由于写入网络共享的权限所致。虽然正确设置了共享权限,但缺少 NTFS 权限。系统管理员需要确保两组权限都允许帐户写入文件夹。正确更新后,脚本能够执行到网络共享的复制。
我知道这已经过时了,但我在尝试获得预定工作(运行 作为 GMSA)以使用 Copy-Item 和得到相同的 "Access to the path ... is denied" 错误。我检查并仔细检查了远程共享的权限 - 共享权限和 NTFS 权限。我的登录 运行 成功,管理员登录 运行 成功。
最后,为了开心,我将共享权限从 "All Users" 更改为 "Everyone",它开始工作了!因此,GMSA 帐户似乎不属于 "All Users"。万万没想到!
希望这可以为某人节省 10 个小时的徒劳...