0

我有以下用于删除暂停服务实例的 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 脚本中添加“创建时间少于某个值”(假设是上周五)?

4

1 回答 1

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!"
}
于 2017-05-07T15:38:46.793 回答