2014-12-30 15:42:00 +01:00
|
|
|
#!powershell
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
2015-06-18 23:15:15 +02:00
|
|
|
# Copyright 2015, Phil Schwartz <schwartzmx@gmail.com>
|
2014-12-30 15:42:00 +01:00
|
|
|
#
|
|
|
|
# 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-11-02 21:08:00 +01:00
|
|
|
|
2014-12-30 15:42:00 +01:00
|
|
|
$params = Parse-Args $args;
|
|
|
|
|
|
|
|
$result = New-Object psobject @{
|
|
|
|
win_unzip = New-Object psobject
|
|
|
|
changed = $false
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:47:30 +01:00
|
|
|
$creates = Get-AnsibleParam -obj $params -name "creates"
|
|
|
|
If ($creates -ne $null) {
|
2015-06-23 01:51:58 +02:00
|
|
|
If (Test-Path $params.creates) {
|
|
|
|
Exit-Json $result "The 'creates' file or directory already exists."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:47:30 +01:00
|
|
|
$src = Get-AnsibleParam -obj $params -name "src" -failifempty $true
|
|
|
|
If (-Not (Test-Path -path $src)){
|
|
|
|
Fail-Json $result "src file: $src does not exist."
|
2014-12-30 15:42:00 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 15:47:30 +01:00
|
|
|
$ext = [System.IO.Path]::GetExtension($src)
|
2014-12-30 15:42:00 +01:00
|
|
|
|
|
|
|
|
2015-10-30 15:47:30 +01:00
|
|
|
$dest = Get-AnsibleParam -obj $params -name "dest" -failifempty $true
|
|
|
|
If (-Not (Test-Path $dest -PathType Container)){
|
|
|
|
Try{
|
|
|
|
New-Item -itemtype directory -path $dest
|
|
|
|
}
|
|
|
|
Catch {
|
2015-11-02 21:08:00 +01:00
|
|
|
$err_msg = $_.Exception.Message
|
|
|
|
Fail-Json $result "Error creating $dest directory! Msg: $err_msg"
|
2015-10-30 15:47:30 +01:00
|
|
|
}
|
2014-12-30 15:42:00 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 15:47:30 +01:00
|
|
|
$recurse = ConvertTo-Bool (Get-AnsibleParam -obj $params -name "recurse" -default "false")
|
|
|
|
$rm = ConvertTo-Bool (Get-AnsibleParam -obj $params -name "rm" -default "false")
|
2015-01-11 20:03:26 +01:00
|
|
|
|
|
|
|
If ($ext -eq ".zip" -And $recurse -eq $false) {
|
|
|
|
Try {
|
|
|
|
$shell = New-Object -ComObject Shell.Application
|
2016-03-20 12:29:26 +01:00
|
|
|
$zipPkg = $shell.NameSpace([IO.Path]::GetFullPath($src))
|
|
|
|
$destPath = $shell.NameSpace([IO.Path]::GetFullPath($dest))
|
2016-02-11 16:43:12 +01:00
|
|
|
# 20 means do not display any dialog (4) and overwrite any file (16)
|
|
|
|
$destPath.CopyHere($zipPkg.Items(), 20)
|
2015-01-11 20:03:26 +01:00
|
|
|
$result.changed = $true
|
|
|
|
}
|
|
|
|
Catch {
|
2015-11-02 21:08:00 +01:00
|
|
|
$err_msg = $_.Exception.Message
|
|
|
|
Fail-Json $result "Error unzipping $src to $dest! Msg: $err_msg"
|
2015-01-11 20:03:26 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-18 23:15:15 +02:00
|
|
|
# Requires PSCX
|
2015-01-11 20:03:26 +01:00
|
|
|
Else {
|
|
|
|
# Check if PSCX is installed
|
|
|
|
$list = Get-Module -ListAvailable
|
2015-06-18 23:15:15 +02:00
|
|
|
|
2015-01-11 20:03:26 +01:00
|
|
|
If (-Not ($list -match "PSCX")) {
|
2015-06-23 01:51:58 +02:00
|
|
|
Fail-Json $result "PowerShellCommunityExtensions PowerShell Module (PSCX) is required for non-'.zip' compressed archive types."
|
2015-01-11 20:03:26 +01:00
|
|
|
}
|
|
|
|
Else {
|
2015-06-22 15:55:41 +02:00
|
|
|
Set-Attr $result.win_unzip "pscx_status" "present"
|
2015-01-11 20:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Import
|
|
|
|
Try {
|
2015-06-18 23:15:15 +02:00
|
|
|
Import-Module PSCX
|
2015-01-11 20:03:26 +01:00
|
|
|
}
|
|
|
|
Catch {
|
|
|
|
Fail-Json $result "Error importing module PSCX"
|
|
|
|
}
|
|
|
|
|
|
|
|
Try {
|
|
|
|
If ($recurse) {
|
|
|
|
Expand-Archive -Path $src -OutputPath $dest -Force
|
|
|
|
|
2015-07-06 16:59:51 +02:00
|
|
|
If ($rm -eq $true) {
|
2015-01-11 20:03:26 +01:00
|
|
|
Get-ChildItem $dest -recurse | Where {$_.extension -eq ".gz" -Or $_.extension -eq ".zip" -Or $_.extension -eq ".bz2" -Or $_.extension -eq ".tar" -Or $_.extension -eq ".msu"} | % {
|
|
|
|
Expand-Archive $_.FullName -OutputPath $dest -Force
|
|
|
|
Remove-Item $_.FullName -Force
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Else {
|
|
|
|
Get-ChildItem $dest -recurse | Where {$_.extension -eq ".gz" -Or $_.extension -eq ".zip" -Or $_.extension -eq ".bz2" -Or $_.extension -eq ".tar" -Or $_.extension -eq ".msu"} | % {
|
|
|
|
Expand-Archive $_.FullName -OutputPath $dest -Force
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Else {
|
|
|
|
Expand-Archive -Path $src -OutputPath $dest -Force
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Catch {
|
2015-11-02 21:08:00 +01:00
|
|
|
$err_msg = $_.Exception.Message
|
2015-01-11 20:03:26 +01:00
|
|
|
If ($recurse) {
|
2015-11-02 21:08:00 +01:00
|
|
|
Fail-Json $result "Error recursively expanding $src to $dest! Msg: $err_msg"
|
2015-01-11 20:03:26 +01:00
|
|
|
}
|
|
|
|
Else {
|
2015-11-02 21:08:00 +01:00
|
|
|
Fail-Json $result "Error expanding $src to $dest! Msg: $err_msg"
|
2015-01-11 20:03:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
If ($rm -eq $true){
|
|
|
|
Remove-Item $src -Recurse -Force
|
2014-12-30 15:42:00 +01:00
|
|
|
Set-Attr $result.win_unzip "rm" "true"
|
|
|
|
}
|
|
|
|
|
2015-01-11 20:03:26 +01:00
|
|
|
# Fixes a fail error message (when the task actually succeeds) for a "Convert-ToJson: The converted JSON string is in bad format"
|
|
|
|
# This happens when JSON is parsing a string that ends with a "\", which is possible when specifying a directory to download to.
|
|
|
|
# This catches that possible error, before assigning the JSON $result
|
|
|
|
If ($src[$src.length-1] -eq "\") {
|
|
|
|
$src = $src.Substring(0, $src.length-1)
|
|
|
|
}
|
|
|
|
If ($dest[$dest.length-1] -eq "\") {
|
|
|
|
$dest = $dest.Substring(0, $dest.length-1)
|
|
|
|
}
|
|
|
|
Set-Attr $result.win_unzip "src" $src.toString()
|
2014-12-30 15:42:00 +01:00
|
|
|
Set-Attr $result.win_unzip "dest" $dest.toString()
|
2015-01-11 20:03:26 +01:00
|
|
|
Set-Attr $result.win_unzip "recurse" $recurse.toString()
|
2014-12-30 15:42:00 +01:00
|
|
|
|
2015-10-10 00:49:34 +02:00
|
|
|
Exit-Json $result;
|