invoke-command on old PS version

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

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.

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.

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 :confused:
Is there a way of running v3+ commands remotely on a machine with v2?

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

} 

I took it a little further to fill my needs too:

https://github.com/AapeliH/PowerShell/blob/master/Add-LocalGroupMember.ps1

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.