I have had this lying around for a while now, and thought I would share. Script as follows (Download here - please rate):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | ############################################################### # Backup service manager management packs # # Authored by: # Anders Spælling, [email protected] # ############################################################### # EVENT IDs # 800 - Warning - Backup completed with errors # 700 - Error - Unable to import module # 701 - Error - Failed to backup unsealed MPs # 702 - Error - Failed to backup sealed MPs # 703 - Error - Failed to create new backup folder # 600 - Information - Succesfully backed up unsealed MPs # 601 - Information - Succesfully backed up sealed MPs # 602 - Information - Removed old backup (unsealed MPs) # 603 - Information - Removed old backup (sealed MPs) # 604 - Information - Backup done # 605 - Information - Starting MP backup job # CONSTANTS # Write to event log on this computer $EventLogComputerName = '' $EventLogName = "SCSM backup task" $Date = Get-Date # user defined # # definde rootpath to save managemen packs. Should be UNC format $RootPath = '\SCSM_MP\' # Define service manager management server $SMMS = $EventLogComputerName # keep MP backups for this many days $BACKUP_RETAIN_IN_DAYS = 28 # increase if failed to backup MPs $ErrorCount = 0 # used to write to event log # Example use, create event with event ID 702 and type Error: CreateEventLog "Error description" 702 "Error" Function CreateEventLog { Param($EventDescription,$EventID,$Type) $EventlogExists = Get-EventLog -ComputerName $EventLogComputerName -List | Where-Object {$_.LogDisplayName -eq $EventLogName} If(-not $EventlogExists) { New-EventLog -LogName $EventLogName -Source AlertUpdate -ComputerName $EventLogComputerName } Write-EventLog -ComputerName $EventLogComputerName -LogName $EventLogName -Source AlertUpdate -Message "$EventDescription" -EventId $EventID -EntryType $Type } # remove SMLets module (from session) if loaded - needed to load the System.Center.Service.Manager module as command names overlap if(Get-Module -Name SMLets) { Remove-Module 'SMLets' } # find module in '%SMinstalldir%\Powershell\System.Center.Service.Manager.psd1' $SMInstallDir = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\System Center\2010\Service Manager\Setup').InstallDirectory $ModuleDir = $SMInstallDir + 'Powershell\System.Center.Service.Manager.psd1' Import-Module $ModuleDir if(-not (Get-Module -Name System.Center.Service.Manager)) { CreateEventLog "Unable to import module System.Center.Service.Manager`nException message: $($_.Exception.Message)" 700 "Error" Exit } ####################### # BACKUP UNSEALED MPS # ####################### CreateEventLog "Starting MP backup job" 605 "Information" # Define path to save todays backup to $Path = $RootPath + $Date.ToString('yyyy-MM-dd') # create path if it does not exists If (-not (Test-Path $Path -ErrorAction Stop)) { try { $CreateOutput = New-Item -ItemType Directory $Path } catch [System.Exception] { CreateEventLog "Unable to create new backup folder $Path`nException message: $($_.Exception.Message)" 703 "Error" $ErrorCount++ } } try { # get unsealed MPs and export to disk Get-SCManagementPack -ComputerName $SMMS | where{$_.sealed -eq $False} | Export-SCManagementPack -Path $Path CreateEventLog "Succesfully backed up unsealed MPs" 600 "Information" } catch [System.Exception] { CreateEventLog "Failed to backup unsealed MPs`nException message: $($_.Exception.Message)" 701 "Error" $ErrorCount++ } finally { # } ##################### # BACKUP SEALED MPS # ##################### # Define path to save todays backup to $Path = $RootPath + $Date.ToString('yyyy-MM-dd') + "\sealed" # create path if it does not exists If (-not (Test-Path $Path)) { $CreateOutput = New-Item -ItemType Directory $Path } try { # get sealed MPs and export to disk Get-SCManagementPack -ComputerName $SMMS | where{$_.sealed -eq $True -and $_.Name -like "XX*"} | Export-SCManagementPack -Path $Path CreateEventLog "Succesfully backed up sealed MPs" 601 "Information" } catch [System.Exception] { CreateEventLog "Failed to backup sealed MPs`nException message: $($_.Exception.Message)" 702 "Error" $ErrorCount++ } finally { # } ########### # CLEANUP # ########### # remove backup folder from $BACKUP_RETAIN_IN_DAYS days ago $DeleteFolder = $Date.AddDays(-$BACKUP_RETAIN_IN_DAYS) $DeletePath = $RootPath + $DeleteFolder.ToString('yyyy-MM-dd') # remove folder if it exists If (Test-Path $DeletePath) { $RemoveOutput = Remove-Item $DeletePath -Recurse CreateEventLog "Removed old backup of MPs in `"$DeletePath`"" 602 "Information" } # remove module (from session) if(Get-Module -Name System.Center.Service.Manager) { Remove-Module 'System.Center.Service.Manager' } if($ErrorCount -gt 0) { CreateEventLog "Backup completed with $ErrorCount errors" 800 "Warning" } else { CreateEventLog "Backup done" 604 "Information" } |
Ingen kommentarer:
Send en kommentar
Bemærk! Kun medlemmer af denne blog kan sende kommentarer.