Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Monday, October 22, 2012

Creating SharePoint Custom List Columns in PowerShell

 

I needed to create a sizable test in SharePoint using a custom list.  I did not want to manually configure each column so I build a tab delimited text file that held the column configuration options and a PowerShell script to read it.  The first column of the file provides the field name, the second provides the options (they were separated by a pipe delimiter), and the third column is the field description.  I wanted all of the questions to be Radio Button choice fields so those options were hard coded.  I also wanted a letter and appended to each answer so I created an array of letters up to G. 

$w = get-spweb http://spsite
$l = $w.lists["MyTest"]

$letters = "A|B|C|D|E|F|G"
$lttr = $letters.split("|")
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Choice

$file = Get-Content c:\Questions.txt

foreach ($line in $file) {
  
    $fields = $line.ToString().Split("`t")
    $fName = $l.Fields.Add($fields[0],$spFieldType,$True)   
    write-host $fName
    $f = $l.Fields[$fName]
    $choices = $fields[1].split("|")

    for ($i=0; $i -lt $choices.count; $i++)
    {
        $choiceValue = $lttr[$i] + ". " + $choices[$i]
        $f.Choices.Add($choiceValue)
    }
    $f.description = $fields[2]
    $f.EditFormat = "RadioButtons"
    $f.update()

}

The script works like a champ, and I saved myself a little time and frustration of repetitively clicking the same options over and over again.

PowerShell to display SPN and Delegation Information for SharePoint Accounts

 

In troubleshooting Kerberos issues it is sometimes helpful to see the all the SPNs and delegate to settings for my various SharePoint accounts.  Since our SharePoint accounts are named in a consistent way this ended up being quite easy.  I set a filter that looks for accounts that start with the name PRD-SP and looked in the proper container in the Active Directory.  I piped the output to a file and I had a useful listing of all the SPNs and their delegate to settings.

$strFilter = "(&(objectCategory=User)(sAMAccountName=PRD-SP*))"

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = "LDAP://OU=SharePoint,OU=Special Accounts,DC=domain,DC=com"
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colProplist = "sAMAccountName","name","msDS-AllowedToDelegateTo","servicePrincipalName"

foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults) {
    #$objResult.Properties
    $objResult.Properties["name"]
    $objResult.Properties["samaccountname"]
    "================================================"
    "msDS-AllowedToDelegateTo"
    "------------------------------------------------"
    $objResult.Properties["msds-allowedtodelegateto"]
    "------------------------------------------------"
    "servicePrincipalName"
    "------------------------------------------------"
    $objResult.Properties["serviceprincipalname"]
    ""
    ""
}

Delete a Retention Information Management Policy that is Corrupt

 

We had a series of content type retention polices that for some reason ended up being corrupt.  When I would try to edit the policy through the user interface I would get an error:

image

No matter what I did through the user interface I would get that error.  To remedy this I wrote a little 4 line PowerShell script that deletes the policy.  The script accepts 3 arguments (site URL, list name, and content type). 

$w = Get-SPWeb $args[0]
$l = $w.lists[$args[1]]
$c = $l.ContentTypes[$args[2]]
[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::DeletePolicy($c)

The script deletes the policy and then the policy can be reconfigured through the site settings.

Force a Single User’s Profile to Sync With Active Directory

 

Occasionally we will have a user that changes their last name and wants it to reflect the change across SharePoint.  In order to aid this process I wrote this script to force a resynchronize of each instance of the user profile in the user information lists.  The script accepts one argument, which is the username to resync (“DOMAN\User”).

$webapps = Get-SPWebApplication
foreach ($webapp in $webapps) {
    [string] $login = $args[0]
    $sites = get-spsite -Limit All -WebApplication $webapp
    foreach ($s in $sites) {
        write-host $s.url
        $w = $s.RootWeb;
        $u = get-SPUser -Web $w -limit all | Where-Object {$_.userlogin -eq $login}
        if ($u -ne $null) {
            write-host "`t$($w.url)"
            Set-SPUser $u -SyncFromAD
            write-host "`tUpdated"
        }
    }
}

Showing left navigation for entire library of web part pages

 

I had a request to make a whole library full of web part pages show the left navigation.  These pages had been created and populated with web parts previously.  Instead of editing the template on each of these pages manually, I decided to script it using PowerShell. 

$w = get-spweb “http://sharepoint/sites/collection/subweb”
$f = $w.RootFolder.SubFolders["Pages"]
foreach ($p in $f.Files) {
    $enc = [system.Text.Encoding]::ASCII
    $b = $p.OpenBinary()
    $str = $enc.GetString($b)
    $new = $str -replace "<ContentTemplate>(.|\n)*</ContentTemplate>","<ContentTemplate></ContentTemplate>"
    $new = $new -replace "<asp:Content.*PlaceHolderLeftNavBar.*/asp:Content>",""
    $new = $new -replace "<asp:Content.*PlaceHolderLeftActions.*/asp:Content>",""
    $new = $new -replace "^\?\?\?",""
    $p.SaveBinary($enc.GetBytes($new))
}

The script opens each file in the “Pages” library that housed the web part pages and strips out the code that hides the left navigation.