【发布时间】: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