You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible/windows/win_unzip.ps1

85 lines
2.3 KiB
PowerShell

10 years ago
#!powershell
# This file is part of Ansible
#
# Copyright 2014, Phil Schwartz <schwartzmx@gmail.com>
#
# 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;
$result = New-Object psobject @{
win_unzip = New-Object psobject
changed = $false
}
If ($params.zip) {
$zip = $params.zip.toString()
If (-Not (Test-Path -path $zip)){
Fail-Json $result "zip file: $zip does not exist."
}
}
Else {
Fail-Json $result "missing required argument: zip"
}
If (-Not($params.dest -eq $null)) {
$dest = $params.dest.toString()
If (-Not (Test-Path $dest -PathType Container)){
Try{
New-Item -itemtype directory -path $dest
}
Catch {
Fail-Json $result "Error creating $dest directory"
}
10 years ago
}
}
Else {
Fail-Json $result "missing required argument: dest"
}
Try {
$shell = New-Object -ComObject Shell.Application
$shell.NameSpace($dest).copyhere(($shell.NameSpace($zip)).items(), 20)
$result.changed = $true
}
Catch {
# Used to allow reboot after exe hotfix extraction (Windows 2008 R2 SP1)
# This will have no effect in most cases.
If (-Not ([System.IO.Path]::GetExtension($zip) -match ".exe")){
10 years ago
$result.changed = $false
Fail-Json $result "Error unzipping $zip to $dest"
}
}
If ($params.rm -eq "true" -Or $params.rm -eq "yes"){
10 years ago
Remove-Item $zip -Recurse -Force
Set-Attr $result.win_unzip "rm" "true"
}
If ($params.restart -eq "true" -Or $params.restart -eq "yes") {
10 years ago
Restart-Computer -Force
Set-Attr $result.win_unzip "restart" "true"
}
Set-Attr $result.win_unzip "zip" $zip.toString()
Set-Attr $result.win_unzip "dest" $dest.toString()
Exit-Json $result;