【问题标题】:Powershell Remove-Item on folder failing only when run through Ansible仅当通过 Ansible 运行时,文件夹上的 Powershell Remove-Item 才会失败
【发布时间】:2017-03-16 23:00:07
【问题描述】:

我有一个完全删除目录的 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 尝试运行相同的脚本(通过 Script 任务)时,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 问题,但不明白这是怎么回事,我不知道该怎么办

【问题讨论】:

  • 你明白了@techraf

标签: windows powershell ansible


【解决方案1】:

发现问题。由于我的 Ansible 实现,路径末尾有一个隐藏的尾随空格。出于某种原因,PowerShell 忽略了 Get-ChildItem 调用中的空格,但没有忽略 Remove-Item 调用中的空格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2016-08-13
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 2014-06-06
    相关资源
    最近更新 更多