PowerShell Check for Empty SecureString Entry

A few days ago my friend Todd Klindt (click here for his blog, I get helpful advice there all the time) asked the following question on Twitter: ‘Is there a way to see if someone just his enter with “read-host –assecurestring”? I need to test for no input. #powershell’.

SecureString1

Problem

As you may know, secure strings in PowerShell are not readable (unless using ConvertFrom-SecureString or some alternate process).  If attempt to check equality of a secure string against an empty string or the null variable $null you will return a result of false even if the user didn’t enter anything.  How can you check if the secure string is empty then?

 

Solution

What you do have access to is the Length property of the secure string.  Store the input of your secure string into a variable and check it’s length property equals to 0.  If the length is equals to 0 then the user didn’t enter anything.  If it greater than 0 then they did enter something.  See the screenshot example below.

SecureString2

Example:

$myInput = Read-Host -AsSecureString -Prompt "Enter phrase"
if($myInput.Length -eq 0){Write-Host "No input detected"}
else{Write-Host "Input detected"}

Conclusion

This is a very short but not entirely intuitive answer to the question of how to check if a secure string is empty.  At first I thought the length value would be obscured, but since the PowerShell host does visibly show the number of asterisk ‘*’ characters I guess someone else would be able to find out that information easily as well.  I haven’t thoroughly tested this solution but it appears to be working thus far.  Let me know if you find any issues with this approach.

-Frog Out

PowerShell Script To Traverse All Sites In SharePoint 2010 (or 2007) Farm

    Over the past few years I’ve written a number of blog posts on performing various actions against a site collection or web application (display site collection admins, find all SPShell admins with database, find closed web parts).  Invariably with every post I get some comments along the lines of “this is great, how can I run this against every site in the farm”.  Well today you get your wish (sort of).  Below you will find a template script that traverses all sites within your local farm.  Isn’t that great!?!

    In it’s current state this script will simply output the title and URL of every site within the farm.  You may modify the function to perform your desired actions.  One stipulation is that you must have proper access to each of the web applications / site collections in order to actually traverse them.  Please leave any feedback that you have on this template in the comments.

 

Scripts

SharePoint 2010

Download the SharePoint 2010 template here.

 

Here is the source as well

function RecurseSiteAndDoSomething() {
    param([Microsoft.SharePoint.SPWeb]$SiteIdentity)

    Write-Output "Site: $($SiteIdentity.Url)"
    
    if($SiteIdentity.Webs.Count -gt 0)
    {
        foreach($subWeb in $SiteIdentity.Webs)
        {
            RecurseSiteAndDoSomething -SiteIdentity $subWeb
        }
    }
}

$contentWebAppServices = (Get-SPFarm).services |
 ? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"}

foreach($webApp in $contentWebAppServices.WebApplications)
{
    Write-Output "Web Application: $($webApp.name)"
    foreach($siteColl in $webApp.Sites)
    {
        Write-Output "Site Collection: $($siteColl.Url)"
        RecurseSiteAndDoSomething -SiteIdentity $($siteColl.RootWeb)
    }
} 

SharePoint 2007

I am still ironing out a few things with the SharePoint 2007 version, but here is the current script.

 

function RecurseSiteAndDoSomething() {
    param([Microsoft.SharePoint.SPWeb]$SiteIdentity)

    Write-Output "Site: $($SiteIdentity.Url)"
    
    if($SiteIdentity.Webs.Count -gt 0)
    {
        foreach($subWeb in $SiteIdentity.Webs)
        {
            RecurseSiteAndDoSomething -SiteIdentity $subWeb
        }
    }
}

$contentWebAppServices = ([Microsoft.SharePoint.Administration.SPFarm]::Local).services | 
? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"}

foreach($webApp in $contentWebAppServices.WebApplications)
{
    Write-Output "Web Application: $($webApp.name)"
    foreach($siteColl in $webApp.Sites)
    {
        Write-Output "Site Collection: $($siteColl.Url)"
        RecurseSiteAndDoSomething -SiteIdentity $($siteColl.RootWeb)
    }
} 

 

Conclusion

    In this post I presented a template script for recursively traversing each site within the local SharePoint farm.  This is just a first pass at this template.  If I make any updates in the future I will update this post to reflect.

 

      -Frog Out

Speaking at BuckeyeSPUG May 2011 Meeting

Print

This Thursday May 19th, 2011 I will be presenting my “PowerShell for the SharePoint 2010 Developer” session at the Buckeye SharePoint User Group (BuckeyeSPUG).  BuckeyeSPUG is my local SharePoint user group and I always enjoy giving back by helping out with the steering committee, presenting, and volunteering with any other areas that I can.  I recently gave this presentation at SharePoint Saturday Michigan last weekend and the early feedback I heard was good.  I’m looking forward to a good meeting and hope everyone can learn something.  See you there.

 

Session

Where: Buckeye SharePoint User Group (BuckeyeSPUG)

Title: PowerShell for the SharePoint 2010 Developer

Audience and Level: Developer, Intermediate to Advanced

Abstract: PowerShell is not just for SharePoint 2010 administrators. Developers also get access to a wide range of functionality with PowerShell. In this session we will dive into using PowerShell with the .Net framework, web services, and native SharePoint commandlets. We will also cover some of the more intermediate to advanced techniques available within PowerShell that will improve your work efficiency. Not only will you learn how to automate your work but also learn ways to prototype solutions faster. This session is targeted to developers and assumes a basic familiarity with PowerShell.

Slides and Code download: Click here

(Note: I included many hidden slides for some PowerShell basics and intro material. Also included extra scripts for additional areas to look into.)

 

-Frog Out

PowerShell Script To Display All SharePoint Site Collection Administrators In Web Application

In this post I present a script that will display all of the site collection administrators for a given web application.  This script will work for SharePoint 2007 or 2010 as it uses the object model rather than the new SharePoint 2010 commandlets.  Special thanks to Tasha Scott (Twitter) for posting a request for this script.  It took less than 15 minutes to come up with and formalize.

tweet1

Solution

The solution is fairly straight forward.  First you grab a reference to a site collection.  Get the web application from that.  Then loop through all of the site collections within the web application.  For each site collection iterate over the SiteAdministrators property for the RootWeb.  Then write out the site url and admin display names.  The script below is the condensed version, but the version on the Script Repository is a bit fleshed out.

Click here for link to TechNet Script Repository full version

$siteUrl = Read-Host "Enter Site URL"


$rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)

$spWebApp = $rootSite.WebApplication


foreach($site in $spWebApp.Sites)

{

    foreach($siteAdmin in $site.RootWeb.SiteAdministrators)

    {

        Write-Host "$($siteAdmin.ParentWeb.Url) - $($siteAdmin.DisplayName)"

    }

    $site.Dispose()

}

$rootSite.Dispose()

Conclusion

As in past times a friend on Twitter has run into a roadblock, requested help, and I was able to come up with a PowerShell script in a short amount of time to solve the problem.  I really enjoy the SharePoint community and how it can band together when situations like this arise.  Hopefully you’ll use this script and share some of your own in the future.  For now enjoy documenting the site collection admins in your farm.

-Frog Out

Slides, Scripts, and Photos from SharePoint Saturday New Orleans 2011

SharePointSaturdayNola

This weekend I presented “Managing SharePoint 2010 Farms with PowerShell” at SharePoint Saturday New Orleans.  This was my first time visiting New Orleans so I was excited for the experience.  A big thanks to everyone who attended my session.  I condensed the material a little but the slides and scripts below have additional material that we couldn’t cover.  Let me know if you have any comments, questions, or feedback.  Thanks.

Slides and Scripts

Managing SharePoint 2010 Farms with PowerShell

Photos

Pictures on Facebook

click here

Pictures on Windows Live (higher res)

Conclusion

SharePoint Saturday New Orleans 2011 was a great conference overall.  This was my first visit to New Orleans and despite some issues with my flight in on Friday the weekend ended up positive.  I met some great speakers, organizers, sponsors, and attendees as well as meeting old friends.  It was nice to get a sense of the local community with Hubig’s pies, Mardi Gras parades, and Cajun food.  I hope I get a chance to visit the area again in the future.  A big thanks to everyone who made SharePoint Saturday New Orleans 2011 a big success.

 

-Frog Out

Slides, Code, and Photos from SPTechCon San Francisco 2011

    Note: Updated 2/12/11 with links to both presentation materials.

    This past week I presented two sessions at SPTechCon San Francisco 2011.  The first session was “The Expanding Developer Toolbox for SharePoint 2010” which .  Thanks to all of my attendees for this session.  They had so many great questions that we ran out of time before covering all of the planned material.  Especially for them I’ve provided the slides and code samples to walk through them on their own.

The second session was “Real World Deployment of SharePoint 2007 Solutions”.  In talking with attendees before the session many were looking for 2007 content.  At the conference SharePoint 2010 was represented much more heavily than 2007, so I was glad to fill a need in the community.

Slides and Code

Click here for “The Expanding Developer Toolbox for SharePoint 2010” materials

Click here for “Real World Deployment of SharePoint 2007 Solutions” materials

Photos

Pictures on FaceBook

Click here

Pictures on Windows Live (higher res)

Side Trips

Aside from the conference itself I also got to take a few side trips during the nights.  A special thanks to Dux Raymond Sy (Twitter) for organizing a Mongolian Hot Pot dinner on Monday (see pictures) and Michael Noel (Twitter) for organizing a Korean bbq dinner on Tuesday (again see pictures).  These were both new experiences for me and I thoroughly enjoyed the time with friends and trying something new.  Another thanks to Mark Miller (Twitter) for giving a personal tour around various sites of San Fran to myself and a few others.  It was great hearing the backstory of different neighborhoods and buildings from someone who had lived in the area for years.  Overall a great addition to the conference itself.

Conclusion

This is the 3rd SPTechCon I’ve attended and the conference is getting better with each iteration.  The fine folks at BZ Media should be proud of the effort they’ve put in.  The next SPTechCon will be in Boston in June.  As of right now I won’t be attending that one but I highly recommend anyone to go if you have the chance.

-Frog Out