Wednesday, March 23, 2016

Getting the current script directory in PowerShell

Getting the current script directory in PowerShell

One thing to remember when developing PowerShell scripts is that your current value in $PWD is the process’ current directory not the one where the script was stored.
In my desire to break out a few functions into a common file in order to load them “dot-sourced” (so that the functions become global) I realised I needed help!
In comes a new function from the PowerShell Team Blog. It’s going straight into my PowerShell Profile: Get-ScriptDirectory. It does exactly as the name says, it gets the current directory that script is executed from.
  1. function Get-ScriptDirectory
  2. {
  3. $Invocation = (Get-Variable MyInvocation -Scope 1).Value
  4. Split-Path $Invocation.MyCommand.Path
  5. }
So that now from my script that requires the Common-Functions.ps1 (located in the same directory) I can call:
  1. # Load up our common functions
  2. $commons = Join-Path (Get-ScriptDirectory) Common-Functions.ps1;
  3. $commons;
Hope this help,
Oumaima Achkari

Thursday, March 17, 2016

PowerShell Snippet: Import Data from Multiple CSV Files into a PowerShell Array

PowerShell Snippet: Import Data from Multiple CSV Files into a PowerShell Array

Here’s one way to get data from multiple CSV files in a directory structure into a PowerShell array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$DataFiles = Get-ChildItem $CSVSourceDir -recurse -force | Where { $_.Name -like "*.csv" } | Foreach-Object -process { $_.FullName }
[int] $DataFilesCount = $DataFiles.Count
Write-Output "Discovered $DataFilesCount CSV Data files in $CSVSourceDir "

ForEach ($DataFilesItem in $DataFiles)
    {
        $FileInfo = Get-Item $DataFilesItem
        $LogDate = $FileInfo.LastWriteTime
        Write-Output "Reading data from $DataFilesItem ($LogDate ) "
        [array]$CSVData += Import-CSV $DataFilesItem -Header UserID,FirstName,LastName,DisplayName
        ## Only use the header if you want to rename the attributes for the imported objects. If the column headers in the CSV are fine, don't use -header
      }
[int] $CSVDataCount = $CSVData.Count 
Write-Output "Imported $CSVDataCount records"
Now the data in multiple CSV files is in the array $CSVData.
Note that this only works if each CSV file has the same column structure (ex. UserID,FirstName,LastName,DisplayName ).
Also make sure you always use [int] for count variables, otherwise PowerShell assumes it’s a string value and will leave it blank if there is no value. If you declare the count variable as [int] and it’s blank, PowerShell sets it to 0.
Example: [int]$CSVDataCount = $CSVData.Count

Hope this help,
Oumaima

Powershell: Reference a property that contains a space

Powershell: Reference a property that contains a space


I am importing a CSV file using powershell.
The problem is, the title of one of the columns is "Remaining Time - Hours". So I'm getting a sequence of objects, and it's actually assigned the property "Remaining Time - Hours".
but if I type
$case.Remaining Time - Hours
I get "Unexpected token 'Time' in expression or statement"

Solution:
Properties with embedded spaces have to be quoted when they're referenced:
 $case."Remaining Time - Hours"
Hope this help,
Oumaima

Tuesday, March 15, 2016

Add users to SharePoint Group using PowerShell

Add users to SharePoint Group using PowerShell

I wanted to add bulk of users to a SharePoint group in a site collection.
I wrote the following PowerShell script to read a file with list of users and add them in a loop to a SharePoint group using Set-SPUser. There isn’t much description or documentation available on MSDN for this cmdlet, but it basically adds a user to a SharePoint site within a certain group.

$loc = Get-Location  
$Users = Get-Content "$loc\Data\Users.txt" 

foreach ($User in $Users) {  
 Set-SPUser -Identity $User -Web $siteCollUrl -Group $group   
}  
but here comes the surprise. Using Set-SPUser, I was not able to add all the users to the group and it gave the following error on most of the users.
You must specify a valid user object or user identity
t turns out to be that Set-SPUser can only set properties, permissions to a user account if it has already been added to SharePoint site. This means, you have to make sure that the user is already present in the SharePoint site. Otherwise, you can use the New-SPUSercmdlet to add the user. 
It just gets better. And I should be asked to bake a cake for anyone who is reading this blog as it was only while I writing this post I realized that it was even simpler and straight forward by using the EnsureUser.
SPWeb.EnsureUser checks whether the specified logon name belongs to a valid user of the website, and if the logon name does not already exist, adds it to the website.
In fact, EnsureUser does the same thing which I implemented myself in Solution 1 by using Get and New-SPUser.

$loc = Get-Location  
$Users = Get-Content "$loc\Data\Users.txt" 

$web = Get-SPWeb -identity $siteCollUrl
foreach ($User in $Users) { 
    $web.EnsureUser($User) 
    Set-SPUser -Identity $User -Web $siteCollUrl -Group $group  
} 
Hope this help,
oumaima


Monday, March 7, 2016

Get SPUser from person or group column

Get SPUser from person or group column

The following code sample shows how simple it an be to retrieve a SPUser object from a Person or Group column in SharePoint 2010.
using (SPSite oSite = new SPSite(“url”))
{
        using (SPWeb oWeb = oSite.OpenWeb())
        {
  SPList oList = oWeb.Lists[“List Name”];
  SPListItem oItem = oList.GetItemById(0);
  SPFieldUserValue userValue = new SPFieldUserValue(oWeb, oItem[“Person or Group       Column Name”].ToString());
 SPUser userVal = userValue.User;
       }
 }
Hope this helps.

Sharepoint Designer 2013, XSLT List View Options ribbon option is not showing

Sharepoint Designer 2013, XSLT List View Options ribbon option is not showing I have an ordinary Wiki Page, also tried making an Article...