none
PowerShell Script to add a account to a site systems local user group RRS feed

  • Question

  • Hello

    I'm trying to add an account to about a 100 systems in a site Workstations OU.

    I'm trying this PowerShell Script but I keep getting errors below.............

    $computername = $env:computername   # place computername here for remote access
    $username = 'AdminAccount1'
    $password = 'topSecret@99'
    $desc = 'Automatically created local admin account'


    $computer = [ADSI]"WinNT://$computername,computer"
    $user = $computer.Create("user", $username)
    $user.SetPassword($password)
    $user.Setinfo()
    $user.description = $desc
    $user.setinfo()
    $user.UserFlags = 65536
    $user.SetInfo()
    $group = [ADSI]("WinNT://$computername/administrators,group")
    $group.add("WinNT://$username,user")

    Errors:


    Thanks for the help

    Exception calling "Setinfo" with "0" argument(s): "Exception from HRESULT: 0x800708B0"

    At line:11 char:1

    + $user.Setinfo()

    + ~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

    Exception setting "description": "Cannot set the Value property for PSMemberInfo object of type

    "System.Management.Automation.PSMethod"."

    At line:12 char:1

    + $user.description = $desc

    + ~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException

        + FullyQualifiedErrorId : ExceptionWhenSetting

    Exception calling "setinfo" with "0" argument(s): "Exception from HRESULT: 0x800708B0"

    At line:13 char:1

    + $user.setinfo()

    + ~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

    Exception calling "SetInfo" with "0" argument(s): "Exception from HRESULT: 0x800708B0"

    At line:15 char:1

    + $user.SetInfo()

    + ~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

    Exception calling "add" with "1" argument(s): "The specified account name is already a member of the group.

    "

    At line:17 char:1

    + $group.add("WinNT://$username,user")

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

    Tuesday, September 29, 2015 1:27 PM

Answers

  • There is no need for a script.

    Create a group and use Group Policy to add the group to the local Administrators group.

    Then manage the members of the group.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 29, 2015 2:06 PM
    Moderator