Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
556ff9b7
Commit
556ff9b7
authored
Jun 19, 2014
by
Paul Durivage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor of win_user module
parent
dd3f7c2d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
34 deletions
+68
-34
library/windows/win_user.ps1
+68
-34
No files found.
library/windows/win_user.ps1
View file @
556ff9b7
#!powershell
# (c) 2014, Matt Martz <matt@sivel.net>, and others
#
# 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
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
...
...
@@ -21,12 +21,13 @@
$params
=
Parse-Args
$args
;
$result
=
New-Object
psobject;
Set
-Attr
$result
"changed"
$false
;
$result
=
New-Object
psobject @
{
changed
=
false
}
;
If
(
-not
$params
.
user
name.GetType
)
If
(
-not
$params
.name.GetType
)
{
Fail-Json
$result
"missing required arguments:
user
name"
Fail-Json
$result
"missing required arguments: name"
}
If
(
-not
$params
.password.GetType
)
...
...
@@ -34,43 +35,77 @@ If (-not $params.password.GetType)
Fail-Json
$result
"missing required arguments: password"
}
$extra_args
=
""
If
(
$params
.extra_args.GetType
)
{
$extra_args
=
$params
.extra_args;
}
$extra_args
=
$params
"extra_args"
""
If
(
$params
.creates.GetType -and
$params
.state.GetType -and
$params
.state -ne
"absent"
)
{
If
(
Does-User-Exist
$params
.username
)
{
Exit
-Json
$result
;
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"
}
$logfile
=
[
IO.Path]::GetTempFileName
()
;
if
(
$params
.state.GetType -and
$params
.state -eq
"absent"
)
{
NET USER
$params
.username
$params
.password /ADD
$username
=
Get-Attr
$params
"name"
$password
=
Get-Attr
$params
"password"
$user_obj
=
Get-User
$username
if
(
-not
$user_obj
)
{
Fail-Json
$result
"Could not find user:
$username
"
}
Set
-Attr
$result
"changed"
$true
;
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
}
}
else
{
# Remove user
try
{
Delete-User
$bob
$result
.changed
=
$true
}
catch
{
Fail-Json
$result
$_
.Exception.Message
}
}
$logcontents
=
Get-Content
$logfile
;
Remove-Item
$logfile
;
$adsi
=
[
ADSI]
"WinNT://
$env
:COMPUTERNAME"
Set
-Attr
$result
"log"
$logcontents
;
function
Get-User
(
$user
)
{
$adsi
.Children |
where
{
$_
.SchemaClassName -eq
'user'
-and
$_
.Name -eq
$user
}
return
}
Exit
-Json
$result
;
function
Create-User
([
string
]
$user
,
[
string
]
$passwd
)
{
$user
=
$adsi
.Create
(
"User"
,
$user
)
$user
.SetPassword
(
$passwd
)
$user
.SetInfo
()
$user
return
}
Function
Does-User-Exist
(
$username
)
{
$objComputer
=
[
ADSI]
(
"WinNT://
$env
:COMPUTERNAME,computer"
)
function
Update-Password
(
$user
,
[
string
]
$passwd
)
{
$user
.SetPassword
(
$passwd
)
$user
.SetInfo
()
}
$colUsers
=
(
$objComputer
.psbase.children |
Where
-Object
{
$_
.psBase.schemaClassName -eq
"User"
}
|
Select-Object
-expand Name
)
function
Delete-User
(
$user
)
{
$adsi
.delete
(
"user"
,
$user
.Name.Value
)
}
$blnFound
=
$colUsers
-eq
$username
Set
-Attr
$result
"user"
$user_obj
;
# Soemthing goes here.
}
\ No newline at end of file
Exit
-Json
$result
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment