根据创建时间删除挂起的服务实例

Delete suspended service instance depending on creation time

我有下面的 PowerShell 脚本用于删除挂起的服务实例。

Get-WmiObject MSBTS_ServiceInstance -Namespace 'root\MicrosoftBizTalkServer' -Filter 'ServiceStatus=4 OR ServiceStatus=32'  |
  %{ Get-WmiObject MSBTS_ServiceInstance -Namespace 'root\MicrosoftBizTalkServer' -Filter "InstanceID='$($_.InstanceID)'" } |
  %{ $_.Terminate() } |
  %{ Out-Null }

如何在上面的 PowerShell 脚本中添加 "creation time less then some value"(假设是上周五)?

繁琐的部分是转换 ActivationTime,由于某种原因它显示为字符串,而不是文档中预期的日期时间: https://msdn.microsoft.com/en-us/library/ee268286%28v=bts.10%29.aspx?f=255&MSPPError=-2147217396

试了一下,下面删除了从现在起超过 5 天的所有暂停消息:

# Now minus 5 days
$OlderThan = (get-date).AddDays(-5)

Write-Host "Will look for suspended message(s) older than:"$OlderThan

# Convert from date to string
$OlderThan = $OlderThan.ToString("yyyyMMddHHmmss")

# Get all suspended messages older than value in $OlderThan
$SuspendedMsgs = Get-WmiObject MSBTS_ServiceInstance -namespace 'root\MicrosoftBizTalkServer' | Where-object {$_.ServiceStatus -eq 4 -or $_.ServiceStatus -eq 32 -and $_.ActivationTime -lt $OlderThan}

# If any suspended messages matching critiera found
If($SuspendedMsgs){
    # For each suspended message 
    $SuspendedMsgs | ForEach-Object {
        Write-host "---------"
        $ConvertedDate = [datetime]::ParseExact($_.ActivationTime.Substring(0,14),'yyyyMMddHHmmss',[Globalization.CultureInfo]::InvariantCulture )
        Write-host "Suspended message on service name:"$_.ServiceName"from"$ConvertedDate" will be deleted!"
    }
    # Start terminating!
    Write-host "---------"
    Write-host "Termination started..."
    $SuspendedMsgs.Terminate() | Out-null
    Write-host "Termination completed!"
} else {
    write-host "No suspended messages found!"
}