@ -20,36 +20,39 @@
# WANT_JSON
# POWERSHELL_COMMON
$ params = Parse-Args $args ;
$result = New-Object PSObject ;
Set-Attr $result " changed " $false ;
$ result = @ {
changed = $false
}
$package = Get-Attr -obj $params -name name -failifempty $true -emptyattributefailmessage " missing required argument: name "
$force = Get-Attr -obj $params -name force -default " false " | ConvertTo-Bool
$upgrade = Get-Attr -obj $params -name upgrade -default " false " | ConvertTo-Bool
$version = Get-Attr -obj $params -name version -default $null
$params = Parse-Args $args
$package = Get-AnsibleParam -obj $params -name " name " -type " str " -failifempty $true
$force = Get-AnsibleParam -obj $params -name " force " -type " bool " -default $false
$upgrade = Get-AnsibleParam -obj $params -name " upgrade " -type " bool " -default $false
$version = Get-AnsibleParam -obj $params -name " version " -type " str "
$source = Get-AnsibleParam -obj $params -name " source " -type " str "
$showlog = Get-AnsibleParam -obj $params -name " showlog " -type " bool " -default $false
$state = Get-AnsibleParam -obj $params -name " state " -type " str " -default " present " -validateset " present " , " absent " , " latest "
$installargs = Get-AnsibleParam -obj $params -name " install_args " -type " str "
$packageparams = Get-AnsibleParam -obj $params -name " params " -type " str "
$allowemptychecksums = Get-AnsibleParam -obj $params -name " allow_empty_checksums " -type " bool " -default $false
$ignorechecksums = Get-AnsibleParam -obj $params -name " ignore_checksums " -type " bool " -default $false
$ignoredependencies = Get-AnsibleParam -obj $params -name " ignore_dependencies " -type " bool " -default $false
$source = Get-Attr -obj $params -name source -default $null
if ( $source ) { $source = $source . Tolower ( ) }
$showlog = Get-Attr -obj $params -name showlog -default " false " | ConvertTo-Bool
$state = Get-Attr -obj $params -name state -default " present "
$installargs = Get-Attr -obj $params -name install_args -default $null
$packageparams = Get-Attr -obj $params -name params -default $null
$allowemptychecksums = Get-Attr -obj $params -name allow_empty_checksums -default " false " | ConvertTo-Bool
$ignorechecksums = Get-Attr -obj $params -name ignore_checksums -default " false " | ConvertTo-Bool
$ignoredependencies = Get-Attr -obj $params -name ignore_dependencies -default " false " | ConvertTo-Bool
# as of chocolatey 0.9.10, nonzero success exit codes can be returned
# see https://github.com/chocolatey/choco/issues/512#issuecomment-214284461
$successexitcodes = ( 0 , 1605 , 1614 , 1641 , 3010 )
if ( " present " , " absent " -notcontains $state )
if ( $upgrade )
{
Fail-Json $result " state is $state ; must be present or absent "
Add-DeprecateWarning $result " Parameter upgrade=yes is replaced with state=latest "
if ( $state -eq " present " )
{
$state = " latest "
}
}
# As of chocolatey 0.9.10, nonzero success exit codes can be returned
# See https://github.com/chocolatey/choco/issues/512#issuecomment-214284461
$successexitcodes = ( 0 , 1605 , 1614 , 1641 , 3010 )
Function Chocolatey-Install-Upgrade
{
@ -60,24 +63,28 @@ Function Chocolatey-Install-Upgrade
$ChocoAlreadyInstalled = get-command choco -ErrorAction 0
if ( $ChocoAlreadyInstalled -eq $null )
{
#We need to install chocolatey
$install_output = ( new-object net . webclient ) . DownloadString ( " https://chocolatey.org/install.ps1 " ) | powershell -
if ( $LASTEXITCODE -ne 0 )
{
Set-Attr $result " choco_bootstrap_output " $install_output
$result . choco_bootstrap_output = $install_output
Fail-Json $result " Chocolatey bootstrap installation failed. "
}
$result . changed = $true
$script:executable = " C:\ProgramData\chocolatey\bin\choco.exe "
}
else
{
$script:executable = " choco.exe "
if ( [ Version ] ( choco - -version ) -lt [ Version ] '0.9.9' )
{
Choco-Upgrade chocolatey
}
}
}
@ -92,12 +99,13 @@ Function Choco-IsInstalled
)
$cmd = " $executable list --local-only $package "
$ results = invoke-expression $cmd
$ output = invoke-expression $cmd
$result . rc = $LastExitCode
if ( $LastExitCode -ne 0 )
{
Set-Attr $result " choco_error_cmd " $cmd
Set-Attr $result " choco_error_log " " $results "
$result . choco_error_cmd = $cmd
$result . choco_error_log = $output
Throw " Error checking installation status for $package "
}
@ -107,7 +115,7 @@ Function Choco-IsInstalled
return $matches [ 1 ] -gt 0
}
$false
return $false
}
Function Choco-Upgrade
@ -182,12 +190,13 @@ Function Choco-Upgrade
$cmd + = " -ignoredependencies "
}
$ results = invoke-expression $cmd
$ output = invoke-expression $cmd
$result . rc = $LastExitCode
if ( $LastExitCode -notin $successexitcodes )
{
Set-Attr $result " choco_error_cmd " $cmd
Set-Attr $result " choco_error_log " " $results "
$result . choco_error_cmd = $cmd
$result . choco_error_log = $output
Throw " Error installing $package "
}
@ -227,24 +236,11 @@ Function Choco-Install
[ bool ] $ignoredependencies
)
if ( Choco-IsInstalled $package )
if ( ( Choco-IsInstalled $package ) -and -not $force )
{
if ( $upgrade )
{
Choco-Upgrade -package $package -version $version -source $source -force $force `
-installargs $installargs -packageparams $packageparams `
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
-ignoredependencies $ignoredependencies
return
}
if ( -not $force )
{
return
}
}
$cmd = " $executable install -dv -y $package "
if ( $version )
@ -287,12 +283,13 @@ Function Choco-Install
$cmd + = " -ignoredependencies "
}
$ results = invoke-expression $cmd
$ output = invoke-expression $cmd
$result . rc = $LastExitCode
if ( $LastExitCode -notin $successexitcodes )
{
Set-Attr $result " choco_error_cmd " $cmd
Set-Attr $result " choco_error_log " " $results "
$result . choco_error_cmd = $cmd
$result . choco_error_log = $output
Throw " Error installing $package "
}
@ -334,38 +331,45 @@ Function Choco-Uninstall
$cmd + = " -params ' $packageparams ' "
}
$ results = invoke-expression $cmd
$ output = invoke-expression $cmd
$result . rc = $LastExitCode
if ( $LastExitCode -notin $successexitcodes )
{
Set-Attr $result " choco_error_cmd " $cmd
Set-Attr $result " choco_error_log " " $results "
$result . choco_error_cmd = $cmd
$result . choco_error_log = $output
Throw " Error uninstalling $package "
}
$result . changed = $true
}
Try
{
Chocolatey-Install -Upgrade
if ( $state -eq " present " )
{
Choco-Install -package $package -version $version -source $source `
-force $force -upgrade $upgrade -installargs $installargs `
-packageparams $packageparams -allowemptychecksums $allowemptychecksums `
-ignorechecksums $ignorechecksums -ignoredependencies $ignoredependencies
Choco-Install -package $package -version $version -source $source -force $force `
-installargs $installargs -packageparams $packageparams `
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
-ignoredependencies $ignoredependencies
}
elseif ( $state -eq " latest " )
{
Choco-Upgrade -package $package -version $version -source $source -force $force `
-installargs $installargs -packageparams $packageparams `
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
-ignoredependencies $ignoredependencies
}
else
else if ( $state -eq " absent " )
{
Choco-Uninstall -package $package -version $version -force $force
}
Exit-Json $result ;
Exit-Json $result
}
Catch
{
Fail-Json $result $_ . Exception . Message
}