|
|
@ -1,8 +1,8 @@
|
|
|
|
#!powershell
|
|
|
|
#!powershell
|
|
|
|
# (c) 2014, Matt Martz <matt@sivel.net>, and others
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
# Copyright 2014, Paul Durivage <paul.durivage@rackspace.com>
|
|
|
|
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
@ -21,12 +21,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
$params = Parse-Args $args;
|
|
|
|
$params = Parse-Args $args;
|
|
|
|
|
|
|
|
|
|
|
|
$result = New-Object psobject;
|
|
|
|
$result = New-Object psobject @{
|
|
|
|
Set-Attr $result "changed" $false;
|
|
|
|
changed = false
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
If (-not $params.username.GetType)
|
|
|
|
If (-not $params.name.GetType)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Fail-Json $result "missing required arguments: username"
|
|
|
|
Fail-Json $result "missing required arguments: name"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
If (-not $params.password.GetType)
|
|
|
|
If (-not $params.password.GetType)
|
|
|
@ -34,43 +35,77 @@ If (-not $params.password.GetType)
|
|
|
|
Fail-Json $result "missing required arguments: password"
|
|
|
|
Fail-Json $result "missing required arguments: password"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$extra_args = ""
|
|
|
|
$extra_args = $params "extra_args" ""
|
|
|
|
If ($params.extra_args.GetType)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$extra_args = $params.extra_args;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If ($params.creates.GetType -and $params.state.GetType -and $params.state -ne "absent")
|
|
|
|
If ($params.state) {
|
|
|
|
{
|
|
|
|
$state = $params.state.ToString().ToLower()
|
|
|
|
If (Does-User-Exist $params.username)
|
|
|
|
If (($state -ne 'present') -and ($state -ne 'absent')) {
|
|
|
|
{
|
|
|
|
Fail-Json $result "state is '$state'; must be 'present' or 'absent'"
|
|
|
|
Exit-Json $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Elseif (!$params.state) {
|
|
|
|
$logfile = [IO.Path]::GetTempFileName();
|
|
|
|
$state = "present"
|
|
|
|
if ($params.state.GetType -and $params.state -eq "absent")
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
NET USER $params.username $params.password /ADD
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Set-Attr $result "changed" $true;
|
|
|
|
$username = Get-Attr $params "name"
|
|
|
|
|
|
|
|
$password = Get-Attr $params "password"
|
|
|
|
|
|
|
|
|
|
|
|
$logcontents = Get-Content $logfile;
|
|
|
|
$user_obj = Get-User $username
|
|
|
|
Remove-Item $logfile;
|
|
|
|
if (-not $user_obj) {
|
|
|
|
|
|
|
|
Fail-Json $result "Could not find user: $username"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Set-Attr $result "log" $logcontents;
|
|
|
|
if ($state -eq 'present') {
|
|
|
|
|
|
|
|
# Add or update user
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if ($user_obj) {
|
|
|
|
|
|
|
|
Update-Password $user_obj $password
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
Create-User $username $password
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$result.changed = $true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch {
|
|
|
|
|
|
|
|
Fail-Json $result $_.Exception.Message
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Exit-Json $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
# Remove user
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Delete-User $bob
|
|
|
|
|
|
|
|
$result.changed = $true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch {
|
|
|
|
|
|
|
|
Fail-Json $result $_.Exception.Message
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function Does-User-Exist($username)
|
|
|
|
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
|
|
|
|
{
|
|
|
|
|
|
|
|
$objComputer = [ADSI]("WinNT://$env:COMPUTERNAME,computer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$colUsers = ($objComputer.psbase.children |
|
|
|
|
function Get-User($user) {
|
|
|
|
Where-Object {$_.psBase.schemaClassName -eq "User"} |
|
|
|
|
$adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
|
|
|
|
Select-Object -expand Name)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$blnFound = $colUsers -eq $username
|
|
|
|
function Create-User([string]$user, [string]$passwd) {
|
|
|
|
|
|
|
|
$user = $adsi.Create("User", $user)
|
|
|
|
|
|
|
|
$user.SetPassword($passwd)
|
|
|
|
|
|
|
|
$user.SetInfo()
|
|
|
|
|
|
|
|
$user
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Update-Password($user, [string]$passwd) {
|
|
|
|
|
|
|
|
$user.SetPassword($passwd)
|
|
|
|
|
|
|
|
$user.SetInfo()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Delete-User($user) {
|
|
|
|
|
|
|
|
$adsi.delete("user", $user.Name.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set-Attr $result "user" $user_obj; # Soemthing goes here.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Exit-Json $result;
|
|
|
|