[英]Powershell Remove-Item on folder failing only when run through Ansible
我有一个Powershell函数,可以完全删除目录。 我已经按照针对Powershell的建议构建了它(在那里有一些额外的跟踪)
function DeleteFolderAndContents()
{
param(
[Parameter(Mandatory=$true, Position=1)] [string] $folder_path
)
Get-ChildItem -Path "$folder_path" -Recurse -Force | Remove-Item -Force -Recurse
Write-Host "Deleted all files in directory. Will attempt to delete directory next"
Start-Sleep 3
Write-Host "Slept for 3 seconds. Now trying to remove folder"
Remove-Item "$folder_path" -Force
Write-Host "DeleteFolderAndContents worked seemingly without error"
while (Test-Path "$folder_path") { Start-Sleep 10 }
}
如果我通过Powershell cmdline运行它,则没有问题。 当Ansible尝试运行同一脚本(通过“脚本”任务)时,Get-ChildItem部分将起作用,删除所有文件夹内容,但是Remove-Item无法完全消除该目录。
我收到以下错误消息
System.Management.Automation.PSArgumentException:
An object at the specified path C:\\bblabla\\blabla\\blabla\\A.C.S.Api does not exist.
at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeThePath(String basepath, Stack`1 tokenizedPathStack),
at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeRelativePathHelper(String path, String basePath)
我不知道为什么会这样。 我很确定这是一个Ansible问题,但是不知道怎么回事,我也不知道该怎么办
找到了问题。 由于我的Ansible实现,路径的末尾有一个隐藏的尾随空间。 由于某些原因,PowerShell忽略了Get-ChildItem调用中的空间,但没有忽略Remove-Item调用中的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.