2015-05-22 20:25:05 +02:00
|
|
|
#!powershell
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# WANT_JSON
|
|
|
|
# POWERSHELL_COMMON
|
|
|
|
|
2015-05-29 03:57:13 +02:00
|
|
|
$params = Parse-Args $args
|
2015-05-22 20:25:05 +02:00
|
|
|
|
|
|
|
# path
|
2015-05-29 03:57:13 +02:00
|
|
|
$path = Get-Attr $params "path" $FALSE
|
2015-05-22 20:25:05 +02:00
|
|
|
If ($path -eq $FALSE)
|
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
$path = Get-Attr $params "dest" $FALSE
|
2015-05-22 20:25:05 +02:00
|
|
|
If ($path -eq $FALSE)
|
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
$path = Get-Attr $params "name" $FALSE
|
2015-05-22 20:25:05 +02:00
|
|
|
If ($path -eq $FALSE)
|
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
Fail-Json (New-Object psobject) "missing required argument: path"
|
2015-05-22 20:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2015-05-29 03:57:13 +02:00
|
|
|
$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
|
2015-05-22 20:25:05 +02:00
|
|
|
|
|
|
|
# result
|
|
|
|
$result = New-Object psobject @{
|
|
|
|
changed = $FALSE
|
2015-05-29 03:57:13 +02:00
|
|
|
}
|
2015-05-22 20:25:05 +02:00
|
|
|
|
|
|
|
If ( $state -eq "touch" )
|
|
|
|
{
|
|
|
|
If(Test-Path $path)
|
|
|
|
{
|
|
|
|
(Get-ChildItem $path).LastWriteTime = Get-Date
|
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
echo $null > $file
|
|
|
|
}
|
2015-05-29 03:57:13 +02:00
|
|
|
$result.changed = $TRUE
|
2015-05-22 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
If (Test-Path $path)
|
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
$fileinfo = Get-Item $path
|
2015-05-22 20:25:05 +02:00
|
|
|
If ( $state -eq "absent" )
|
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
Remove-Item -Recurse -Force $fileinfo
|
|
|
|
$result.changed = $TRUE
|
2015-05-22 20:25:05 +02:00
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
# Only files have the .Directory attribute.
|
|
|
|
If ( $state -eq "directory" -and $fileinfo.Directory )
|
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
Fail-Json (New-Object psobject) "path is not a directory"
|
2015-05-22 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Only files have the .Directory attribute.
|
|
|
|
If ( $state -eq "file" -and -not $fileinfo.Directory )
|
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
Fail-Json (New-Object psobject) "path is not a file"
|
2015-05-22 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Else
|
2015-05-29 03:57:13 +02:00
|
|
|
# doesn't yet exist
|
2015-05-22 20:25:05 +02:00
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
If ( $state -eq "unspecified" )
|
|
|
|
{
|
|
|
|
$basename = Split-Path -Path $path -Leaf
|
|
|
|
If ($basename.length -gt 0)
|
|
|
|
{
|
|
|
|
$state = "file"
|
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
$state = "directory"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 20:25:05 +02:00
|
|
|
If ( $state -eq "directory" )
|
|
|
|
{
|
|
|
|
New-Item -ItemType directory -Path $path
|
2015-05-29 03:57:13 +02:00
|
|
|
$result.changed = $TRUE
|
2015-05-22 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
If ( $state -eq "file" )
|
|
|
|
{
|
2015-05-29 03:57:13 +02:00
|
|
|
Fail-Json (New-Object psobject) "path will not be created"
|
2015-05-22 20:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:57:13 +02:00
|
|
|
Exit-Json $result
|