VMware Cloud Community
virtualtech_wor
Enthusiast
Enthusiast
Jump to solution

PowerCLI Export DRS Rules Script help

Hi,

I'm using this code to export DRS-Rules.

1. The data is exporting fine (if the script is executed manually), but the console is still throwing the errors (screenshot below, also attached with this post)

2. The script doesn't export any data if I schedule the same from windows task scheduler.

Please let me know how to address the above.

Code:

## get cluster .NET view objects, which hold the info about the DRS rules

Get-View -ViewType ClusterComputeResource -Property Name, ConfigurationEx | %{

    ## if the cluster has any DRS rules

    if ($_.ConfigurationEx.Rule -ne $null) {

        $viewCurrClus = $_

        $viewCurrClus.ConfigurationEx.Rule | %{

            $oRuleInfo = New-Object -Type PSObject -Property @{

                ClusterName = $viewCurrClus.Name

                RuleName = $_.Name

                RuleType = $_.GetType().Name

                RuleEnabled = $_.Enabled

                Mandatory = $_.Mandatory

            } ## end new-object

            ## add members to the output object, to be populated in a bit

            "KeepTogether,VMNames,VMGroupName,VMGroupMembers,AffineHostGrpName,AffineHostGrpMembers,AntiAffineHostGrpName,AntiAffineHostGrpMembers".Split(",") | %{Add-Member -InputObject $oRuleInfo -MemberType NoteProperty -Name $_ -Value $null}

            ## switch statement based on the object type of the .NET view object

            switch ($_){

                ## if it is a ClusterVmHostRuleInfo rule, get the VM info from the cluster View object

                #   a ClusterVmHostRuleInfo item "identifies virtual machines and host groups that determine virtual machine placement"

                {$_ -is [VMware.Vim.ClusterVmHostRuleInfo]} {

                    $oRuleInfo.VMGroupName = $_.VmGroupName

                    ## get the VM group members' names

                    $oRuleInfo.VMGroupMembers = (Get-View -Property Name ($viewCurrClus.ConfigurationEx.Group | ?{($_ -is [VMware.Vim.ClusterVmGroup]) -and ($_.Name -eq $oRuleInfo.VMGroupName)}).Vm | %{$_.Name}) -join ","

                    $oRuleInfo.AffineHostGrpName = $_.AffineHostGroupName

                    ## get affine hosts' names

                    $oRuleInfo.AffineHostGrpMembers = if ($_.AffineHostGroupName -ne $null) {(Get-View -Property Name ($viewCurrClus.ConfigurationEx.Group | ?{($_ -is [VMware.Vim.ClusterHostGroup]) -and ($_.Name -eq $oRuleInfo.AffineHostGrpName)}).Host | %{$_.Name}) -join ","}

                    $oRuleInfo.AntiAffineHostGrpName = $_.AntiAffineHostGroupName

                    ## get anti-affine hosts' names

                    $oRuleInfo.AntiAffineHostGrpMembers = if ($_.AntiAffineHostGroupName -ne $null) {(Get-View -Property Name ($viewCurrClus.ConfigurationEx.Group | ?{($_ -is [VMware.Vim.ClusterHostGroup]) -and ($_.Name -eq $oRuleInfo.AntiAffineHostGrpName)}).Host | %{$_.Name}) -join ","}

                    break;

                } ## end block

                ## if ClusterAffinityRuleSpec (or AntiAffinity), get the VM names (using Get-View)

                {($_ -is [VMware.Vim.ClusterAffinityRuleSpec]) -or ($_ -is [VMware.Vim.ClusterAntiAffinityRuleSpec])} {

                    $oRuleInfo.VMNames = if ($_.Vm.Count -gt 0) {(Get-View -Property Name $_.Vm | %{$_.Name}) -join ","}

                } ## end block

                {$_ -is [VMware.Vim.ClusterAffinityRuleSpec]} {

                    $oRuleInfo.KeepTogether = $true

                } ## end block

                {$_ -is [VMware.Vim.ClusterAntiAffinityRuleSpec]} {

                    $oRuleInfo.KeepTogether = $false

                } ## end block

                default {"none of the above"}

            } ## end switch

            $oRuleInfo

        }

    } ## end if

  } ##end get-view

Error

drsruleserror.JPG

Error Text:

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null or empty. Provide an argument that is not null or empty, and then try the

command again.

At E:\Scripts\DRSRules\drsrules-export-updated.ps1:58 char:74

+                     $oRuleInfo.VMGroupMembers = (Get-View -Property Name ($viewC ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null or empty. Provide an argument that is not null or empty, and then try the

command again.

At E:\Scripts\DRSRules\drsrules-export-updated.ps1:58 char:74

+                     $oRuleInfo.VMGroupMembers = (Get-View -Property Name ($viewC ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

When you launch the script via the scheduler, are you using the same account?

In the scheduled script do you load the PowerCLI modules, and do you do a Connect-VIServer?


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

When you launch the script via the scheduler, are you using the same account?

In the scheduled script do you load the PowerCLI modules, and do you do a Connect-VIServer?


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

0 Kudos
virtualtech_wor
Enthusiast
Enthusiast
Jump to solution

Hi LucD

I got that fixed, it was indeed different account on task scheduler and connect-viserver authentication.

Thanks.

0 Kudos