我们正在尝试检查 CmdLetStart-ScheduledTask
是否已为特定的计划任务准确调用了一次。但由于某种原因,ParameterFilter
不匹配。
代码
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
Function New-TaskObjectsHC {
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[HashTable[]]$Hash
)
Process {
foreach ($H in $Hash) {
$Obj = New-Object -TypeName 'Microsoft.Management.Infrastructure.CimInstance' -ArgumentList @('MSFT_ScheduledTask')
$H.GetEnumerator() | ForEach-Object {
$Obj.CimInstanceProperties.Add([Microsoft.Management.Infrastructure.CimProperty]::Create($_.Key,$_.Value, [Microsoft.Management.Infrastructure.CimFlags]::None))
}
$Obj
}
}
}
Describe 'Monitor sheduled tasks' {
it 'test' {
Mock Get-ScheduledTask {
@(
@{
TaskName = 'My task'
State = 'Running'
}
) | New-TaskObjectsHC
}
Mock Start-ScheduledTask
Start-ScheduledTask -InputObject (Get-ScheduledTask)
Assert-MockCalled Start-ScheduledTask -Scope it -Times 1 -ParameterFilter {
($TaskName -eq 'My task')
}
}
}
当我们删除时,ParameterFilter
我们可以清楚地看到 CmdLet 已经被调用了一次。所以我假设过滤器中一定有语法错误。