Question

Local admin account for Windows

  • 19 September 2022
  • 3 replies
  • 395 views

Badge

Hi Team ,

 

We have below script which we are using to create LOcal admin account in windows:

It is working fine manually, but not through AUTOMOX Worklets.

 

Kindly suggest.


 

$username = "username"   # Administrator is built-in name

$password = ConvertTo-SecureString "Password here" -AsPlainText -Force

Function Write-Log {

 param(

     [Parameter(Mandatory = $true)][string] $message,

     [Parameter(Mandatory = $false)]

     [ValidateSet("INFO","WARN","ERROR")]

     [string] $level = "INFO"

 )

 # Create timestamp

 $timestamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")


 # Append content to log file

 Add-Content -Path $logFile -Value "$timestamp [$level] - $message"

}


Function Create-LocalAdmin {

   process {

     try {

       New-LocalUser "$username" -Password $password -FullName "$username" -Description "local admin" -ErrorAction stop -PasswordNeverExpires

       Write-Log -message "$username local user crated"


       # Add new user to administrator group

       Add-LocalGroupMember -Group "Administrators" -Member "$username" -ErrorAction stop

       Write-Log -message "$username added to the local administrator group"

     }catch{

       Write-log -message "Creating local account failed" -level "ERROR"

     }

   }   

}


Write-Log -message "#########"

Write-Log -message "$env:COMPUTERNAME - Create local admin account"


Create-LocalAdmin


Write-Log -message "#########"


3 replies

Userlevel 1
Badge

Hello!

 

While I can’t speak to the exact content of your script, I do want to point out that the latest Automox Agent version (version 1.0.40) has changed it’s install directory, which may impact how your antivirus or endpoint protection software monitors script executions.

 

Please follow our guide linked below to whitelist/allowlist the appropriate filepath for Automox to execute scripts.
https://help.automox.com/hc/en-us/articles/5352205452948-Change-Automox-Script-Execution-Location

It may also help to follow this guide to ensure the scripts aren’t being blocked by other means:
https://help.automox.com/hc/en-us/articles/5352186671252-Globally-Trust-listing-Automox-Through-EPP-Application-Control

Cheers,
Brandon

Userlevel 5
Badge +1

Hi @spragya this occurs because the cmdlet you are using, New-LocalUser, is not supported in 32-bit PowerShell on a 64-bit system 

 

A neat work-around you could use

$scriptblock = {
New-LocalUser "$username" -Password $password -FullName "$username" -Description "local admin" -ErrorAction stop -PasswordNeverExpires
}
$64bit = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptblock

 

Badge

Thank you Jack

Reply