title: Powershell Invoke command in a function
body: <p>Im trying to do an invoke command inside a function, but having difficulty getting defined variables to pass through.</p>
  <pre><code>  function Set-RemoteLocalAdmin {
  
      [CmdletBinding()]
      param (
          [Parameter(Mandatory)]
          [string]$ComputerName,
      [String] $UserId
      )
        $sb = {​
  param($userid)
      add-LocalGroupMember -Group &quot;Administrators&quot; -Member &quot;$UserId&quot;}
  
  Invoke-Command -ComputerName $ComputerName -Scriptblock $sb -argumentlist $userid 
  }
  
  </code></pre>
  <hr />
  <p>Errors:
  The term '​' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  + CategoryInfo          : ObjectNotFound: (​:String) [], CommandNotFoundException
  + FullyQualifiedErrorId : CommandNotFoundException
  + PSComputerName        : COMPUTERNAME-1</p>
  <p>The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  + CategoryInfo          : ObjectNotFound: (param:String) [], CommandNotFoundException
  + FullyQualifiedErrorId : CommandNotFoundException
  + PSComputerName        : COMPUTERNAME-1</p>
  <p>Member BUILTIN\ was not found in group Administrators.
  + CategoryInfo          : ObjectNotFound: (BUILTIN:String) [Add-LocalGroupMember], MemberNotFoundException
  + FullyQualifiedErrorId : MemberNotFound,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand
  + PSComputerName        : COMPUTERNAME-1</p>
  <hr />
  <p>What am I missing here?</p>
  <p>Thanks in advance.</p>