Reputation: 25
Firstly I want to thank the community for all the help in a previous inquiry I had. Now I found the below code in this site that creates remotely a local user and adds it in the administrator group in pcs taken from a list. The problem is that when I run it and while it creates the user it gives me the error " error creating admin on: Exception calling add with 1 arguments. Type mismatch. (Exception from HRESULT:0x80020005)" . I tried putting the names of the pcs in the text file, in every possible way. Only pc name, full pc name (including domain), in capitals and not.
#Define variables
$computers = Get-Content C:\Computers.txt
#$computers = Import-CSV C:\Computers.txt | select Computer
$username = "Admin"
$password = "admin1"
$fullname = "Admin"
$local_security_group = "Administrators"
$description = "Description"
Foreach ($computer in $computers) {
$users = $null
$comp = [ADSI]"WinNT://$computer"
#Check if username exists
Try {
$users = $comp.psbase.children | select -expand name
if ($users -like $username) {
Write-Host "$username already exists on $computer"
} else {
#Create the account
$user = $comp.Create("User", "$username")
$user.SetPassword("$password")
$user.Put("Description", "$description")
$user.Put("Fullname", "$fullname")
$user.SetInfo()
#Set password to never expire
#And set user cannot change password
$ADS_UF_DONT_EXPIRE_PASSWD = 0x10000
$ADS_UF_PASSWD_CANT_CHANGE = 0x40
$user.userflags = $ADS_UF_DONT_EXPIRE_PASSWD + $ADS_UF_PASSWD_CANT_CHANGE
$user.SetInfo()
#Add the account to the local admins group
$group = ([ADSI]"WinNT://$computer/$local_security_group,group")
$computerHostName = (Get-WmiObject -ComputerName $computer Win32_ComputerSystem).Name
$group.Add([ADSI]"WinNT://$computerHostName/$username,user")
#Validate whether user account has been created or not
$users = $comp.psbase.children | select -expand name
if ($users -like $username) {
Write-Host "$username has been created on $computer"
} else {
Write-Host "$username has not been created on $computer"
}
}
}
Catch {
Write-Host "Error creating $username on $($computer.path): $($Error[0].Exception.Message)"
}
}
Please help if you can, it will really save me alot of time if I make it work.
Upvotes: 1
Views: 453
Reputation: 8868
You have a couple of errors in your code.
For this line, you have an extra comma and the word 'group'.
$group = ([ADSI]"WinNT://$computer/$local_security_group,group")
Change this to
$group = ([ADSI]"WinNT://$computer/$local_security_group")
And for the Add call just pass the path to the new user like this
$group.Add($user.Path)
With the suggested changes your code is
#Define variables
$computers = Import-CSV C:\Computers.txt | ForEach-Object Computer
$username = "Admin"
$password = "admin1"
$fullname = "Admin"
$local_security_group = "Administrators"
$description = "Description"
Foreach ($computer in $computers) {
$users = $null
$comp = [ADSI]"WinNT://$computer"
#Check if username exists
Try {
$users = $comp.psbase.children | ForEach-Object Name
if ($users -like $username) {
Write-Host "$username already exists on $computer"
} else {
#Create the account
$user = $comp.Create("User", "$username")
$user.SetPassword("$password")
$user.Put("Description", "$description")
$user.Put("Fullname", "$fullname")
$user.SetInfo()
#Set password to never expire
#And set user cannot change password
$ADS_UF_DONT_EXPIRE_PASSWD = 0x10000
$ADS_UF_PASSWD_CANT_CHANGE = 0x40
$user.userflags = $ADS_UF_DONT_EXPIRE_PASSWD + $ADS_UF_PASSWD_CANT_CHANGE
$user.SetInfo()
#Add the account to the local admins group
$group = ([ADSI]"WinNT://$computer/$local_security_group")
$computerHostName = (Get-WmiObject -ComputerName $computer Win32_ComputerSystem).Name
$group.Add($user.Path)
#Validate whether user account has been created or not
$users = $comp.psbase.children | ForEach-Object Name
if ($users -like $username) {
Write-Host "$username has been created on $computer"
} else {
Write-Host "$username has not been created on $computer"
}
}
}
Catch {
Write-Host "Error creating $username on $($computer.path): $($Error[0].Exception.Message)"
}
}
Finally, I would definitely stick with the CSV, any extra whitespace in the lines won't cause issues like with Get-Content.
Upvotes: 2