invoke-command on old PS version

This topic contains 6 replies, has 4 voices, and was last updated by  JohnM 2 months ago.

  • Author
    Posts
  • #96750

    kate Ritter
    Participant

    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

  • #96756

    Aapeli Hietikko
    Participant

    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.

  • #96762

    Pedro Rafael
    Participant

    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.

  • #96888

    kate Ritter
    Participant

    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?

    • #96891

      Aapeli Hietikko
      Participant

      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
      
      } 
      
    • #96896

      Aapeli Hietikko
      Participant

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

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

  • #96920

    JohnM
    Participant

    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.

You must be logged in to reply to this topic.