我们正在尝试检查CmdLet Hashtable
是否已针对特定计划任务调用了一次。但由于某种原因,Start-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')
}
}
}
时,我们可以清楚地看到CmdLet已被调用一次。所以我假设过滤器中必定存在语法错误。
答案 0 :(得分:1)
问题已解决:
Assert-MockCalled Start-ScheduledTask -Scope it -Times 1 -ParameterFilter {
($InputObject.TaskName -eq 'My task')
}