IT:AD:Powershell:HowTo:Snippets:Ensure a Local User Account
Summary
When configuring a new CI/CD server, it's common to need a service account to run services.
They don't automatically have to be AD accounts.
Process
The following will set up a Local user:
function Ensure-LocalUser(){
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string] $userName,
[Parameter(Mandatory=$true)]
[string] $password,
[Parameter(Mandatory=$true)]
[string] $description
)
process{
$objOu = [ADSI]"WinNT://${env:Computername}"
$localUsers = $objOu.Children | where {$_.SchemaClassName -eq 'user'} | % {$_.name[0].ToString()}
if($localUsers -NotContains $userName)
{
$objUser = $objOU.Create("User", $userName)
$objUser.setpassword($password)
$objUser.SetInfo()
$objUser.description = $description
$objUser.SetInfo()
return $true
}
else
{
return $false
}
}
}