adding local admins

Hi there.

I just wanted to write a really small and easy script but it doesn’t seem to work :frowning:

I simply just want to give local admin rights.

$computername=Read-Host “enter the computername”
$username=Read-Host “enter the user name you want to give access to (DOMAIN\username)”

Invoke-Command -ComputerName $computername -Command {Add-LocalGroupMember -Group Administratoren -Member $username }

when I only run the command without the parameters but with filled out values it works fine. As soon as I put in $username it doesn’t work anymore and I get the following error message:

  • CategoryInfo : InvalidData: (:slight_smile: [Add-LocalGroupMember], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand

In this post there is a script you maybe able to use to do this.

https://4sysops.com/archives/add-a-user-to-the-local-administrators-group-on-a-remote-computer/

$DomainName = Read-Host “Domain name:”
$ComputerName = Read-Host “Computer name:”
$UserName = Read-Host “User name:”
$AdminGroup = [ADSI]“WinNT://$ComputerName/Administrators,group”
$User = [ADSI]“WinNT://$DomainName/$UserName,user”
$AdminGroup.Add($User.Path)

USING LOCAL VARIABLES

$ps = "Windows PowerShell"
Invoke-Command -ComputerName S1 -ScriptBlock {
  Get-WinEvent -LogName $Using:ps
}

Hello Crovax,

thank you that’s exactly what I needed!

yeah, now that I think about it, it seems logical. thanks :slight_smile: