Windows - Local User

Octopus.Script exported 2014-05-16 by bobjwalker belongs to ‘Windows’ category.

Configures a local user. (If assigning user rights the ntrights.exe file should be installed on the target server and added to the system path variable)

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Username

Username

The username

Password

Password

The password

Group memberships

MemberOf

A comma separated list of group memberships

User rights

UserRights

A comma separated list of user rights to assign (e.g. SeServiceLogonRight). See http://support.microsoft.com/kb/315276 for full list of rights

Script body

Steps based on this template will execute the following PowerShell script.

$username = $OctopusParameters['Username']
$password = $OctopusParameters['Password']
$memberOf = $OctopusParameters['MemberOf']
$userRights = $OctopusParameters['UserRights']

# Add/Update User

$user = Get-WmiObject Win32_UserAccount -filter "LocalAccount=True AND Name='$username'"
if($user -eq $null)
{
    Write-Host "Adding user"
    net user "$username" "$password" /add /expires:never /passwordchg:no /yes
}
else
{
    Write-Host "User already exists, updating password"
    net user "$username" "$password" /expires:never /passwordchg:no
}

# Ensure password never expires

write "Ensuring password never expires"
$user = Get-WmiObject Win32_UserAccount -filter "LocalAccount=True AND Name='$username'"
$user.PasswordExpires = $false; 
$user.Put();

# Add/Update group membership

if($memberOf)
{
    $groups = $memberOf.Split(",")
    foreach($group in $groups)
    {
        $ntGroup = [ADSI]("WinNT://./$group")
        $members = $ntGroup.psbase.Invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
        if($members -contains "$username")
        {
            Write-Host "User already a member of the $group group" 
        }
        else
        {
            Write-Host "Adding to the $group group"
            net localgroup "$group" "$username" /add
        }
    }
}

# Add/Update user rights assignment

if($userRights)
{
    $userRightsArr = $userRights.Split(",")
    foreach($userRight in $userRightsArr)
    {
        Write-Host "Granting $userRight right"
        & "ntrights" +r $userRight -u "$username"
    }
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "6dbe826d-f973-46fe-a897-a0a2cdfd01f4",
  "Name": "Windows - Local User",
  "Description": "Configures a local user. (If assigning user rights the ntrights.exe file should be installed on the target server and added to the system path variable)",
  "Version": 3,
  "ExportedAt": "2014-05-16T10:30:58.052+00:00",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Parameters": [
    {
      "Name": "Username",
      "Label": "Username",
      "HelpText": "The username",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Password",
      "Label": "Password",
      "HelpText": "The password",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "MemberOf",
      "Label": "Group memberships",
      "HelpText": "A comma separated list of group memberships",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "UserRights",
      "Label": "User rights",
      "HelpText": "A comma separated list of user rights to assign (e.g. SeServiceLogonRight). See http://support.microsoft.com/kb/315276 for full list of rights",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "$username = $OctopusParameters['Username']\n$password = $OctopusParameters['Password']\n$memberOf = $OctopusParameters['MemberOf']\n$userRights = $OctopusParameters['UserRights']\n\n# Add/Update User\n\n$user = Get-WmiObject Win32_UserAccount -filter \"LocalAccount=True AND Name='$username'\"\nif($user -eq $null)\n{\n    Write-Host \"Adding user\"\n    net user \"$username\" \"$password\" /add /expires:never /passwordchg:no /yes\n}\nelse\n{\n    Write-Host \"User already exists, updating password\"\n    net user \"$username\" \"$password\" /expires:never /passwordchg:no\n}\n\n# Ensure password never expires\n\nwrite \"Ensuring password never expires\"\n$user = Get-WmiObject Win32_UserAccount -filter \"LocalAccount=True AND Name='$username'\"\n$user.PasswordExpires = $false; \n$user.Put();\n\n# Add/Update group membership\n\nif($memberOf)\n{\n    $groups = $memberOf.Split(\",\")\n    foreach($group in $groups)\n    {\n        $ntGroup = [ADSI](\"WinNT://./$group\")\n        $members = $ntGroup.psbase.Invoke(\"Members\") | %{$_.GetType().InvokeMember(\"Name\", 'GetProperty', $null, $_, $null)}\n        if($members -contains \"$username\")\n        {\n            Write-Host \"User already a member of the $group group\" \n        }\n        else\n        {\n            Write-Host \"Adding to the $group group\"\n            net localgroup \"$group\" \"$username\" /add\n        }\n    }\n}\n\n# Add/Update user rights assignment\n\nif($userRights)\n{\n    $userRightsArr = $userRights.Split(\",\")\n    foreach($userRight in $userRightsArr)\n    {\n        Write-Host \"Granting $userRight right\"\n        & \"ntrights\" +r $userRight -u \"$username\"\n    }\n}\n",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-local-user.json",
  "Website": "/step-templates/6dbe826d-f973-46fe-a897-a0a2cdfd01f4",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////Da3qSsLvhtb0wur6O7zuWcfxldv2aMzyK7ftpOD3s+X48Pr+0fD7d9HzHLLr4fX8xD/OcwAAAaNJREFUeNrs3cFygjAUQFECWott1f//2sJoW6kIKEzNs+euXOmcmSSGDa8oJEmSJEmSJGmsj1W1K9cpsGD1Vr2WdToVEPC+2lYvZfpVrEW0qZpF1F+MRdRugzoNlvkiarfBPk0pT8GhWUSX2yASpDlLr2+DEJBmEY1ug6whx7N0n2b30G1QlmmxHsRYp6X76yvF9vg5RYQczq8UVURI35UiFmTgShED0p6lI1eKzCHTrxS5Qk6PZ9PLDtJ9PIsJmXWlyAky6/dAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQMJCyjltF/iO3gpJUpD8s4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgID8T8itwwKyhbTdMr4ha8hXUwZqhICcOgyNOIkE+V5wo4MSgr1u/fp7poO+AL8K/gL8yw0UeyRB34m9iQ/pVD8L5JYTO3NI58R+AsiEEzsW5OfE3sUe/zRwYkeGnG2g2CPS7rhjF4GKP0ZwyoldxK37kFqEL/7wU0mSJEmSJOmJ+xRgAHxZTCXGdZkfAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Friday, May 16, 2014