Powershell script to list the managed paths


Recently I came across task to create a list of all the managed paths
for a SharePoint webapp and store it in a list. So here is a copy of
the Powershell script and what it does:

1. Reads the managed paths for a web application
2. Updates the list of managed path to a SharePoint list
3. Update the status of activity in a status list

Powershell script function


function syncManagedPaths([string] $webApp,[string] $listName,[string] $statusListName)
{
try
{
$managedPaths = get-spmanagedpath -webapplication $webApp
$spAssignment = Start-SPAssignment
$myList = (Get-SPWeb -Identity $TSAdmin -AssignmentCollection
$spAssignment).Lists[$listName]
$listItems = $myList.Items
$listItemCount = $listItems.Count
for($x = $listItemCount - 1;$x -ge 0;$x--)
{
$listItems[$x].Delete()
}
$countPaths = 0
foreach($managedpath in $managedpaths)
{
if($managedpath.Name -ne "" -and $managedpath.Name -ne "sites")
{
$newItem = $myList.Items.Add()
$newItem["Category"] = $managedpath.name
$countPaths = $countPaths + 1
$newItem["CategoryID"] = $countPaths;
$newItem.Update()
}
}
Stop-SPAssignment $spAssignment
$spAssignment = Start-SPAssignment
$myList = (Get-SPWeb -Identity $TSAdmin -AssignmentCollection
$spAssignment).Lists[$statusListName]
$newItem = $myList.Items.Add()
$newItem["Activity"] = "List managed paths"
$newItem["Status"] = "Successful";
$newItem["Comment"] = "All managed paths updated"
$newItem.Update()
Stop-SPAssignment $spAssignment
}
catch [Exception]
{
$managedPaths = get-spmanagedpath -webapplication $webApp
$spAssignment = Start-SPAssignment
$myList = (Get-SPWeb -Identity $TSAdmin -AssignmentCollection
$spAssignment).Lists[$statusListName]
$newItem = $myList.Items.Add()
$newItem["Activity"] = "Synchronise Category/Managed Paths"
$newItem["Status"] = "Failed";
$newItem["Comment"] = $_.Exception.ToString();
$newItem.Update()
Stop-SPAssignment $spAssignment
}
}

The “Managed path” list where the above function will store data
should have 2 columns:

1. Catergory – Single line of text
2. CategoryID - Number

Similarly the status list where the status of the activity will be
stored should have following column:

1. Activity name – single line of text
2. Status – single line of text
3. Comment – Multiple line of text

No comments:

Post a Comment