1

following code

function Get-SSMMaintenanceWindowMatch ($instance) {
  $mws = (Get-SSMMaintenanceWindowList -Region eu-central-1).WindowId
  for ($i=0; $i -le $mws.Length; $i++)  {
        $val = ((Get-SSMMaintenanceWindowTargets -region eu-central-1 -WindowId $mws[$i]).Targets).Values
        if (($val -eq $instance) -and ($val -eq $null)) {
        return $instance
        else {
        return -1
             }

      }
   }
}

gives following error.

PS C:\WINDOWS\system32> Get-SSMMaintenanceWindowMatch $instanceId
Get-SSMMaintenanceWindowTargets : 1 validation error detected: Value null at 'windowId' failed to satisfy constraint: Member must not be 
null
At line:4 char:18
+ ...    $val = ((Get-SSMMaintenanceWindowTargets -region eu-central-1 -Win ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Amazon.PowerShe...owTargetsCmdlet:GetSSMMaintenanceWindowTargetsCmdlet) [Get-SSMMaintenan 
   ceWindowTargets], InvalidOperationException
    + FullyQualifiedErrorId : Amazon.SimpleSystemsManagement.AmazonSimpleSystemsManagementException,Amazon.PowerShell.Cmdlets.SSM.GetSSMMa 
   intenanceWindowTargetsCmdlet

Some Maintenance Windows will not have a value since not targets (instances) are registered to them. In that momement a member of the array is null and the function doesnt work. If I use an instance that is registered directly it works.

How can I get the loop to work with just null members.

Thx for any input.

ghraz0r
  • 31
  • 5
  • What are you trying to do here? ($val -eq $instance) -and ($val -eq $null) Also, this may not be accurate to search against instance - if the maintenance window target is based of tags – Ketanbhut May 09 '19 at 14:59
  • Hi, sry. That is an error but with the two conditions. I already corrected it but the instance is not based of tags. Its just a function given inside. – ghraz0r May 09 '19 at 15:22

1 Answers1

1

I think the problem lies in your for loop. You have

 for ($i=0; $i -le $mws.Length; $i++)

-le is the operator for 'less than or equal' so you are walking past the end of the array and that's where I think you are picking up a null entry. I think you need -lt instead:

 for ($i=0; $i -lt $mws.Length; $i++)
Steve Roberts
  • 714
  • 4
  • 8