none
How to Get a List of Change Requests that meet multiple Criteria

    Question

  • I am attempting to get a list of Change requests that meet a specific criteria using the following code:

    # Convert to UTC Date time
    $Date = (get-date);
    $UTCDate = $Date.ToUniversalTime();

    import-module smlets;
    $statusCompletedId = (Get-SCSMEnumeration ChangeStatusEnum.Completed$).Id


    $7DaysOld = (get-date).addhours(-1);
    Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.ChangeRequest$) | where{$_.lastmodified -lt $7DaysOld -AND $_.CreatedDate -gt '2010-11-08' -AND $_.Status -like $statusCompletedID}|Format-Table DisplayName, LastModified, CreatedDate, Status;

    In listing all the CRs I have confirmed there are CRs that meet my criteria. But when I run the above code, nothing is returned.

    I have tried modifying the code at http://myitforum.com/myitforumwp/2011/11/14/scsm-auto-closing-a-change-request-dual-criteria/?utm_source=rss&utm_medium=rss&utm_campaign=scsm-auto-closing-a-change-request-dual-criteria

    as follows:

    # Convert to UTC Date time
    $Date = (get-date);
    $UTCDate = $Date.ToUniversalTime();

    import-module smlets;
    $7DaysOld = (get-date).addhours(-1);
    $class = "Get-SCSMClass -name System.WorkItem.ChangeRequest$"
    $statusCompletedId = (Get-SCSMEnumeration ChangeStatusEnum.Completed$).Id
    $critString = "Status = ‘$statusCompletedId’ AND LastModified < '$7daysOld'"
    $criteriaType = "Microsoft.EnterpriseManagement.Common.EnterpriseManagementObjectCriteria"
    $criteria = New-Object $criteriaType $critString, $class
    Get-SCSMObject -Criteria $criteria

    remove-module smlets;

    But get the following error:

    New-Object : Cannot find an overload for "EnterpriseManagementObjectCriteria" and the argument count: "2".
    + $criteria = New-Object <<<< $criteriaType $critString, $class
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    Get-SCSMObject : Cannot bind argument to parameter 'Criteria' because it is null.
    + Get-SCSMObject -Criteria <<<< $criteria
    + CategoryInfo : InvalidData: (:) [Get-SCSMObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,SMLets.GetSMObjectCommand

    Any help would be greatly appreciated


    • Edited by LDobell Monday, November 21, 2011 8:24 PM
    Monday, November 21, 2011 8:23 PM

Answers

  • $7DaysOld = (get-date).addhours(-1);
    $class = "Get-SCSMClass -name System.WorkItem.ChangeRequest$"
    $statusCompletedId = (Get-SCSMEnumeration ChangeStatusEnum.Completed$).Id
    $critString = "Status = ‘$statusCompletedId’ AND LastModified < '$7daysOld'"
    $criteriaType = "Microsoft.EnterpriseManagement.Common.EnterpriseManagementObjectCriteria"
    $criteria = New-Object $criteriaType $critString, $class
    Get-SCSMObject -Criteria $criteria

     

    There was nothing wrong with your criteria, just remove the quotes!  Or else your $class is a String rather than a ManagementPackClass type :)

    $class = Get-SCSMClass -Name System.WorkItem.ChangeRequest$


    Thursday, January 19, 2012 7:00 AM