0

I'm trying to pass a variable into a powershell command like so:

$Today = Get-Date
Get-SCSMClassInstance -Filter {ClosedDate -eq $Today}

But I get this error:

Get-SCSMClassInstance : ClosedDate_C529833E_0926_F082_C185_294CBC8BB9FD='$Today' 
-- String was not recognized as a valid DateTime.
At line:1 char:1
+ Get-SCSMClassInstance -ComputerName $computer $IncidentClass -Filter 
{ClosedDate ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Microsoft.Syste...InstanceComman 
   d:GetSCClassInstanceCommand) [Get-SCClassInstance], UnknownDatabaseException
+ FullyQualifiedErrorId : ExecutionError,Microsoft.SystemCenter.Core.Commands 
   .GetSCClassInstanceCommand

If I pass the string directly, it works fine, regardless of format:

Get-SCSMClassInstance -Filter {ClosedDate -eq "Friday, June 5, 2015 11:42:33 AM"}
Get-SCSMClassInstance -Filter {ClosedDate -gt "2015-6-5 11:42:33Z"}

I've tried setting the variable as DateTime as well as a string, every imaginable format - I've ensured that, when the variable is parsed, it will absolutely turn into the correct string, but nothing works.

Any ideas?

1 Answer 1

2

The Filter parameter is a string not scriptblock. It is working with a scriptblock in those other cases because the string form of script block is the text of the block without the surrounding braces. However, in this case, you need the variable to be substituted in the filter. Try this:

Get-SCSMClassInstance -Filter "ClosedDate -eq ""$Today"""
Sign up to request clarification or add additional context in comments.

1 Comment

So simple! Thanks so much! I had to enclose the entire expression in parenthesis - Get-SCSMClassInstance -Filter ("ClosedDate -eq $Today"), and it did the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.