Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Tuesday, August 23, 2016

Set SSRS Report Data Source Programmatically

Set SSRS Report Data Source Programmatically


 This script is designed for SSRS 2008 R2 and 2012 in SharePoint 2010/2013 Integrated mode, it will require tweaking for native mode or previous versions. 

This script will accept a SharePoint Document Library url and set the DataSource url for all the reports in that library.


#------------------------------------------------------------------------------------------- 
# Name:            Set-SSRSDataSourceInfo 
# Description:     This script will set the data source url for all reports in a doclib 
# Usage:        Run the function with the DocLibUrl,DataSourceName and DataSourceURL parameters 
# By:             Ivan Josipovic, softlanding.ca 
#------------------------------------------------------------------------------------------- 
Function Set-SSRSDataSourceInfo ($DocLibUrl,$DataSourceName,$DataSourceURL){ 
$DocLibUrl = $DocLibUrl.Replace("%20"," ").tolower(); 
if ($docliburl.EndsWith("/")){  
    $docliburl = $docliburl.Substring(0,$docliburl.length-1); 
} 
$webfound = $false$weburl = $DocLibUrl; 
 
while ($webfound -eq $false) { 
    if ($weburl.Contains("/")){ 
        $weburl = $weburl.Substring(0,$weburl.LastIndexOf("/")); 
        $web = get-spweb -identity $weburl -ea 0; 
        if ($web -ne $null){ 
            $webfound = $true; 
        } 
    }else{ 
        Write-Output -ForegroundColor Red "The Web could not be found"; 
        return -1; 
    } 
} 
$doclib = $web.lists |where {$_.RootFolder.Name.tolower() -eq $docliburl.Substring($docliburl.LastIndexOf("/")+1,$docliburl.length-$docliburl.LastIndexOf("/")-1)}; 
if($doclib -eq $null){ 
Write-Output -ForegroundColor Red "The DocLib could not be found"return; 
} 
$reports = $doclib.items | where {$_.File.Name.tolower().Contains(".rdl")} 
 
$SSRSurl = "$($web.url)/_vti_bin/ReportServer/ReportService2010.asmx"$SSRS = New-WebServiceProxy -uri $SSRSurl -UseDefaultCredential; 
foreach ($report in $reports){ 
    write-output "$($web.url)/$($report.url)"; 
    $dataSources = $ssrs.GetItemDataSources("$($web.url)/$($report.url)"); 
    if ($datasources.count -gt 0){ 
        for ($i = 0; $i -lt $dataSources.count; $i++) { 
            if ($DataSources[$i].name -eq $DataSourceName){ 
                write-output "Updating $($DataSources[$i].name)"; 
                $proxyNamespace = $DataSources[$i].GetType().Namespace; 
                $DataSources[$i].Item = New-Object ("$proxyNamespace.DataSourceReference"); 
                $DataSources[$i].Item.Reference = $DataSourceURL; 
                $SSRS.SetItemDataSources("$($web.url)/$($report.url)"$DataSources[$i]) 
                write-output "Done"; 
            } 
        } 
    } 
    write-output ""; 
 
} 
} 
 
Set-SSRSDataSourceInfo -DocLibUrl "http://test.com/site/reports/Documents" -DataSourceName "BIRepository" -DataSourceURL "http://test.com/site/reports/Data Sources/BIRepository.rsds" 
Set-SSRSDataSourceInfo -DocLibUrl "http://test.com/site/reports/Documents" -DataSourceName "BIOlapStore" -DataSourceURL "http://test.com/site/reports/Data Sources/BIOlapStore.rsds"
That it!

 Hope this help,
Oumaima

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


Wednesday, September 10, 2014

Setting up Master Page using PowerShell in SharePoint 2013

Setting up Master Page using PowerShell in SharePoint 2013


In this blog, we will see how to set up a master page for a sharepoint Site using PowerShell command.
 
$web = Get-SPWeb http://mysite$web.CustomMasterUrl = "/_catalogs/masterpage/Mycustom.master"
$web.MasterUrl = "/_catalogs/masterpage/Mycustom.master"
$web.Update()
  •  CustomMasterURL is siteMaster Page
  • MasterURL is Master Page 

Monday, July 21, 2014

Powershell function to send Email

Sending Email using Powershell 


Below is a script that can be used to send email using Powershell.


 $sd = new-object System.collections.specialized.stringdictionary
$sd.add("to","oumaima@example.com")
$sd.add("from","achkari@example.com")
$sd.add("Subject","Mail Sharepoint")
$web = get-spweb "http://Server_Name"
$body = "This is the email body"

[Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,$sd,$body)

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...