aenagy
Hot Shot
Hot Shot

Script to automate DRS-Groups Manger affinity - modified script acting wierd.

This is a modification of a script written by LucD (Re: Script to automate DRS-Groups Manger affinity). This change was intended to gather both the virtual machine and host groups. The wierd part is that only the virtual machine script block or host script block seem to work individually, but not when both are enabled. Even if I swap the order of the two script blocks, only the first one outputs results.

# https://communities.vmware.com/thread/470759

$ClusterName = "the name of my cluster"

$HostDrsGroup = $null

$HostDrsGroupObject = $null

$HostDrsGroupProperties = $null

$HostDrsGroups = $null

$VirtualmachineDrsGroup = $null

$VirtualmachineDrsGroupObject =$null

$VirtualmachineDrsGroupProperties = $null

$VirtualmachineDrsGroups = $null

Write-Output ""

Write-Output "Getting Host DRS groups."

$HostDrsGroups = ((get-cluster -Name $ClusterName ).ExtensionData.ConfigurationEx.group) | ForEach {

      $_ | where-object { $_.gettype().name -eq "ClusterHostGroup" }

}

Write-Output "Showing Host DRS groups."

foreach ( $HostDrsGroup in $HostDrsGroups ) {

           $HostDrsGroupProperties = @{

                'Host Drs Group'= $HostDrsGroup.name ;

                'Host'          = $HostDrsGroup.host | foreach {get-vmhost -id $_ }

           }

      $HostDrsGroupObject = New-Object -TypeName PSObject -Property $HostDrsGroupProperties

      $HostDrsGroupObject

}

Write-Output ""

Write-Output "Getting virtual machine DRS groups."

$VirtualmachineDrsGroups = ((get-cluster -Name $ClusterName ).ExtensionData.ConfigurationEx.group) | ForEach {

      $_ | where-object { $_.gettype().name -eq "ClusterVmGroup" }

}

Write-Output "Showing virtual machine DRS groups."

foreach ($VirtualmachineDrsGroup in $VirtualmachineDrsGroups) {

      $VirtualmachineDrsGroupProperties = @{

           'Virtual machine Drs Group'= $VirtualmachineDrsGroup.Name;

           'Virtual machine'          = $VirtualmachineDrsGroup.vm | foreach {get-vm -id $_ }

      }

      $VirtualmachineDrsGroupObject = New-Object -TypeName PSObject -Property $VirtualmachineDrsGroupProperties

      $VirtualmachineDrsGroupObject

}

Write-Output ""

Write-Output "End of script."

0 Kudos
5 Replies
LucD
Leadership
Leadership

That is the PowerShell output formatter that is doing that.

Try changing

$HostDrsGroupObject

into

$HostDrsGroupObject | Out-Default

and

$VirtualmachineDrsGroupObject

into

$VirtualmachineDrsGroupObject | Out-Default

That way you kind of reset the output formatter for each object you pass to it.

But that is probably not the ideal way of displaying the info. Better would be to pipe the results to a Select-Object cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
aenagy
Hot Shot
Hot Shot



LucD:


That seems to have fixed the problem, although I don't understand why. I would mark this question as Answered, if I could figure out how.

(I don't plan on using the script in its current form. At this point I'm just gathering current state info for modification later.)

0 Kudos
LucD
Leadership
Leadership

Who better than Jeffrey Snover to explain the how :smileycool:

See How PowerShell Formatting and Outputting REALLY works


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
aenagy
Hot Shot
Hot Shot



LucD:


Thanks. That helped.


Sometimes, but not always, I miss the simplicity of CMD shell.

0 Kudos
LucD
Leadership
Leadership

Perhaps true on the simplicity, but you get so much more with PowerShell :smileycool:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos