I have the following code, it's a simple script to add a new computer to our domain. It works when executed locally using Read-Host for credentials and Organizational unit name
$domainName = "mydomain.live"
# Manually provide the username and password for joining the domain
$adminUsername = Read-Host "Enter the domain admin username"
$adminPassword = Read-Host -AsSecureString "Enter the domain admin password"
# Create a PSCredential object with the provided username and password
$credential = New-Object System.Management.Automation.PSCredential ($adminUsername, $adminPassword)
# Get the local computer name
$computerName = $env:COMPUTERNAME
# Prompt for the OU name where the computer should be added
$ouName = Read-Host "Enter the name of the parent Organizational Unit (OU)"
$childOUName = Read-Host "Enter the name of the child Organizational Unit (OU)"
# Join the computer to the domain and specify the target OU
Add-Computer -DomainName $domainName -Credential $credential -ComputerName $computerName -OUPath " OU=$childOUName,OU=$ouName, DC=mydomain, DC=live" -Restart
Now I've modified the code above to support Atera's custom script variables. These function similar to the read-host prompt and output a System:string when verified with getType(). However something is causing an access denied error when done through the RMM
$domainName = 'mydomain.live'
# Manually provide the username and password for joining the domain
$adminUsername = [string]"{[adminUsername]}"
$adminPassword = [string]"{[adminPassword]}"
$encrypted = convertto-securestring $adminPassword -AsPlainText -Force
# Create a PSCredential object with the provided username and password
$credential = New-Object System.Management.Automation.PSCredential ($adminUsername, $encrypted)
# Get the local computer name
$computerName = $env:COMPUTERNAME
# Prompt for the OU name where the computer should be added
$ouName = [string]"{[ouName]}"
$childOUName = [string]"{[childOUName]}"
# Join the computer to the domain and specify the target OU
Add-Computer -DomainName $domainName -Credential $credential -ComputerName $computerName -OUPath "OU=$childOUName,OU=$ouName, DC=mydomain, DC=live" -Restart
When this is run (as user) from atera, I get the following error:
Computer RH009 could not join domain MyDomain from WORKGROUP, access refused.
+ CategoryInfo : OperationStopped: (MyDomain-RH009:String) [Add-Computer], InvalidOperationException
+ FullyQualifiedErrorId : FailToJoinDomainFromWorkgroup,Microsoft.PowerShell.Commands.AddComputerCommand
Any lead or advice is appreciated