2017-03-16 89 views
1

我有一个PowerShell功能,完全删除一个目录。我已经建立了它按(在那里与一些额外的跟踪)推荐的PowerShell作为这样PowerShell删除项目文件夹失败时只有当通过Ansible运行

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问题,但不明白如何可能,我不知道该怎么办

+0

你懂了@techraf – Mark

回答

1

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

相关问题