@ -5,33 +5,48 @@
# Based on: http://powershellblogger.com/2016/01/create-shortcuts-lnk-or-url-files-with-powershell/
# Based on: http://powershellblogger.com/2016/01/create-shortcuts-lnk-or-url-files-with-powershell/
# Requires -Module Ansible.ModuleUtils.Legacy
#AnsibleRequires -CSharpUtil Ansible.Basic
$ErrorActionPreference = " Stop "
$spec = @ {
options = @ {
$params = Parse-Args -arguments $args -supports_check_mode $true
src = @ { type = 'str' }
$check_mode = Get-AnsibleParam -obj $params -name " _ansible_check_mode " -type " bool " -default $false
dest = @ { type = 'path' ; required = $true }
state = @ { type = 'str' ; default = 'present' ; choices = @ ( 'absent' , 'present' ) }
$orig_src = Get-AnsibleParam -obj $params -name " src "
arguments = @ { type = 'str' ; aliases = @ ( 'args' ) }
$dest = Get-AnsibleParam -obj $params -name " dest " -type " path " -failifempty $true
directory = @ { type = 'path' }
$state = Get-AnsibleParam -obj $params -name " state " -type " str " -default " present " -validateset " absent " , " present "
hotkey = @ { type = 'str' }
$orig_args = Get-AnsibleParam -obj $params -name " args " -type " str "
icon = @ { type = 'path' }
$directory = Get-AnsibleParam -obj $params -name " directory " -type " path "
description = @ { type = 'str' }
$hotkey = Get-AnsibleParam -obj $params -name " hotkey " -type " str "
windowstyle = @ { type = 'str' ; choices = @ ( 'maximized' , 'minimized' , 'normal' ) }
$icon = Get-AnsibleParam -obj $params -name " icon " -type " path "
}
$orig_description = Get-AnsibleParam -obj $params -name " description " -type " str "
supports_check_mode = $true
$windowstyle = Get-AnsibleParam -obj $params -name " windowstyle " -type " str " -validateset " maximized " , " minimized " , " normal "
}
$module = [ Ansible.Basic.AnsibleModule ] :: Create ( $args , $spec )
$src = $module . Params . src
$dest = $module . Params . dest
$state = $module . Params . state
$arguments = $module . Params . arguments # NOTE: Variable $args is a special variable
$directory = $module . Params . directory
$hotkey = $module . Params . hotkey
$icon = $module . Params . icon
$description = $module . Params . description
$windowstyle = $module . Params . windowstyle
# Expand environment variables on non-path types
# Expand environment variables on non-path types
$args = Expand-Environment ( $orig_args )
if ( $null -ne $src ) {
$description = Expand-Environment ( $orig_description )
$src = [ System.Environment ] :: ExpandEnvironmentVariables ( $src )
$src = Expand-Environment ( $orig_src )
$result = @ {
changed = $false
dest = $dest
state = $state
}
}
if ( $null -ne $arguments ) {
$arguments = [ System.Environment ] :: ExpandEnvironmentVariables ( $arguments )
}
if ( $null -ne $description ) {
$description = [ System.Environment ] :: ExpandEnvironmentVariables ( $description )
}
$module . Result . dest = $dest
$module . Result . state = $state
# Convert from window style name to window style id
# Convert from window style name to window style id
$windowstyles = @ {
$windowstyles = @ {
@ -47,13 +62,13 @@ If ($state -eq "absent") {
If ( Test-Path -Path $dest ) {
If ( Test-Path -Path $dest ) {
# If the shortcut exists, try to remove it
# If the shortcut exists, try to remove it
Try {
Try {
Remove-Item -Path $dest -WhatIf: $ check_m ode
Remove-Item -Path $dest -WhatIf: $ module. CheckM ode
} Catch {
} Catch {
# Report removal failure
# Report removal failure
Fail-Json -obj $result -message " Failed to remove shortcut ' $dest '. ( $( $_ . Exception . Message ) ) "
$module . FailJson ( " Failed to remove shortcut ' $dest '. ( $( $_ . Exception . Message ) ) " , $_ )
}
}
# Report removal success
# Report removal success
$ r esult. changed = $true
$ module. R esult. changed = $true
} Else {
} Else {
# Nothing to report, everything is fine already
# Nothing to report, everything is fine already
}
}
@ -64,23 +79,23 @@ If ($state -eq "absent") {
# Compare existing values with new values, report as changed if required
# Compare existing values with new values, report as changed if required
If ( $src -ne $null ) {
If ( $null -ne $src ) {
# Windows translates executables to absolute path, so do we
# Windows translates executables to absolute path, so do we
If ( Get-Command -Name $src -Type Application -ErrorAction SilentlyContinue ) {
If ( Get-Command -Name $src -Type Application -ErrorAction SilentlyContinue ) {
$src = ( Get-Command -Name $src -Type Application ) . Definition
$src = ( Get-Command -Name $src -Type Application ) . Definition
}
}
If ( -not ( Test-Path -Path $src -IsValid ) ) {
If ( -not ( Test-Path -Path $src -IsValid ) ) {
If ( -not ( Split-Path -Path $src -IsAbsolute ) ) {
If ( -not ( Split-Path -Path $src -IsAbsolute ) ) {
Fail-Json -obj $result -message " Source ' $src ' is not found in PATH and not a valid or absolute path. "
$module . FailJson ( " Source ' $src ' is not found in PATH and not a valid or absolute path. " )
}
}
}
}
}
}
If ( $src -ne $null -and $ShortCut . TargetPath -ne $src ) {
If ( ( $null -ne $src ) -and ( $ShortCut . TargetPath -ne $src ) ) {
$ r esult. changed = $true
$ module. R esult. changed = $true
$ShortCut . TargetPath = $src
$ShortCut . TargetPath = $src
}
}
$ r esult. src = $ShortCut . TargetPath
$ module. R esult. src = $ShortCut . TargetPath
# Determine if we have a WshShortcut or WshUrlShortcut by checking the Arguments property
# Determine if we have a WshShortcut or WshUrlShortcut by checking the Arguments property
# A WshUrlShortcut objects only consists of a TargetPath property
# A WshUrlShortcut objects only consists of a TargetPath property
@ -88,51 +103,51 @@ If ($state -eq "absent") {
If ( Get-Member -InputObject $ShortCut -Name Arguments ) {
If ( Get-Member -InputObject $ShortCut -Name Arguments ) {
# This is a full-featured application shortcut !
# This is a full-featured application shortcut !
If ( $orig_args -ne $null -and $ShortCut . Arguments -ne $arg s) {
If ( ( $null -ne $arguments ) -and ( $ShortCut . Arguments -ne $arg ument s) ) {
$ r esult. changed = $true
$ module. R esult. changed = $true
$ShortCut . Arguments = $arg s
$ShortCut . Arguments = $arg ument s
}
}
$ r esult. args = $ShortCut . Arguments
$ module. R esult. args = $ShortCut . Arguments
If ( $directory -ne $null -and $ShortCut . WorkingDirectory -ne $directory ) {
If ( ( $null -ne $directory ) -and ( $ShortCut . WorkingDirectory -ne $directory ) ) {
$ r esult. changed = $true
$ module. R esult. changed = $true
$ShortCut . WorkingDirectory = $directory
$ShortCut . WorkingDirectory = $directory
}
}
$ r esult. directory = $ShortCut . WorkingDirectory
$ module. R esult. directory = $ShortCut . WorkingDirectory
# FIXME: Not all values are accepted here ! Improve docs too.
# FIXME: Not all values are accepted here ! Improve docs too.
If ( $hotkey -ne $null -and $ShortCut . Hotkey -ne $hotkey ) {
If ( ( $null -ne $hotkey ) -and ( $ShortCut . Hotkey -ne $hotkey ) ) {
$ r esult. changed = $true
$ module. R esult. changed = $true
$ShortCut . Hotkey = $hotkey
$ShortCut . Hotkey = $hotkey
}
}
$ r esult. hotkey = $ShortCut . Hotkey
$ module. R esult. hotkey = $ShortCut . Hotkey
If ( $icon -ne $null -and $ShortCut . IconLocation -ne $icon ) {
If ( ( $null -ne $icon ) -and ( $ShortCut . IconLocation -ne $icon ) ) {
$ r esult. changed = $true
$ module. R esult. changed = $true
$ShortCut . IconLocation = $icon
$ShortCut . IconLocation = $icon
}
}
$ r esult. icon = $ShortCut . IconLocation
$ module. R esult. icon = $ShortCut . IconLocation
If ( $orig_description -ne $null -and $ShortCut . Description -ne $description ) {
If ( ( $null -ne $description ) -and ( $ShortCut . Description -ne $description ) ) {
$ r esult. changed = $true
$ module. R esult. changed = $true
$ShortCut . Description = $description
$ShortCut . Description = $description
}
}
$ r esult. description = $ShortCut . Description
$ module. R esult. description = $ShortCut . Description
If ( $windowstyle -ne $null -and $ShortCut . WindowStyle -ne $windowstyles . $windowstyle ) {
If ( ( $null -ne $windowstyle ) -and ( $ShortCut . WindowStyle -ne $windowstyles . $windowstyle ) ) {
$ r esult. changed = $true
$ module. R esult. changed = $true
$ShortCut . WindowStyle = $windowstyles . $windowstyle
$ShortCut . WindowStyle = $windowstyles . $windowstyle
}
}
$ r esult. windowstyle = $windowstyleids [ $ShortCut . WindowStyle ]
$ module. R esult. windowstyle = $windowstyleids [ $ShortCut . WindowStyle ]
}
}
If ( $result . changed -eq $true -and $check_mode -ne $true ) {
If ( ( $module . Result . changed -eq $true ) -and ( $module . CheckMode -ne $true ) ) {
Try {
Try {
$ShortCut . Save ( )
$ShortCut . Save ( )
} Catch {
} Catch {
Fail-Json -obj $result -message " Failed to create shortcut ' $dest '. ( $( $_ . Exception . Message ) ) "
$module . FailJson ( " Failed to create shortcut ' $dest '. ( $( $_ . Exception . Message ) ) " , $_ )
}
}
}
}
}
}
Exit-Json -obj $result
$module . ExitJson ( )