ansible/windows/win_file.ps1

117 lines
2.8 KiB
PowerShell
Raw Normal View History

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
$params = Parse-Args $args
2015-05-22 20:25:05 +02:00
# path
$path = Get-Attr $params "path" $FALSE
2015-05-22 20:25:05 +02:00
If ($path -eq $FALSE)
{
$path = Get-Attr $params "dest" $FALSE
2015-05-22 20:25:05 +02:00
If ($path -eq $FALSE)
{
$path = Get-Attr $params "name" $FALSE
2015-05-22 20:25:05 +02:00
If ($path -eq $FALSE)
{
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)
$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-22 20:25:05 +02:00
If ( $state -eq "touch" )
{
If(Test-Path $path)
{
(Get-ChildItem $path).LastWriteTime = Get-Date
}
Else
{
echo $null > $file
}
$result.changed = $TRUE
2015-05-22 20:25:05 +02:00
}
If (Test-Path $path)
{
$fileinfo = Get-Item $path
2015-05-22 20:25:05 +02:00
If ( $state -eq "absent" )
{
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 )
{
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 )
{
Fail-Json (New-Object psobject) "path is not a file"
2015-05-22 20:25:05 +02:00
}
}
}
Else
# doesn't yet exist
2015-05-22 20:25:05 +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
$result.changed = $TRUE
2015-05-22 20:25:05 +02:00
}
If ( $state -eq "file" )
{
Fail-Json (New-Object psobject) "path will not be created"
2015-05-22 20:25:05 +02:00
}
}
Exit-Json $result