我有以下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脚本中添加“创建时间少于某个值”(假设上周五)?
答案 0 :(得分:1)
繁琐的部分是转换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!"
}