2014-06-19 15:13:57 +00:00
|
|
|
#!powershell
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
2014-06-19 20:42:07 +00:00
|
|
|
# Copyright 2014, Paul Durivage <paul.durivage@rackspace.com>
|
|
|
|
#
|
2014-06-19 15:13:57 +00:00
|
|
|
# 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
|
|
|
|
|
2014-06-19 21:23:38 +00:00
|
|
|
########
|
|
|
|
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
|
|
|
|
|
|
|
|
function Get-User($user) {
|
|
|
|
$adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
function Create-User([string]$user, [string]$passwd) {
|
2014-06-19 21:40:35 +00:00
|
|
|
$adsiuser = $adsi.Create("User", $user)
|
|
|
|
$adsiuser.SetPassword($passwd)
|
|
|
|
$adsiuser.SetInfo()
|
|
|
|
$adsiuser
|
2014-06-19 21:23:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
function Update-Password($user, [string]$passwd) {
|
|
|
|
$user.SetPassword($passwd)
|
|
|
|
$user.SetInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
function Delete-User($user) {
|
|
|
|
$adsi.delete("user", $user.Name.Value)
|
|
|
|
}
|
|
|
|
########
|
|
|
|
|
2014-06-19 15:13:57 +00:00
|
|
|
$params = Parse-Args $args;
|
|
|
|
|
2014-06-19 20:42:07 +00:00
|
|
|
$result = New-Object psobject @{
|
2014-06-19 21:24:12 +00:00
|
|
|
changed = $false
|
2014-06-19 20:42:07 +00:00
|
|
|
};
|
2014-06-19 15:13:57 +00:00
|
|
|
|
2014-06-19 20:42:07 +00:00
|
|
|
If (-not $params.name.GetType)
|
2014-06-19 15:13:57 +00:00
|
|
|
{
|
2014-06-19 20:42:07 +00:00
|
|
|
Fail-Json $result "missing required arguments: name"
|
2014-06-19 15:13:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 20:42:07 +00:00
|
|
|
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'"
|
2014-06-19 15:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 20:42:07 +00:00
|
|
|
Elseif (!$params.state) {
|
|
|
|
$state = "present"
|
|
|
|
}
|
2014-06-19 15:13:57 +00:00
|
|
|
|
2014-06-19 21:41:05 +00:00
|
|
|
If ((-not $params.password.GetType) -and ($state -eq 'present'))
|
|
|
|
{
|
|
|
|
Fail-Json $result "missing required arguments: password"
|
|
|
|
}
|
|
|
|
|
2014-06-19 20:42:07 +00:00
|
|
|
$username = Get-Attr $params "name"
|
|
|
|
$password = Get-Attr $params "password"
|
|
|
|
|
|
|
|
$user_obj = Get-User $username
|
2014-06-19 15:13:57 +00:00
|
|
|
|
2014-06-19 20:42:07 +00:00
|
|
|
if ($state -eq 'present') {
|
|
|
|
# Add or update user
|
|
|
|
try {
|
2014-06-19 21:41:24 +00:00
|
|
|
if ($user_obj.GetType) {
|
2014-06-19 20:42:07 +00:00
|
|
|
Update-Password $user_obj $password
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Create-User $username $password
|
|
|
|
}
|
|
|
|
$result.changed = $true
|
2014-06-19 21:24:57 +00:00
|
|
|
$user_obj = Get-User $username
|
2014-06-19 20:42:07 +00:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Fail-Json $result $_.Exception.Message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# Remove user
|
|
|
|
try {
|
2014-06-19 21:24:57 +00:00
|
|
|
if ($user_obj.GetType) {
|
|
|
|
Delete-User $user_obj
|
|
|
|
$result.changed = $true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Set-Attr $result "msg" "User '$username' was not found"
|
|
|
|
}
|
2014-06-19 20:42:07 +00:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Fail-Json $result $_.Exception.Message
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 15:13:57 +00:00
|
|
|
|
2014-06-19 21:23:38 +00:00
|
|
|
# Set-Attr $result "user" $user_obj
|
|
|
|
Set-Attr $result "user_name" $user_obj.Name
|
|
|
|
Set-Attr $result "user_fullname" $user_obj.FullName
|
|
|
|
Set-Attr $result "user_path" $user_obj.Path
|
2014-06-19 15:13:57 +00:00
|
|
|
|
2014-06-19 20:42:07 +00:00
|
|
|
Exit-Json $result;
|