locked
ForEach-Object Unexpected token 'in' in expression or statement RRS feed

  • Question

  • I need to move VM's in a Hyper-V 2012 (non-R2) Failover Cluster to another node after they are set to the state "offline".
    Looking at the Move-ClusterVirtualMachineRole page, there is a nice example in Example 4.
    Except it doesn't work in my case.

    The lines in the example are are follows:

    PS C:\>$groups = Get-ClusterNode -Name node1 | Get-ClusterGroup | Where-Object -FilterScript {$_ | Get-ClusterResource | Where-Object -FilterScript {$_.ResourceType -Like "Virtual Machine"}}
    
    PS C:\>ForEach-Object -InputObject ($group in $groups) -Process { $group | Move-ClusterVirtualMachineRole -Node node2 }

    Where I change the first line to include the state -like offline
    $groups = Get-ClusterNode -Name node1 | Get-ClusterGroup | Where-Object -FilterScript {$_ | Get-ClusterResource | Where-Object -FilterScript {$_.ResourceType -Like "Virtual Machine" -and $_.State -Like "Offline"}}

    When I run the script I get an error as follows:
    At line:1 char:37
    + ForEach-Object -InputObject ($group in $groups) -Process { $group | Move-Cluster ...
    +                                     ~~
    Unexpected token 'in' in expression or statement.
    At line:1 char:36
    + ForEach-Object -InputObject ($group in $groups) -Process { $group | Move-Cluster ...
    +                                    ~
    Missing closing ')' in expression.
    At line:1 char:47
    + ForEach-Object -InputObject ($group in $groups) -Process { $group | Move-Cluster ...
    +                                               ~
    Unexpected token ')' in expression or statement.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken

    What am I missing here?

    Monday, June 25, 2018 3:40 PM

All replies

  • Foreach-Object is a pipeline cmdlet to loop through a collection of objects on the pipeline

    some command | Foreach {$_ # $_ current pipeline object}

    # or use foreach loop to loop through the collection $coll = some command Foreach ($obj in coll) { do something }



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, June 25, 2018 4:38 PM
  • ForEach-Object usage is wrong.  What you are trying to do doesn't make much sense.

    help ForEach-Object -online

    Search for more examples and discussions on the use of this CmdLet.

    $groups | 
        ForEach-Object { 
            $_ | Move-ClusterVirtualMachineRole -Node node2 
        }

    or

    foreach($group in $groups){
        $group | Move-ClusterVirtualMachineRole -Node node2 
    }

    "foreach()" is a syntax element.  "ForEach-Object" is a CmdLet.  They are not the same thing.


    \_(ツ)_/

    Monday, June 25, 2018 4:38 PM
  • So what it comes down to is that the example in the Move-ClusterVirtualMachineRole link is wrong?

    I know foreach and ForEach-Object are different things, and had been looking at foreach as well.
    I'll go further with foreach.

    Thanks for the replies, both.

    Tuesday, June 26, 2018 9:40 AM
  • Got it working like this now:

    $groups = Get-ClusterNode -Name node01 | Get-ClusterGroup | Where-Object -FilterScript {
        $_ | Get-ClusterResource | Where-Object -FilterScript {
            $_.ResourceType -Like "Virtual Machine" -and $_.State -Like "Offline"
            }
        }
    
    foreach ($group in $groups){
        $group | Move-ClusterVirtualMachineRole -MigrationType Quick -Node node02
        }

    Only issue is that the Move- CmdLet throws an error, which seems to be a bug, from what I read online, because the migrate is executed just fine.
    Part of the bug might be that we have information in the Notes property of the VM (Get-VM).

    Also, -MigrationType Quick is needed, in this case, because without it b0rks about not everything is in the right state...

    Tuesday, June 26, 2018 11:59 AM