0

Im trying to do an invoke command inside a function, but having difficulty getting defined variables to pass through.

  function Set-RemoteLocalAdmin {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$ComputerName,
    [String] $UserId
    )
      $sb = {​
param($userid)
    add-LocalGroupMember -Group "Administrators" -Member "$UserId"}

Invoke-Command -ComputerName $ComputerName -Scriptblock $sb -argumentlist $userid 
}


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

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

Member BUILTIN\ was not found in group Administrators. + CategoryInfo : ObjectNotFound: (BUILTIN:String) [Add-LocalGroupMember], MemberNotFoundException + FullyQualifiedErrorId : MemberNotFound,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand + PSComputerName : COMPUTERNAME-1


What am I missing here?

Thanks in advance.

iceman
  • 304
  • 1
  • 10
  • 2
    Please expand on "having difficulty getting defined variables to pass through." - does it result in errors being thrown? If so, what are the error messages? – Mathias R. Jessen Jul 16 '21 at 13:08
  • 1
    Please cut/paste the _text_ of the error messages being thrown (and format them as code). The image you've inserted is unreadable. – Jeff Zeitlin Jul 16 '21 at 13:15
  • 2
    There appears to be a zero-width space in your code immediately following `$sb = {`, looks like PowerShell is trying to interpret it as a command invocation, after which the `param` block is no longer valid (can't declare `param()` in the "middle" of a block). Try re-writing it manually (no copy-pasting) and it'll likely work – Mathias R. Jessen Jul 16 '21 at 13:26
  • Nice catch, @MathiasR.Jessen; from what I can tell, that is indeed the problem. MattyS, [this answer](https://stackoverflow.com/a/68328388/45375) contains a function that can help you find such hidden control characters in your code. – mklement0 Jul 16 '21 at 13:52

0 Answers0