简体   繁体   English

仅当通过Ansible运行时,文件夹上的Powershell Remove-Item才会失败

[英]Powershell Remove-Item on folder failing only when run through Ansible

I have a powershell function that completely deletes a directory. 我有一个Powershell函数,可以完全删除目录。 I've built it as per recommended for powershell as so (with some extra tracing in there) 我已经按照针对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 } 
}

If I run it through the Powershell cmdline it works no problem. 如果我通过Powershell cmdline运行它,则没有问题。 When Ansible tries to run the same script (through the Script task), the Get-ChildItem part works, deleting all folder contents, but the Remove-Item fails to completely eliminate the directory. 当Ansible尝试运行同一脚本(通过“脚本”任务)时,Get-ChildItem部分将起作用,删除所有文件夹内容,但是Remove-Item无法完全消除该目录。

I get the following error message 我收到以下错误消息

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)

I have no idea why this would happen. 我不知道为什么会这样。 I'm pretty sure its an Ansible issue but don't understand how that could be and I don't know what to do about it 我很确定这是一个Ansible问题,但是不知道怎么回事,我也不知道该怎么办

Found the issue. 找到了问题。 There was a hidden trailing space at the end of the path because of my Ansible implementation. 由于我的Ansible实现,路径的末尾有一个隐藏的尾随空间。 For some reason, PowerShell ignored the space in the Get-ChildItem call but not in the Remove-Item call. 由于某些原因,PowerShell忽略了Get-ChildItem调用中的空间,但没有忽略Remove-Item调用中的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM