gpt4 book ai didi

powershell - 删除项目错误 : Cannot remove item [item path & name]: Access to the path '[item path & name]' is denied

转载 作者:行者123 更新时间:2023-12-04 01:18:12 28 4
gpt4 key购买 nike

我是 PowerShell 的新手。

我正在尝试自动将 dll 组件从源服务器上的文件夹部署到目标服务器上的多个文件夹。这看起来应该很简单:将组件从源服务器上的源(部署)文件夹复制到目标服务器上的文件夹,验证副本并最终从源服务器上的部署文件夹中删除组件。

将文件从源服务器复制到目标服务器的工作没有问题。
但是,当脚本继续从源服务器删除组件时,我间歇性地遇到错误:
“删除项目错误:无法删除项目 [项目路径和名称]:拒绝访问路径“[项目路径和名称]”。”

我已经多次运行这个脚本;有时它完成没有问题,有时有错误。并非每个要删除的文件都会发生该错误,并且每次出现时似乎都发生在不同的组件上。

下面是我编写的用于删除组件和验证删除的函数:

function DeleteSourceFiles($srcPath) {
# Announce delete
OutputToHostAndLog ("Files will be removed from "+$srcPath+"...")
OutputToHostAndLog "Removing files..."

# Deletes all file items (i.e. all except folders) in source folder
$filesToDelete=Get-ChildItem $srcPath | Where-Object {$_ -is [IO.FileInfo]}
ForEach($item in $filesToDelete) {
Remove-Item $srcPath\$item -force

# Verify deletions
if(Test-Path($srcPath+"\"+$item)) {
OutputToHostAndLog ("Delete failed: "+$item.Name)
$fail++
}
else {
OutputToHostAndLog ($item.Name+" deleted successfully...")
}
}
}

将 -force 参数与 Remove-Item cmdlet 一起使用似乎对该问题没有任何影响。无论如何,这些文件(同样,每次失败的不同文件)似乎都不是 isReadOnly 。

同样,以管理员身份运行 PowerShell 似乎没有任何效果,尽管源文件夹的 Get-Acl 指示管理员应该具有 FullControl。

这是我缺少的权限问题吗?
任何建议非常感谢...

编辑:
我因此更新了我的脚本:
function DeleteSourceFiles($srcPath) {
# Announce delete
OutputToHostAndLog ("Files will be removed from "+$srcPath+"...")
OutputToHostAndLog "Removing files..."
OutputToHostAndLog $gap

# Delete all file items (i.e. all except folders) in source folder
$filesToDelete=Get-ChildItem $srcPath | Where-Object {$_ -is [IO.FileInfo]} | ForEach {
Remove-Item $_.FullName -Force

# Verify deletions
if(Test-Path($srcPath+"\"+$_)) {
OutputToHostAndLog ("Delete failed: "+$_.Name)
$fail++
}
else {
OutputToHostAndLog ($_.Name+" deleted successfully...")
}
}
}

这似乎工作正常,尽管我仍然不确定为什么这种安排会产生不同的结果。为了学习,任何见解将不胜感激......

最佳答案

间歇性访问被拒绝错误可能表明您尝试删除的一个或多个文件已被另一个应用程序锁定。当您尝试清理日志目录时,这是一个非常常见的问题。

我唯一建议做的就是等待带有锁的应用程序释放文件。

关于powershell - 删除项目错误 : Cannot remove item [item path & name]: Access to the path '[item path & name]' is denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22939800/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com