mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
win_file: fix error when creating an existing dir (#19070)
If something else created the dir New-Item will throw an exception and the task will fail. Check the existing file and as long as it's a dir, we don't need to error out.
This commit is contained in:
parent
011b324638
commit
c80d696cec
1 changed files with 13 additions and 2 deletions
|
@ -66,7 +66,7 @@ function Remove-File($file, $checkmode) {
|
||||||
Remove-Item -Path $file.FullName -Force -WhatIf:$checkmode
|
Remove-Item -Path $file.FullName -Force -WhatIf:$checkmode
|
||||||
}
|
}
|
||||||
} catch [Exception] {
|
} catch [Exception] {
|
||||||
Fail-Json (New-Object psobject) "Failed to delete $($file.FullName): $($_.Exception.Message)"
|
Fail-Json $result "Failed to delete $($file.FullName): $($_.Exception.Message)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +116,18 @@ if (Test-Path $path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($state -eq "directory") {
|
if ($state -eq "directory") {
|
||||||
New-Item -Path $path -ItemType Directory -WhatIf:$check_mode | Out-Null
|
try {
|
||||||
|
New-Item -Path $path -ItemType Directory -WhatIf:$check_mode | Out-Null
|
||||||
|
} catch {
|
||||||
|
if ($_.CategoryInfo.Category -eq "ResourceExists") {
|
||||||
|
$fileinfo = Get-Item $_.CategoryInfo.TargetName
|
||||||
|
if ($state -eq "directory" -and -not $fileinfo.PsIsContainer) {
|
||||||
|
Fail-Json $result "path $path is not a directory"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Fail-Json $result $_.Exception.Message
|
||||||
|
}
|
||||||
|
}
|
||||||
$result.changed = $true
|
$result.changed = $true
|
||||||
} elseif ($state -eq "file") {
|
} elseif ($state -eq "file") {
|
||||||
Fail-Json $result "path $path will not be created"
|
Fail-Json $result "path $path will not be created"
|
||||||
|
|
Loading…
Reference in a new issue