Na verdade, o script que foi copiado e colado não funciona.
Possui aspas duplas inválidas: “
e ”
Ele faz referência ao computador local de três maneiras diferentes:
[ADSI]“WinNT://$computer1”
e [ADSI]("WinNT://"+$env:COMPUTERNAME+"
e WinNT://$env:localhost/
Existem várias ocorrências em que o texto não é citado corretamente e separado / concatenado com variáveis.
Isso funciona:
cls
$username = "test_user"
$password = "zlug7nPn5$"
$computername = "ComputerName"
$users = $null
$computer = [ADSI]"WinNT://$computername"
Try {
$users = $computer.psbase.children | select -expand name
if ($users -like $username) {
Write-Host "$username already exists"
} Else {
$user_obj = $computer.Create("user", "$username")
$user_obj.SetPassword($password)
$user_obj.SetInfo()
$user_obj.Put("description", "$username")
$user_obj.SetInfo()
$user_obj.psbase.invokeset("AccountDisabled", "False")
$user_obj.SetInfo()
$users = $computer.psbase.children | select -expand name
if ($users -like $username) {
Write-Host "$username has been created on $($computer.name)"
$group = [ADSI]("WinNT://"+$computername+"/administrators,group")
$group.add("WinNT://"+$computername+"/"+$username+",user")
} Else {
Write-Host "$username has not been created on $($computer.name)"
}
}
} Catch {
Write-Host "Error creating $username on $($computer.path): $($Error[0].Exception.Message)"
}