Wbadmin& powershell - 最新的备份版本标识符

时间:2013-04-18 06:44:05

标签: powershell

我需要在powershell脚本中获取最新的备份版本标识符。如果我运行wbadmin get版本,我会得到一个备份列表,最后一个是我需要的备份。

有没有办法做一种select top 1 version identifier from backups order by date或解析wbadmin输出并得到它。

修改

它可能是我正在寻找的get.WBBackupSet的windows.serverbackup模块和versionId,但仍然需要帮助来解析它。

VersionId        : 04/17/2013-21:00
BackupTime       : 17/04/2013 22:00:55
BackupTarget     : U:
RecoverableItems : Volumes, SystemState, Applications, Files, BareMetalRecovery
Volume           : {System Reserved, Local disk (C:), Local disk (I:), Local disk (O:)...}
Application      : {"Cluster", "Registry", "Microsoft Hyper-V VSS Writer"}
VssBackupOption  : VssFullBackup
SnapshotId       : 58999c7d-dfbf-4272-a5b9-21361d171486

2 个答案:

答案 0 :(得分:2)

尝试一下,使用-Last而不是-First来获取最后一项:

Get-WBBackupSet | 
Sort-Object BackupTime | 
Select-Object -First 1 -ExpandProperty VersionId

您还可以使用-Ascending开关

播放排序顺序

答案 1 :(得分:2)

编辑:修订版

用于混合环境(撰写本文时为Windows Server 2008,2008R2,2012,2012R2):

function Get-MyWBSummary
{
<#
    .SYNOPSIS
        Retrieves the history of backup operations on the local or any number of remote computers.

    .DESCRIPTION
        The Get-MyWBSummary cmdlet retrieves the history of backup operations on the local or any number of remote computers with remoting enabled. This information includes backuptime, backuplocation, bersion identifier and recovery information.  
        To use this cmdlet, you must be a member of the Administrators group or Backup Operators group on the local or remote computer, or supply credentials that are.

    .PARAMETER ComputerName
        Retrives backup results on the specified computers. The default is the local computer.
        Type the NetBIOS name, an IP address, or a fully qualified domain name of one or more computers. To specify the local computer ignore the ComputerName parameter.
        This parameter rely on Windows PowerShell remoting, so your computer has to be configured to run remote commands.

    .PARAMETER Credential
        Specifies a user account that has permission to perform this action. The default is the current user. Type a user name, such as "User01", "Domain01\User01", or User@Contoso.com. Or, enter a PSCredential object, such as an object that is returned by the Get-Credential cmdlet. When you type a user name, you are prompted for a password.

    .PARAMETER Last
        Specifies the last (newest/latest) backup versions.

    .EXAMPLE 
        Get-MyWBSummary
        Retrieves all Windows Server backupversions from the local computer

    .EXAMPLE 
        Get-MyWBSummary | Where BackupTime -gt (Get-Date).AddDays(-7)
        Retrieves all Windows Server backupversions from the local computer within the last week    

    .EXAMPLE 
        Get-MyWBSummary -ComputerName $server1, $server2 -Last 1 -Credential $credential -ErrorAction SilentlyContinue -ErrorVariable sessionErrors         
        Retrieves the last (newest) Windows Server Backup backupversion from remote servers $server1 and $server2

    .NOTES
        Written by Anders Præstegaard (@aPowershell).
        Version 1.0 (20-01-2016)
#>  
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param
    (
        [string[]]$ComputerName = $env:COMPUTERNAME,
        [System.Management.Automation.PSCredential]$Credential,
        [int]$Last
    )

    begin
    {
        if ($Credential)
        {
            $PSDefaultParameterValues['New-PSSession:Credential'] = $Credential
        }

        $psSession = New-PSSession -ComputerName $ComputerName
    }
    Process
    {

        $scriptBlock = {

            if (-not (Test-Path -Path 'C:\Windows\System32\wbadmin.exe'))
            {
                ## Windows Server Backup not installed
                continue
            }

            $content = WBAdmin.exe GET VERSIONS
            if (-not $content)
            {
                ## no versions found
                continue
            }

            ## Get linenumbers for each entity
            $newJobLines = @($content | Select-String -Pattern 'Backup time: ')

            if ($Using:Last -and $using:Last -lt $newJobLines.Count)
            {
                $newJobLines = $newJobLines[- $using:Last.. -1]
            }

            $newJobLines |
            ForEach-Object{
                ## Location
                $lineNumberLocation = $_.LineNumber
                $backupLocation = $content[$lineNumberLocation] -replace 'Backup location: '

                ## Version Identifier
                $lineNumberVersionIdentifier = $_.LineNumber + 1
                $backupVersionIdentifier = $content[$lineNumberVersionIdentifier] -replace 'Version identifier: '

                ## Backuptime UTC
                # Version identifier string in WBAdmin output represents the UTC datetime formated in 'MM/dd/yyyy-HH:mm'
                $wbAdminDateStringFormat = 'MM\/dd\/yyyy-HH:mm'
                $backupDateTimeFromVersionIdentifier = [DateTime]::ParseExact($backupVersionIdentifier, $wbAdminDateStringFormat, $null)
                $backupDateTimeUtcSpecified = [DateTime]::SpecifyKind($backupDateTimeFromVersionIdentifier, [System.DateTimeKind]::Utc)
                # NB WBAdmin calculates the time statically compared to your timezone (ie +1 hour)
                # If your timezone support "Daylight Saving Time" then WBAdmin calculation is wrong 
                # ~ half of the year (as far as I can perceive)
                $backupDateTimeLocalTime = $backupDateTimeUtcSpecified.ToLocalTime()

                ## Can recover
                $lineNumberCanRecover = $_.LineNumber + 2
                $backupVersionCanRecover = $content[$lineNumberCanRecover] -replace 'Can recover: '

                [PSCustomObject]@{
                    BackupTime = $backupDateTimeLocalTime
                    BackupTimeUtc = $backupDateTimeUtcSpecified
                    BackupLocation = $backupLocation
                    VersionIdentifier = $backupVersionIdentifier
                    CanRecover = $backupVersionCanRecover
                }
            }

        } # Scriptblock

        Invoke-Command -Session $psSession -ScriptBlock $scriptBlock |
        Select-Object -Property * -ExcludeProperty RunspaceId
    }
    end
    {
        if ($psSession)
        {
            Remove-PSSession -Session $psSession
        }
    }
}