invoke-command issue can't explain why?

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Yuan Li Yuan Li 8 months, 1 week ago.

  • Author
    Posts
  • #37146
    Profile photo of Yuan Li
    Yuan Li
    Participant

    Hey guys

    I meet a strange issue and can't explain why?
    for example,if I run something like this

    $a=Get-ADComputer -Filter *
    Invoke-Command $a.name -ScriptBlock { hostname} 
    

    it will return an error
    Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.
    At line:1 char:59
    + ... nding -Expression {Invoke-Command $a.name -ScriptBlock { hostname} }
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
    + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

    however ,if I add some filter it'll work.

    $computers=get-adcomputer -Filter{operatingsystem -like "*2012 R2*"}
    Invoke-Command  -ComputerName $b -ScriptBlock { hostname}
    

    I can't find any difference and when I tried to use trace-command to check, I got some error message from the 1st example

    DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
    DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Out-Default]
    DEBUG: ParameterBinding Information: 0 : PIPELINE object TYPE = [System.Management.Automation.ErrorRecord]
    DEBUG: ParameterBinding Information: 0 : RESTORING pipeline parameter's original values
    DEBUG: ParameterBinding Information: 0 : Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION
    DEBUG: ParameterBinding Information: 0 : BIND arg [One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.] to parameter [InputObject]
    DEBUG: ParameterBinding Information: 0 : BIND arg [One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.] to param [InputObject] SUCCESSFUL
    DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Out-Default]
    DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Out-LineOutput]

    I din't specify -inputObject, why it automatically call this parameter?

    Cheers
    Yuan

  • #37152
    Profile photo of Lauras Juozulynas
    Lauras Juozulynas
    Participant

    Hi,

    I tried your first example and it worked. The only time I got same error as you did was when I placed a "-" in Invoke-Command before $a.name:

    $a=Get-ADComputer -Filter *
    Invoke-Command -$a.name -ScriptBlock { hostname}

    Without that " – " this command works for me.

  • #37226
    Profile photo of Yuan Li
    Yuan Li
    Participant

    Thanks Lauras

    But I still have the same error. Actually if I run a subset of $a it'll work.

    $a=Get-ADComputer -Filter *
    Invoke-Command $a[0..20].name -ScriptBlock { hostname}

  • #37227
    Profile photo of Yuan Li
    Yuan Li
    Participant

    I think I found the issue.

    One computer object contains a space in its name and it caused this weird problem.

    Very strange and I don't know how could that computer object was created in the first place.

You must be logged in to reply to this topic.