Export User Roles to file (powershell)

PowerShell script to write all User Roles to a text file.  Copy and save code to file with .ps1 extension.  Execute the *.ps1 script from within SCOM Command Shell or PowerShell.  User input prompts will not work if copying and pasting code directly into shell.

The output file can be used in conjunction with the ImportUserRoles script.  Format of the output file is as follows:

Profile: <monitoringProfile>
Name: <userRoleName>
DisplayName: <userRoleDisplayName>
Description: <userRoleDescriptioin>
Users: <account1 account2 account3>

##--------------------------------------------------##
# Use this script to export User Roles to a text
# file. The output file from this script can be
# used in conjunction with the ImportUserRoles
# script.
# Author: Jonathan Almquist
# Name: ExportUserRoles.ps1
# Ver: 6.0.6278.0-2
# Date: 03/20/2008
# Revisions:
# -Added user input prompt for RMS server name
# -Added user input prompt for file path & name
# --quotes not required for path with spaces
# -Added pssnapin checking
# -Added psdrive checking
# -Management Group connectivity from PS
##--------------------------------------------------##
## Get user input
$rms = read-Host "Enter the RMS server name"
$filename = read-Host "Enter path and filename for output file (ex: c:\user_roles.txt)"
## Check for Operations Manager Snap-in
$snapin = pssnapin | select-Object name
$added = 0
## Loop through each instance of Snap-in
foreach ($pssnapin in $snapin)
{
if ($pssnapin -like "*Microsoft.EnterpriseManagement.OperationsManager.Client*")
{
$added = 1
}
}
if ($added -eq 0)
{
add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"
write-Host "Operations Manager Snap-in added."
}
else
{
write-Host "Operations Manager Snap-in already added."
}

set-location "OperationsManagerMonitoring::"
## Check for Monitoring Drive
$drive = psdrive | select-Object name
$added = 0
## Loop through each instance of Drive
foreach ($psdrive in $drive)
{
if ($psdrive -like "*Monitoring*")
{
$added = 1
}
}
if ($added -eq 0)
{
New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root: \
write-Host "Monitoring Drive added."
}
else
{
write-Host "Monitoring Drive alreaded added."
}
## Connect to Management Group
New-ManagementGroupConnection -ConnectionString: $rms
cd Monitoring:\$rms
## Gather information
$mgName = (get-item .).ManagementGroup.name
$date = get-date -uformat "%Y-%m-%d"
$userroles = get-userrole | select-object name,displayname,description,monitoringprofile,users
## Write output file
foreach ($role in $userroles)
{
"Profile: " + $role.monitoringprofile.name + "`r`n" + "Name: " + $role.name + "`r`n" + "DisplayName: " + $role.displayname + "`r`n" + "Description: " + $role.description + "`r`n" + "Users: " + $role.users + "`r`n" | out-file $filename -append
}
write-Host "Process complete"
##
##