fix win_user type checking

Fixed type checking to be more idiomatic powershell, also fixes a slew of StrictMode issues and gets error handling back to originally-intended behavior.
This commit is contained in:
nitzmahone 2016-04-07 18:25:41 -07:00
parent ee90762388
commit 2f46c35f47

View file

@ -102,10 +102,10 @@ If ($account_locked -ne $null) {
$groups = Get-Attr $params "groups" $null $groups = Get-Attr $params "groups" $null
If ($groups -ne $null) { If ($groups -ne $null) {
If ($groups.GetType().Name -eq "String") { If ($groups -is [System.String]) {
[string[]]$groups = $groups.Split(",") [string[]]$groups = $groups.Split(",")
} }
ElseIf ($groups.GetType().Name -ne "Object[]") { ElseIf ($groups -isnot [System.Collections.IList]) {
Fail-Json $result "groups must be a string or array" Fail-Json $result "groups must be a string or array"
} }
$groups = $groups | ForEach { ([string]$_).Trim() } | Where { $_ } $groups = $groups | ForEach { ([string]$_).Trim() } | Where { $_ }
@ -125,7 +125,7 @@ $user_obj = Get-User $username
If ($state -eq 'present') { If ($state -eq 'present') {
# Add or update user # Add or update user
try { try {
If (-not $user_obj -or -not $user_obj.GetType) { If (-not $user_obj) {
$user_obj = $adsi.Create("User", $username) $user_obj = $adsi.Create("User", $username)
If ($password -ne $null) { If ($password -ne $null) {
$user_obj.SetPassword($password) $user_obj.SetPassword($password)
@ -190,7 +190,7 @@ If ($state -eq 'present') {
ForEach ($grp in $current_groups) { ForEach ($grp in $current_groups) {
If ((($groups_action -eq "remove") -and ($groups -contains $grp)) -or (($groups_action -eq "replace") -and ($groups -notcontains $grp))) { 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 } $group_obj = $adsi.Children | where { $_.SchemaClassName -eq 'Group' -and $_.Name -eq $grp }
If ($group_obj -and $group_obj.GetType) { If ($group_obj) {
$group_obj.Remove($user_obj.Path) $group_obj.Remove($user_obj.Path)
$result.changed = $true $result.changed = $true
} }
@ -204,7 +204,7 @@ If ($state -eq 'present') {
ForEach ($grp in $groups) { ForEach ($grp in $groups) {
If ($current_groups -notcontains $grp) { If ($current_groups -notcontains $grp) {
$group_obj = $adsi.Children | where { $_.SchemaClassName -eq 'Group' -and $_.Name -eq $grp } $group_obj = $adsi.Children | where { $_.SchemaClassName -eq 'Group' -and $_.Name -eq $grp }
If ($group_obj.GetType) { If ($group_obj) {
$group_obj.Add($user_obj.Path) $group_obj.Add($user_obj.Path)
$result.changed = $true $result.changed = $true
} }
@ -223,7 +223,7 @@ If ($state -eq 'present') {
ElseIf ($state -eq 'absent') { ElseIf ($state -eq 'absent') {
# Remove user # Remove user
try { try {
If ($user_obj -and $user_obj.GetType) { If ($user_obj) {
$username = $user_obj.Name.Value $username = $user_obj.Name.Value
$adsi.delete("User", $user_obj.Name.Value) $adsi.delete("User", $user_obj.Name.Value)
$result.changed = $true $result.changed = $true
@ -236,7 +236,7 @@ ElseIf ($state -eq 'absent') {
} }
try { try {
If ($user_obj -and $user_obj.GetType) { If ($user_obj -and $user_obj -is [System.DirectoryServices.DirectoryEntry]) {
$user_obj.RefreshCache() $user_obj.RefreshCache()
Set-Attr $result "name" $user_obj.Name[0] Set-Attr $result "name" $user_obj.Name[0]
Set-Attr $result "fullname" $user_obj.FullName[0] Set-Attr $result "fullname" $user_obj.FullName[0]