How to List Change Requests Satisfying Multiple Criteria

Nov 18, 2011 at 5:10 PM

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

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".
At C:\users\leslier_buchanan\Documents\scripts\CRs.ps1:11 char:23
+ $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.
At C:\users\leslier_buchanan\Documents\scripts\CRs.ps1:12 char:25
+ Get-SCSMObject -Criteria <<<<  $criteria
    + CategoryInfo          : InvalidData: (:) [Get-SCSMObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,SMLets.GetSMObjectCommand

Any help would be greatly appreciated

Dec 26, 2011 at 1:52 PM

Guess it may be too late...

Just change this: $class = "Get-SCSMClass -name System.WorkItem.ChangeRequest$"

To this: $class = Get-SCSMClass -name System.WorkItem.ChangeRequest$


And it works really well!

Thank you, by the way