Author Posts

March 22, 2018 at 1:05 pm

Hello,

I wrote the following little script to set local admin rights on a machine.

function set-localadmin {
[CmdletBinding()]

param (
[Parameter (Position=0,mandatory = $true)]
$computername )

$username=Read-Host "Please type in the emailadresse.: Test@test.com"

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

}

so far so good.
It works on 90% of the machines. Just sometimes I get an error message that Add-LocalGroupMember is not a known cmdlet.
I opened a PSSession onto one of the machines where is doesn't work and found out that it has PSv2 installed.

As far as I know, the cmdlet has been introduced with v3.

Is there a way to make this work independently on which version is installed of the client?

Thank you for your help

Kind Regards
Kate

March 22, 2018 at 1:08 pm

Hello,

How about if you'd use old school net localgroup command to perform the action or at least test if ps version is less than 3 then use net localgroup else go with the one you have already.

March 22, 2018 at 2:18 pm

There is the possibility of updating your powershell, enter this link: http://cooperati.com.br/2017/11/01/como-atualizar-o-powershell-em-versoes-mais-antigas-do-windows-server-e-windows-cliente/

and see if you can upgrade.

March 23, 2018 at 6:50 am

Hallo,

unfortunately I cannot read the link (I dont speak ... spanish? portuguese?)

I tried the following:

 function set-localadmin {
        [CmdletBinding()]

        param (
            [Parameter (Position=0,mandatory = $true)]
            $computername           )

            $username=Read-Host "type in your email adresse e.g.: Test@test.com"


Invoke-Command -ComputerName $computername -Command {net localgroup Administratoren $Using:username /add }

} 

but now it doesn't work at all anymore :/
Is there a way of running v3+ commands remotely on a machine with v2?

March 23, 2018 at 7:20 am

I'm not sure if local admin group is different between the languages. Check it out first by just running net localgroup.
This worked for me:

 function set-localadmin {
        [CmdletBinding()]

         Param
    (
        # ComputerName
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $ComputerName,

        # Account
        [Parameter(Mandatory=$true,
           ValueFromPipelineByPropertyName=$true,
           Position=1)]
        $Identity
    )


    Invoke-Command -ComputerName $ComputerName -Command {net localgroup Administrators $args[0] /add } -ArgumentList $Identity

} 

March 23, 2018 at 12:58 pm

There was a time I tried using [adsi] to read/add from/to local groups. It was early in PS v2. Try Googling
[ADSI]"WinNT://"

I found this on a quick search:
https://blogs.technet.microsoft.com/heyscriptingguy/2014/10/03/adding-local-users-to-local-groups/

usually "Hey scripting Guy" is a great place to start.
Good Luck.