2017-03-16 92 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調用中。

相關問題