Hello everyone and thanks for any assistance with this issue,
I've tried to take detailed notes as I work on this. Where I am at there is a setup and initialization process that I am attempting to automate through Powershell. One of the actions is to change an existing user account to a specific nomenclature. For full
automation it basically needs to prompt the user for the variables in the beginning of the execution then operate as a fire-and-forget script. I have the variables prompt part working well, but the actual change has been causing some problems. Here is the
script and output info I have so far below:
*******************SCRIPT BELOW HERE*******************
# Variables that are needed from the user should all be included here
# in this first section to be prompted for at the beginning of the
# setup process. If the variable is not dynamic to each separate laptop
# and can be defined or pulled without the need for user input then
# please do not include it within this initial variable section!
#
#
$Original_User_Name = Read-Host -Prompt 'What is the original user name?'
$User_Initials = Read-Host -Prompt 'What are the first and last initials to add to the original user name?'
$New_User_Name = $User_Initials + $Original_User_Name
# Variables that are not dynamic, or are able to be pulled dynamically
# WITHOUT user input should be contained in this section below:
$CompStat = Get-WmiObject win32_computersystem
$Localhst = $CompStat.Name
# Display of user entered variables to confirm accuracy before proceeding:
Write-Host "The original user name is $Original_User_Name"
Write-Host "The user's initials are $User_Initials"
Write-Host "The new user name is $New_User_Name"
Write-Host "If these values are correct, press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
# List Item 01 - Rename Local Admin Account
# Attempt #2: In-Progress
$admin= [adsi]("WinNT://"+$Localhst+"/$Original_User_Name,user")
$admin.psbase.Rename('cn=$New_User_Name')
# Attempt #1: Failed
# $admin=[adsi]"WinNT://hostname/$Original_User_Name,user"
# $admin.psbase.rename("$New_User_Name")
# This will keep the window open so admins know the script completed or
# see if any errors were encountered during the process in the event the
# registry settings were not already configured for -noexit
Read-Host -Prompt 'Press Enter to exit'
*******************END SCRIPT*******************
This is the error I am getting. I have removed sensitive info so the username value and file path are not there:
What is the original user name?: REDACTED
What are the first and last initials to add to the original user name?: REDACTED
The original user name is REDACTED
The user's initials are REDACTED
The new user name is REDACTED
If these values are correct, Press any key to continue ...
Exception calling "Rename" with "1" argument(s): "Access is denied. (Exception
from HRESULT: 0x80070005 (E_ACCESSDENIED))"
At REDACTED:36 char:21
+ $admin.psbase.Rename <<<< ('cn=$New_User_Name')
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Press Enter to exit:
I have tried a lot of different syntax to try and get the actual change after psbase.Rename to work correctly, but every one yields this same error.
Here's a list of most of the syntax I have tried:
$admin.psbase.rename('cn=$New_User_Name')
$admin.psbase.rename(cn=$New_User_Name)
$admin.psbase.rename(cn='$New_User_Name')
$admin.psbase.rename("cn=$New_User_Name")
$admin.psbase.rename(cn=$New_User_Name)
$admin.psbase.rename(cn="$New_User_Name")
$admin.psbase.rename('$New_User_Name')
$admin.psbase.rename($New_User_Name)
$admin.psbase.rename('$New_User_Name')
$admin.psbase.rename("$New_User_Name")
$admin.psbase.rename($New_User_Name)
I have also used some write-host lines to check that my variables were correct along the way and found no issues. I am brand new to this and would greatly appreciate any input that would help me get this working. I have gotten this far off of simply reading
about how to change a username through powershell. I have no formal training, just my brain, analytical skills, and a few years hacking at video game dev commands.
The error output has me wondering if there is some type of additional authentication needed before I would be able to actually change the user name on the account. I am logged in on a local administrator account and am trying to change a local user account
who is also a local admin. No interfacing with Active Directory is needed unless I am missing something here. Please let me know and thanks again for any assistance.