FIM ScriptBox Item

Summary

The objective of this script is to create a backup of all ObjectVisualizationConfiguration objects as XML that are exported by FIM Automation cmdlets.
Each Resource Control Display Configuration (RCDC) object is saved in a separate file and the name of the the file is the object's identifier.

The script takes one optional parameter: ConfigurationBackupPath.
ConfigurationBackupPath
is the path to a customer backup folder.
The default is: C:\FIMBackup\ResourceControlDisplayConfiguration\

The script verifies whether the following three prerequisites are satisfied before processing the script code:

  1.  The script must be run by a user with administrative rights.
  2.  The script must be run on a computer running the FIM service.
  3.  The script must be run by a user that has write permissions to ConfigurationBackupPath.

If any of these conditions are not satisfied, the script stops and an error is displayed.

Script Code

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Script to back all the Resource Control Display Configuration Objects
# This script is intended to be run before upgrade to FIM 2010 R2
#------------------------------------------------------------------------------------------------------
 
#------------------------------------------------------------------------------------------------------
# Script Parameter Declarations
param
(
    # Data Warehouse machine name
    [parameter(Mandatory=$false)]
    [String]$ConfigurationBackupPath = "C:\FIMBackup"
)
# End Script parameter declarations
#------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------
# Script constant declarations

Set-Variable -Name constantFIMPowerShellSnapInName -Option Constant -Value "FIMAutomation" -ErrorAction SilentlyContinue
Set-Variable -Name constantRCDCFolderName -Option Constant -Value "ResourceControlDisplayConfiguration" -ErrorAction SilentlyContinue
Set-Variable -Name constantRCDCObjectFilter -Option Constant -Value "/ObjectVisualizationConfiguration" -ErrorAction SilentlyContinue
Set-Variable -Name constantLineSeperator -Option Constant -Value "----------------------------------------------------------------------------------------------------------" -ErrorAction SilentlyContinue

# End Script constant declarations
#------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------
# Function definitions

#--------------------------------------------------------------
# Checks to see if the user running the script is an
# administrative user
#--------------------------------------------------------------
function In-Administrator-Mode
{
    # check that current user is in administrators group.
    try
    {
        # get the current user who is executing the script.
        $currentUser =  [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $windowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($CurrentUser)
        
        # Is the user in the Admin group.
        return $windowsPrincipal.IsInRole("Administrators")
    }
    catch
    {
        Write-Error $_
        return $false
    }
}

#--------------------------------------------------------------
# Checks to see if the FIMAutomation is installed.
#--------------------------------------------------------------
function Is-FIM-Powershell-SnapIn-Registered
{
    try
    {
        # get registry key value
        Add-PSSnapin -Name $constantFIMPowerShellSnapInName -ErrorAction SilentlyContinue            
        return $true
    }
    catch
    {
        Write-Error $_      
        return $false
    }
}

#--------------------------------------------------------------
# Writes an informational message to the Console
# $message : message text to be written to the console
#--------------------------------------------------------------
function Write-Informational-Message([String]$message)
{
    Write-Host $message -ForegroundColor "green"
}

#--------------------------------------------------------------
# Writes an error message to the Console
# $message : message text to be written to the console
#--------------------------------------------------------------
function Write-Error-Message([String]$message)
{
    Write-Host $message -ForegroundColor "red"
}

#--------------------------------------------------------------
# Saves the RCDC objects to files
# $rcdcs : List of objects to be saved
# $filePath : Path to save the files at.
#--------------------------------------------------------------
function Save-RCDC-Objects([Object[]]$rcdcs, [String]$filePath)
{       
    foreach($rcdc in $rcdcs)
    {        
        $fileName = $rcdc.ResourceManagementObject.ObjectIdentifier.Substring(9) + ".xml"
        $fullPath = Join-Path -Path $filePath -ChildPath $fileName
        ConvertFrom-FIMResource $rcdc -File $fullPath               
    }
}

# End Function declarations
#------------------------------------------------------------------------------------------------------

#------------------------------------------------------------------------------------------------------
# Main Script Logic

try
{
    Write-Host $constantLineSeperator
    Write-Informational-Message "Resource Control Display Configuration backup script."
    Write-Informational-Message "This script will back up all resource control display configuration objects."
 

    # Check to see if the script is being run by an administrative user

    $inAdminMode = In-Administrator-Mode  
    if ($inAdminMode -ne $true)
    {
        Write-Error-Message "This script is not being run by an administrator. Please re-run script as an administrative user"
        EXIT
    }

    # check to see if the FIM powershell snapin is installed
    Write-Host $constantLineSeperator
    Write-Informational-Message "Starting Pre-Requisite checks before script execution"
    Write-Host $constantLineSeperator
    Write-Informational-Message "Pre-Requisite check #1"
    Write-Informational-Message "Checking to see if FIM commandlets are installed"
    
    $isFIMPSSnapinInstalled = Is-FIM-Powershell-SnapIn-Registered
    
    if($isFIMPSSnapinInstalled -eq $false)
    {
        Write-Error-Message "FIM commandlets are not installed on this machine. Please run the script on a machine where FIM service is installed."
        Write-Host $constantLineSeperator
        EXIT
    }
    Write-Informational-Message "FIM commandlets are installed on local machine"

    # check to see if the configuration back-up path is valid
    Write-Informational-Message "Pre-Requisite Check #2"
    Write-Informational-Message "Checking to see if the configuration backup folder path is valid"

    if((Test-Path $ConfigurationBackupPath -PathType container)  -ne $true)
    {
        Write-Informational-Message "Configuration Backup directory does not exist. Creating the directory"   
        New-Item $ConfigurationBackupPath -type directory | out-null
        Write-Informational-Message "Configuration Backup directory created"
    }

    Write-Informational-Message "All Pre-Requisite checks passed."
    Write-Host $constantLineSeperator

    Write-Host $constantLineSeperator
    Write-Informational-Message "Setting up folders to backup the configuration settings"
    Write-Host $constantLineSeperator

    # check to see if the RCDC folder is already present. If not, create it.
    $rcdcPath = Join-Path -Path $ConfigurationBackupPath -ChildPath $constantRCDCFolderName
    $saveMessage = "The configuration settings will be saved at : " + $rcdcPath
    Write-Host $saveMessage -foregroundcolor "yellow"
    
    if((Test-Path $rcdcPath -PathType container) -eq $true)
    {
        Write-Informational-Message "The specified directory is already present"
    
        if((Get-ChildItem $rcdcPath) -ne $null)
        {
            Write-Informational-Message "There is already a saved back-up in this directory"
            Write-Informational-Message "Please save the contents in another directory before re-running this script"
            Write-Host $constantLineSeperator
            EXIT
        }   
    }
    else
    {
        Write-Informational-Message "Creating the resource control display configuration backup directory"
        New-Item $rcdcPath -type directory | out-null
        Write-Informational-Message "Created the resource control display configuration backup directory"
    }
    
    # get all the rcdc objects from the FIM Service store.
    Write-Host $constantLineSeperator
    Write-Informational-Message "Gathering all resource control display configuration objects"
    $rcdcObjects = Export-FIMConfig -CustomConfig $constantRCDCObjectFilter 
    $resultMessage = "Found " + $rcdcObjects.Count + " objects"
    Write-Informational-Message $resultMessage
    Write-Host $constantLineSeperator
    
    # Write them out to files as XML
    Write-Host $constantLineSeperator
    Write-Informational-Message "Saving all the resource control display configuration objects"
    Save-RCDC-Objects $rcdcObjects $rcdcPath
    Write-Informational-Message "Successfully saved all the resource control display configuration objects"
    Write-Host $constantLineSeperator
}
catch [Exception]
{
    Write-Error-Message "Script execution failed with the following exception message"
    Write-Host $_.Exception.ToString()
}

# End Script code
#------------------------------------------------------------------------------------------------------

 

note Note
To provide feedback about this script, create a post on the FIM TechNet Forum.
For more FIM related Windows PowerShell scripts, see the FIM ScriptBox.


See Also