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

23 July, 2014

Brushing up on PowerShell

Over the last couple of days, I have been working with PowerShell on a project. My usage of PowerShell in the past has been mostly for one off tasks or for a tasks that I need to get done really quickly so I can move on to the next. My current project caused me to think about PowerShell as a tool for creating re-usable scripts.

To load additional modules and snap-ins use the commands:
 Import-Module  
and
 Add-PSSnapIn  
.
If you try to load the same snap-in more in the same session an error occurs. One way around is to ignore the error.
 Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue  

Great care must be taken when using variables. A simple typo can lead to a lot of confusion. It is also worthwhile to read up on variable scope.
 help about_scope  

PowerShell allows you to create methods or functions within a script. The basic syntax is
 function  { script block }  

A quick example of creating a function to add two numbers together and calling the function.
 function Add ([int] $x, [int] $y) {   
   $results = $x + $y;  
   Write-Host "Results $results";  
 }  
 Add 3 4  

In the above example, I am doing a few small things that are more personal preference than anything else. There are multiple ways to create parameters. I like to create parameters on the same line as the function name. I also like to add the datatypes. This just gives a me a bit of clue as to what the function would like to have for data. I am also using semi-colons at the end of each line. This comes from my history of working with C, C++, Java, JavaScript, and C#.

You will notice in calling the function, "Add 3 4", parameter are separated by spaces.

Single line comments start with the # sign.
 #A simple one line comment  

Block comments are encased in <# #>
 <#  
 This is a multiple line comment  
 or block comment  
 #>  

Accessing the help system within PowerShell is straight-forward. There are multiple key words that can be used.
 Get-Help  
,
 Help  
, or
 Man  

Calling the help system on the cmdlet Get-Content.
 man Get-Content  

The help system also comes with a set of switches for showing the full listing.
 man Get-Content -full  




20 March, 2014

Listing User Profile Accounts from SharePoint 2013

Today, I am doing a thought experiment / learning exercise. The goal is to come up with a few different ways to display data from SharePoint’s User Profile Service.

Thought A – Manage User Profiles within Central Admin.


From the Manage User Profile page within Central Administration, I can search for a user or a group of user. For example, if SharePoint is setup to use claim based identity then a search phrase “i:0” will give list of users.


While using this method is fast and easy trying to get a list of all the accounts or to create a file with all the accounts in it is difficult.

Thought B - PowerShell



With a simple PowerShell script, it is easy to get a list of all the user profiles. What is really nice about this method is it is very simple to pipe the results of the GetEnumerator method to filter the results. PowerShell has built-in functionality that makes creating a file with the account information in it a snap.


add-pssnapin "Microsoft.Sharepoint.Powershell"

$siteList = Get-SPSite 
$serviceContext = Get-SPServiceContext($siteList[0])
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)

foreach($usrProfile in $profileManager.GetEnumerator()) {
    Write-Host $usrProfile.AccountName "|" $usrProfile.DisplayName
}

Thought C – Visual Studio


Referencing the correct libraries and little bit of code, it very simple to get back a list of all the accounts within User Profile. With a little bit of extra code searching the accounts, searching for a specific account or writing the accounts to a file is no problem.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace UserProfileList
{
    class Program
    {
        static void Main(string[] args)
        {
            var siteList = new SPSite("http://sp2013");
            SPServiceContext svcCtx = SPServiceContext.GetContext(siteList);
            var profileMgt = new UserProfileManager(svcCtx);
            var userProfiles = profileMgt.GetEnumerator();

            while (userProfiles.MoveNext())
            {
                UserProfile userProfile = (UserProfile)userProfiles.Current;
                
                Console.WriteLine(userProfile.AccountName);
            }

            Console.ReadLine();
     }
    }
}


Wrap Up

This was a fun experiment in figuring out different ways to do the same task. It showed me a bit about the effort it takes to get a result. Using the out of the box functionality got me to a result very fast and it should cover 80% of the needs around looking up a user or a small group of users. PowerShell makes it simple and easy to get a list of users. 



Challenging myself to learn something new

I have recently set a big challenge for myself. I want to know about Machine Learning . To add to the challenge, I am trying out usin...