我试图传递一个变量到powershell命令,像这样:
$Today = Get-Date
Get-SCSMClassInstance -Filter {ClosedDate -eq $Today}
但是我得到这个错误:
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
如果我直接传递字符串,不管格式如何,它都能正常工作:
Get-SCSMClassInstance -Filter {ClosedDate -eq "Friday, June 5, 2015 11:42:33 AM"}
Get-SCSMClassInstance -Filter {ClosedDate -gt "2015-6-5 11:42:33Z"}
我已经尝试将变量设置为DateTime以及字符串,每种可以想象的格式-我已经确保,当变量被解析时,它绝对会变成正确的字符串,但没有任何工作。
任何想法?
Filter参数是一个字符串,不是scriptblock。在其他情况下,它与scriptblock一起工作,因为脚本块的字符串形式是块的文本,没有周围的大括号。然而,在这种情况下,您需要在过滤器中替换变量。试试这个:
Get-SCSMClassInstance -Filter "ClosedDate -eq ""$Today"""