Nested Foreach in PowerShell

Hi All,

 

I am training my power shell skiils and challenging my abilities, i need you assist with the following easy task.

 

I want to get any VM and his own cluster on PowerCLI.

 

I know that i can get this details using HOST.PARENT but i insist to get this details via “Cluster” in order to learn work with two for each, for some reason it doesn’t work for me, can you assist?

 

$Result =@(

$VMList = (Get-VM).Name 
$Clusters = (get-cluster).Name
Foreach ($Cluster in $Clusters){
Foreach ($vm in $VMList){ 
[pscustomobject]@{
"VM Name" = $vm 
"Cluster Name"= $Cluster
} 
} 
}
)

$Result

I’m not sure your code is doing anything other than combining all VMs with all clusters. Maybe this is what you’re looking for?


foreach($vm in (get-vm))
{
    get-vmhost -vm $vm | foreach{
        [pscustomobject][Ordered]@{
            VM      = $vm.name
            Host    = $_.name
            Cluster = $_.parent
        }
    }
}

If you want to find the VMs within each cluster, you can the following with calculated properties:

# Get all clusters
$clusters = Get-Cluster
# Loop through each cluster
$result = foreach ($cluster in $clusters) {
     # Use calculated properties to create custom object
     Get-VM -Location $cluster | Select @{n='Cluster';e={$cluster.Name}},@{n='VM Name';e={$_.Name}}
}

Hello there,

I have several clusters. Each cluster has several nodes which host a number of VMs.

I have tried to get all clusters that exist within the domain with this code , but it give me an error.

This is the code:

get-cluster

This is the error:

WARNING: If you are running Windows PowerShell remotely, note that some failover clustering cmdlets 
do not work remotely. When possible, run the cmdlet locally and specify a remote computer as the tar
get. To run the cmdlet remotely, try using the Credential Security Service Provider (CredSSP). All a
dditional errors or warnings from this cmdlet might be caused by running it remotely.
Get-Cluster : The cluster service is not running.  Make sure that the service is running on all 
nodes in the cluster.
    There are no more endpoints available from the endpoint mapper
At line:1 char:1
+ Get-Cluster

However, after adding an " * " to " get-cluster" , I get the list of all clusters available on the network

get-cluster *

This is the result:

Name             
----             
HOst1
HOst2
HOst3
HOst4
HOst5

I tried to use this code to get all the VMs available within the Network with the " * " next to get-cluster but I got an error:

Code:

Error:

Get-VM : The cluster object 'Host1' is not a valid cluster group or virtual machine 
cluster resource. Run the command again and specify an object that represents a cluster group or 
virtual machine cluster resource.
At line:1 char:2
+  Get-VM  $cluster
+  ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], VirtualizationException
    + FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.GetVM

Can anyone help me please. Is it a problem of permissions? do I need to run the scrip on every single host within the cluster remotely to get all vm they run?

Thanks.

Please do not hijack other peoples threads. If you have a question on the same topic you should create a new thread for yourself and add a link to the post you’re refering to to your question.

Thanks. :wink:

Sorry Olaf, I did not mean to…just did not understand the rules… i will do so.

Thanks anyway.

Kind regards,

Boulbul