From 1d074d43aa8584f8de01edaee1ffe456a5ab4844 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Sat, 22 Aug 2015 19:01:11 -0400 Subject: [PATCH] * Update core modules to fix strict mode errors. * Also fix creates parameter issue in win_msi as described in https://github.com/ansible/ansible-modules-core/issues/129, slightly different fix from https://github.com/ansible/ansible-modules-core/pull/1482 * Fix setup.ps1 module issue described in https://github.com/ansible/ansible-modules-core/issues/1927 --- windows/setup.ps1 | 2 +- windows/win_feature.ps1 | 50 ++++++++------------------------------ windows/win_group.ps1 | 28 ++++++++++------------ windows/win_msi.ps1 | 32 ++++++++++--------------- windows/win_service.ps1 | 17 +++++++------ windows/win_stat.ps1 | 8 +++---- windows/win_user.ps1 | 53 ++++++++++++++--------------------------- 7 files changed, 65 insertions(+), 125 deletions(-) diff --git a/windows/setup.ps1 b/windows/setup.ps1 index bd2f6ac8c76..3e3317d0450 100644 --- a/windows/setup.ps1 +++ b/windows/setup.ps1 @@ -60,7 +60,7 @@ Set-Attr $result.ansible_facts "ansible_hostname" $env:COMPUTERNAME; Set-Attr $result.ansible_facts "ansible_fqdn" "$([System.Net.Dns]::GetHostByName((hostname)).HostName)" Set-Attr $result.ansible_facts "ansible_system" $osversion.Platform.ToString() Set-Attr $result.ansible_facts "ansible_os_family" "Windows" -Set-Attr $result.ansible_facts "ansible_os_name" $win32_os.Name.Split('|')[0] +Set-Attr $result.ansible_facts "ansible_os_name" ($win32_os.Name.Split('|')[0]).Trim() Set-Attr $result.ansible_facts "ansible_distribution" $osversion.VersionString Set-Attr $result.ansible_facts "ansible_distribution_version" $osversion.Version.ToString() diff --git a/windows/win_feature.ps1 b/windows/win_feature.ps1 index 458d942e328..ec6317fb89b 100644 --- a/windows/win_feature.ps1 +++ b/windows/win_feature.ps1 @@ -27,48 +27,18 @@ $result = New-Object PSObject -Property @{ changed = $false } -If ($params.name) { - $name = $params.name -split ',' | % { $_.Trim() } -} -Else { - Fail-Json $result "mising required argument: name" +$name = Get-Attr $params "name" -failifempty $true +$name = $name -split ',' | % { $_.Trim() } + +$state = Get-Attr $params "state" "present" +$state = $state.ToString().ToLower() +If (($state -ne 'present') -and ($state -ne 'absent')) { + Fail-Json $result "state is '$state'; must be 'present' or 'absent'" } -If ($params.state) { - $state = $params.state.ToString().ToLower() - If (($state -ne 'present') -and ($state -ne 'absent')) { - Fail-Json $result "state is '$state'; must be 'present' or 'absent'" - } -} -Elseif (!$params.state) { - $state = "present" -} - -If ($params.restart) { - $restart = $params.restart | ConvertTo-Bool -} -Else -{ - $restart = $false -} - -if ($params.include_sub_features) -{ - $includesubfeatures = $params.include_sub_features | ConvertTo-Bool -} -Else -{ - $includesubfeatures = $false -} - -if ($params.include_management_tools) -{ - $includemanagementtools = $params.include_management_tools | ConvertTo-Bool -} -Else -{ - $includemanagementtools = $false -} +$restart = Get-Attr $params "restart" $false | ConvertTo-Bool +$includesubfeatures = Get-Attr $params "include_sub_features" $false | ConvertTo-Bool +$includemanagementtools = Get-Attr $params "include_management_tools" $false | ConvertTo-Bool If ($state -eq "present") { try { diff --git a/windows/win_group.ps1 b/windows/win_group.ps1 index febaf47d014..c3fc920c916 100644 --- a/windows/win_group.ps1 +++ b/windows/win_group.ps1 @@ -24,35 +24,31 @@ $params = Parse-Args $args; $result = New-Object PSObject; Set-Attr $result "changed" $false; -If (-not $params.name.GetType) { - Fail-Json $result "missing required arguments: name" +$name = Get-Attr $params "name" -failifempty $true + +$state = Get-Attr $params "state" "present" +$state = $state.ToString().ToLower() +If (($state -ne "present") -and ($state -ne "absent")) { + Fail-Json $result "state is '$state'; must be 'present' or 'absent'" } -If ($params.state) { - $state = $params.state.ToString().ToLower() - If (($state -ne "present") -and ($state -ne "absent")) { - Fail-Json $result "state is '$state'; must be 'present' or 'absent'" - } -} -Elseif (-not $params.state) { - $state = "present" -} +$description = Get-Attr $params "description" $null $adsi = [ADSI]"WinNT://$env:COMPUTERNAME" -$group = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'group' -and $_.Name -eq $params.name } +$group = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'group' -and $_.Name -eq $name } try { If ($state -eq "present") { If (-not $group) { - $group = $adsi.Create("Group", $params.name) + $group = $adsi.Create("Group", $name) $group.SetInfo() Set-Attr $result "changed" $true } - If ($params.description.GetType) { - IF (-not $group.description -or $group.description -ne $params.description) { - $group.description = $params.description + If ($null -ne $description) { + IF (-not $group.description -or $group.description -ne $description) { + $group.description = $description $group.SetInfo() Set-Attr $result "changed" $true } diff --git a/windows/win_msi.ps1 b/windows/win_msi.ps1 index 1c2bc8a3019..f1381e9bf23 100644 --- a/windows/win_msi.ps1 +++ b/windows/win_msi.ps1 @@ -21,36 +21,28 @@ $params = Parse-Args $args; -$result = New-Object psobject; -Set-Attr $result "changed" $false; +$path = Get-Attr $params "path" -failifempty $true +$state = Get-Attr $params "state" "present" +$creates = Get-Attr $params "creates" $false +$extra_args = Get-Attr $params "extra_args" "" -If (-not $params.path.GetType) -{ - Fail-Json $result "missing required arguments: path" -} +$result = New-Object psobject @{ + changed = $false +}; -$extra_args = "" -If ($params.extra_args.GetType) +If (($creates -ne $false) -and ($state -ne "absent") -and (Test-Path $creates)) { - $extra_args = $params.extra_args; -} - -If ($params.creates.GetType -and $params.state.GetType -and $params.state -ne "absent") -{ - If (Test-File $creates) - { - Exit-Json $result; - } + Exit-Json $result; } $logfile = [IO.Path]::GetTempFileName(); -if ($params.state.GetType -and $params.state -eq "absent") +if ($state -eq "absent") { - msiexec.exe /x $params.path /qb /l $logfile $extra_args; + msiexec.exe /x $path /qn /l $logfile $extra_args } Else { - msiexec.exe /i $params.path /qb /l $logfile $extra_args; + msiexec.exe /i $path /qn /l $logfile $extra_args } Set-Attr $result "changed" $true; diff --git a/windows/win_service.ps1 b/windows/win_service.ps1 index a70d82a4ef3..4ea4e2697a1 100644 --- a/windows/win_service.ps1 +++ b/windows/win_service.ps1 @@ -24,26 +24,25 @@ $params = Parse-Args $args; $result = New-Object PSObject; Set-Attr $result "changed" $false; -If (-not $params.name.GetType) -{ - Fail-Json $result "missing required arguments: name" -} +$name = Get-Attr $params "name" -failifempty $true +$state = Get-Attr $params "state" $false +$startMode = Get-Attr $params "start_mode" $false -If ($params.state) { - $state = $params.state.ToString().ToLower() +If ($state) { + $state = $state.ToString().ToLower() If (($state -ne 'started') -and ($state -ne 'stopped') -and ($state -ne 'restarted')) { Fail-Json $result "state is '$state'; must be 'started', 'stopped', or 'restarted'" } } -If ($params.start_mode) { - $startMode = $params.start_mode.ToString().ToLower() +If ($startMode) { + $startMode = $startMode.ToString().ToLower() If (($startMode -ne 'auto') -and ($startMode -ne 'manual') -and ($startMode -ne 'disabled')) { Fail-Json $result "start mode is '$startMode'; must be 'auto', 'manual', or 'disabled'" } } -$svcName = $params.name +$svcName = $name $svc = Get-Service -Name $svcName -ErrorAction SilentlyContinue If (-not $svc) { Fail-Json $result "Service '$svcName' not installed" diff --git a/windows/win_stat.ps1 b/windows/win_stat.ps1 index cf8c14a4d49..af9cbd7eca5 100644 --- a/windows/win_stat.ps1 +++ b/windows/win_stat.ps1 @@ -42,14 +42,14 @@ If (Test-Path $path) Set-Attr $result.stat "exists" $TRUE; $info = Get-Item $path; $epoch_date = Get-Date -Date "01/01/1970" - If ($info.Directory) # Only files have the .Directory attribute. + If ($info.PSIsContainer) { - Set-Attr $result.stat "isdir" $FALSE; - Set-Attr $result.stat "size" $info.Length; + Set-Attr $result.stat "isdir" $TRUE; } Else { - Set-Attr $result.stat "isdir" $TRUE; + Set-Attr $result.stat "isdir" $FALSE; + Set-Attr $result.stat "size" $info.Length; } Set-Attr $result.stat "extension" $info.Extension; Set-Attr $result.stat "attributes" $info.Attributes.ToString(); diff --git a/windows/win_user.ps1 b/windows/win_user.ps1 index b7be7e4eea3..ac40ced2cbc 100644 --- a/windows/win_user.ps1 +++ b/windows/win_user.ps1 @@ -16,7 +16,6 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -# WANT_JSON # POWERSHELL_COMMON ######## @@ -55,33 +54,21 @@ $result = New-Object psobject @{ changed = $false }; -If (-not $params.name.GetType) { - Fail-Json $result "missing required arguments: name" -} - -$username = Get-Attr $params "name" +$username = Get-Attr $params "name" -failifempty $true $fullname = Get-Attr $params "fullname" $description = Get-Attr $params "description" $password = Get-Attr $params "password" -If ($params.state) { - $state = $params.state.ToString().ToLower() - If (($state -ne 'present') -and ($state -ne 'absent') -and ($state -ne 'query')) { - Fail-Json $result "state is '$state'; must be 'present', 'absent' or 'query'" - } -} -ElseIf (!$params.state) { - $state = "present" +$state = Get-Attr $params "state" "present" +$state = $state.ToString().ToLower() +If (($state -ne 'present') -and ($state -ne 'absent') -and ($state -ne 'query')) { + Fail-Json $result "state is '$state'; must be 'present', 'absent' or 'query'" } -If ($params.update_password) { - $update_password = $params.update_password.ToString().ToLower() - If (($update_password -ne 'always') -and ($update_password -ne 'on_create')) { - Fail-Json $result "update_password is '$update_password'; must be 'always' or 'on_create'" - } -} -ElseIf (!$params.update_password) { - $update_password = "always" +$update_password = Get-Attr $params "update_password" "always" +$update_password = $update_password.ToString().ToLower() +If (($update_password -ne 'always') -and ($update_password -ne 'on_create')) { + Fail-Json $result "update_password is '$update_password'; must be 'always' or 'on_create'" } $password_expired = Get-Attr $params "password_expired" $null @@ -126,14 +113,10 @@ If ($groups -ne $null) { } } -If ($params.groups_action) { - $groups_action = $params.groups_action.ToString().ToLower() - If (($groups_action -ne 'replace') -and ($groups_action -ne 'add') -and ($groups_action -ne 'remove')) { - Fail-Json $result "groups_action is '$groups_action'; must be 'replace', 'add' or 'remove'" - } -} -ElseIf (!$params.groups_action) { - $groups_action = "replace" +$groups_action = Get-Attr $params "groups_action" "replace" +$groups_action = $groups_action.ToString().ToLower() +If (($groups_action -ne 'replace') -and ($groups_action -ne 'add') -and ($groups_action -ne 'remove')) { + Fail-Json $result "groups_action is '$groups_action'; must be 'replace', 'add' or 'remove'" } $user_obj = Get-User $username @@ -141,7 +124,7 @@ $user_obj = Get-User $username If ($state -eq 'present') { # Add or update user try { - If (!$user_obj.GetType) { + If (-not $user_obj -or -not $user_obj.GetType) { $user_obj = $adsi.Create("User", $username) If ($password -ne $null) { $user_obj.SetPassword($password) @@ -200,13 +183,13 @@ If ($state -eq 'present') { If ($result.changed) { $user_obj.SetInfo() } - If ($groups.GetType) { + If ($null -ne $groups) { [string[]]$current_groups = $user_obj.Groups() | ForEach { $_.GetType().InvokeMember("Name", "GetProperty", $null, $_, $null) } If (($groups_action -eq "remove") -or ($groups_action -eq "replace")) { ForEach ($grp in $current_groups) { If ((($groups_action -eq "remove") -and ($groups -contains $grp)) -or (($groups_action -eq "replace") -and ($groups -notcontains $grp))) { $group_obj = $adsi.Children | where { $_.SchemaClassName -eq 'Group' -and $_.Name -eq $grp } - If ($group_obj.GetType) { + If ($group_obj -and $group_obj.GetType) { $group_obj.Remove($user_obj.Path) $result.changed = $true } @@ -239,7 +222,7 @@ If ($state -eq 'present') { ElseIf ($state -eq 'absent') { # Remove user try { - If ($user_obj.GetType) { + If ($user_obj -and $user_obj.GetType) { $username = $user_obj.Name.Value $adsi.delete("User", $user_obj.Name.Value) $result.changed = $true @@ -252,7 +235,7 @@ ElseIf ($state -eq 'absent') { } try { - If ($user_obj.GetType) { + If ($user_obj -and $user_obj.GetType) { $user_obj.RefreshCache() Set-Attr $result "name" $user_obj.Name[0] Set-Attr $result "fullname" $user_obj.FullName[0]