|
|
|
@ -19,97 +19,67 @@
|
|
|
|
|
|
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
|
|
|
|
|
$params = Parse-Args $args
|
|
|
|
|
$params = Parse-Args $args -supports_check_mode $true
|
|
|
|
|
|
|
|
|
|
# path
|
|
|
|
|
$path = Get-Attr $params "path" $FALSE
|
|
|
|
|
If ($path -eq $FALSE)
|
|
|
|
|
{
|
|
|
|
|
$path = Get-Attr $params "dest" $FALSE
|
|
|
|
|
If ($path -eq $FALSE)
|
|
|
|
|
{
|
|
|
|
|
$path = Get-Attr $params "name" $FALSE
|
|
|
|
|
If ($path -eq $FALSE)
|
|
|
|
|
{
|
|
|
|
|
Fail-Json (New-Object psobject) "missing required argument: path"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# JH Following advice from Chris Church, only allow the following states
|
|
|
|
|
# in the windows version for now:
|
|
|
|
|
# state - file, directory, touch, absent
|
|
|
|
|
# (originally was: state - file, link, directory, hard, touch, absent)
|
|
|
|
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -default $false
|
|
|
|
|
|
|
|
|
|
$state = Get-Attr $params "state" "unspecified"
|
|
|
|
|
# if state is not supplied, test the $path to see if it looks like
|
|
|
|
|
# a file or a folder and set state to file or folder
|
|
|
|
|
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -default $false -failifempty $true -aliases "dest","name"
|
|
|
|
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -validateset "absent","directory","file","touch"
|
|
|
|
|
|
|
|
|
|
# result
|
|
|
|
|
$result = New-Object psobject @{
|
|
|
|
|
changed = $FALSE
|
|
|
|
|
$result = @{
|
|
|
|
|
changed = $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
If ( $state -eq "touch" )
|
|
|
|
|
{
|
|
|
|
|
If(Test-Path $path)
|
|
|
|
|
{
|
|
|
|
|
(Get-ChildItem $path).LastWriteTime = Get-Date
|
|
|
|
|
}
|
|
|
|
|
Else
|
|
|
|
|
{
|
|
|
|
|
echo $null > $path
|
|
|
|
|
if ($state -eq "touch") {
|
|
|
|
|
if (-not $check_mode) {
|
|
|
|
|
if (Test-Path $path) {
|
|
|
|
|
(Get-ChildItem $path).LastWriteTime = Get-Date
|
|
|
|
|
} else {
|
|
|
|
|
echo $null > $path
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$result.changed = $TRUE
|
|
|
|
|
$result.changed = $true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
If (Test-Path $path)
|
|
|
|
|
{
|
|
|
|
|
if (Test-Path $path) {
|
|
|
|
|
$fileinfo = Get-Item $path
|
|
|
|
|
If ( $state -eq "absent" )
|
|
|
|
|
{
|
|
|
|
|
Remove-Item -Recurse -Force $fileinfo
|
|
|
|
|
$result.changed = $TRUE
|
|
|
|
|
}
|
|
|
|
|
Else
|
|
|
|
|
{
|
|
|
|
|
If ( $state -eq "directory" -and -not $fileinfo.PsIsContainer )
|
|
|
|
|
{
|
|
|
|
|
Fail-Json (New-Object psobject) "path is not a directory"
|
|
|
|
|
if ($state -eq "absent") {
|
|
|
|
|
if (-not $check_mode) {
|
|
|
|
|
Remove-Item -Recurse -Force $fileinfo
|
|
|
|
|
}
|
|
|
|
|
$result.changed = $true
|
|
|
|
|
} else {
|
|
|
|
|
if ($state -eq "directory" -and -not $fileinfo.PsIsContainer) {
|
|
|
|
|
Fail-Json $result "path $path is not a directory"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
If ( $state -eq "file" -and $fileinfo.PsIsContainer )
|
|
|
|
|
{
|
|
|
|
|
Fail-Json (New-Object psobject) "path is not a file"
|
|
|
|
|
if ($state -eq "file" -and $fileinfo.PsIsContainer) {
|
|
|
|
|
Fail-Json $result "path $path is not a file"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Else
|
|
|
|
|
# doesn't yet exist
|
|
|
|
|
{
|
|
|
|
|
If ( $state -eq "unspecified" )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
# If state is not supplied, test the $path to see if it looks like
|
|
|
|
|
# a file or a folder and set state to file or folder
|
|
|
|
|
if ($state -eq $null) {
|
|
|
|
|
$basename = Split-Path -Path $path -Leaf
|
|
|
|
|
If ($basename.length -gt 0)
|
|
|
|
|
{
|
|
|
|
|
if ($basename.length -gt 0) {
|
|
|
|
|
$state = "file"
|
|
|
|
|
}
|
|
|
|
|
Else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
$state = "directory"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
If ( $state -eq "directory" )
|
|
|
|
|
{
|
|
|
|
|
New-Item -ItemType directory -Path $path | Out-Null
|
|
|
|
|
$result.changed = $TRUE
|
|
|
|
|
if ($state -eq "directory") {
|
|
|
|
|
if (-not $check_mode) {
|
|
|
|
|
New-Item -ItemType directory -Path $path | Out-Null
|
|
|
|
|
}
|
|
|
|
|
$result.changed = $true
|
|
|
|
|
} elseif ($state -eq "file") {
|
|
|
|
|
Fail-Json $result "path $path will not be created"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
If ( $state -eq "file" )
|
|
|
|
|
{
|
|
|
|
|
Fail-Json (New-Object psobject) "path will not be created"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Exit-Json $result
|
|
|
|
|