ansible/windows/win_file.ps1

116 lines
2.7 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
2016-01-05 01:05:43 +01:00
$ErrorActionPreference = "Stop"
$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
{
2015-06-27 23:34:16 +02:00
echo $null > $path
2015-05-22 20:25:05 +02:00
}
$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
{
2015-10-13 07:15:25 +02:00
If ( $state -eq "directory" -and -not $fileinfo.PsIsContainer )
2015-05-22 20:25:05 +02:00
{
Fail-Json (New-Object psobject) "path is not a directory"
2015-05-22 20:25:05 +02:00
}
2015-10-13 07:15:25 +02:00
If ( $state -eq "file" -and $fileinfo.PsIsContainer )
2015-05-22 20:25:05 +02:00
{
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