Skip to content

Commit

Permalink
Added new scripts for SharePoint and Office 365
Browse files Browse the repository at this point in the history
Added new scripts for SharePoint and Office 365
  • Loading branch information
Juan Carlos González committed Feb 6, 2015
1 parent 9ca0000 commit 6cfdf66
Show file tree
Hide file tree
Showing 99 changed files with 3,914 additions and 1 deletion.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Office 365/Microsoft.SharePoint.Client.dll
Binary file not shown.
64 changes: 64 additions & 0 deletions Office 365/PS_Activate_Sandbox_Solution.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
############################################################################################################################################
#Script that allows to activate a Sandbox solution in SharePoint Online
# Required Parameters:
# -> $sUserName: User Name to connect to the SharePoint Online Site Collection.
# -> $sPassword: Password for the user.
# -> $sSiteColUrl: SharePoint Online Site Collection.
# -> $sSiteCollectionRelativePath: Site Collection Relative Path.
# -> $sSolutionName: Sandbox solution name.
# -> $sSolutionFile: Sandbox solution file.
############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that allows to activate a Sandbox solution in SharePoint Online
function Activate-SandboxSolution
{
param ($sSiteColUrl,$sUserName,$sPassword,$sSiteCollectionRelativePath,$sSolutionName,$sSolutionFile)
try
{
#Adding the Client OM Assemblies
Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"
Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Publishing.dll"

#SPO Client Object Model Context
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)
$spoCtx.Credentials = $spoCredentials

$spoDesignPackageInfo=New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo
$spoDesignPackageInfo.PackageGuid=[GUID]::Empty
$spoDesignPackageInfo.MajorVersion=1
$spoDesignPackageInfo.MinorVersion=1
$spoDesignPackageInfo.PackageName=$sSolutionName
$sSolutionRelativePath=$sSiteCollectionRelativePath + "_catalogs/solutions/" + $sSolutionFile
$sSolutionRelativePath
#[Microsoft.SharePoint.Client.Publishing.DesignPackage] | Get-Member -Static
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
Write-Host "Activating the Sandbox Solution in the Solution Gallery!!" -foregroundcolor Green
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
$spoDesignPackage=[Microsoft.SharePoint.Client.Publishing.DesignPackage]
$spoDesignPackage::Install($spoCtx,$spoCtx.Site,$spoDesignPackageInfo,$sSolutionRelativePath)
$spoCtx.ExecuteQuery()
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
Write-Host "Sandbox Solution successfully activated in the Solution Gallery!!" -foregroundcolor Green
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
$spoCtx.Dispose()
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}

#Required Parameters
$sSiteColUrl = "https://<Your_SPO_Site_Collection>"
$sUserName = "<Your_SPO_User>"
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
$sPassword=convertto-securestring "<Your_SPO_Pasword>" -asplaintext -force
$sSolutionName="<Sandbox_Solution_Name>"
$sSolutionFile="<Sandbox_Solution_File>"
$sSiteCollectionRelativePath="/sites/<SiteCollectionName>/"

Activate-SandboxSolution -sSiteColUrl $sSiteColUrl -sUserName $sUserName -sPassword $sPassword -sSiteCollectionRelativePath $sSiteCollectionRelativePath -sSolutionName $sSolutionName -sSolutionFile $sSolutionFile
122 changes: 122 additions & 0 deletions Office 365/PS_AddOffice365UsersAndLicenses.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
############################################################################################################################################
# Script that allows to do a add users to Office 365 in bulk. The users are read from a CSV file.
# The csv file only needs a column that stores the account principal name for each user to be added to Office 365
# Required Parameters:
# -> $sUserName: User Name to connect to the SharePoint Admin Center.
# -> $sMessage: Message to show in the user credentials prompt.
# -> $sInputFile: Message to show in the user credentials prompt.
############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that allows to add to Office 365 the list of users contained in the CSV file.
function Add-Office365Users
{
param ($sInputFile)
try
{
# Reading the Users CSV file
$bFileExists = (Test-Path $sInputFile -PathType Leaf)
if ($bFileExists) {
"Loading $sInputFile for processing..."
$tblUsers = Import-CSV $sInputFile
} else {
Write-Host "$sInputFile file not found. Stopping the import process!" -foregroundcolor Red
exit
}

# Deleting the users
Write-Host "Adding the Office 365 users ..." -foregroundcolor Green
foreach ($user in $tblUsers)
{
"Adding user " + $user.UserPrincipalName.ToString()
New-MsolUser -UserPrincipalName $user.UserPrincipalName -DisplayName $user.UserDisplayName
}

Write-Host "-----------------------------------------------------------" -foregroundcolor Green
Write-Host "All the users have been added. The processs is completed." -foregroundcolor Green
Write-Host "-----------------------------------------------------------" -foregroundcolor Green
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}

#Definition of the function that allows to assign Office 365 licenses to the specific users read from a CSV file.
function Add-Office365LicensesToUsers
{
param ($sInputFile,$sOperationType)
try
{
# Reading the Users CSV file
$bFileExists = (Test-Path $sInputFile -PathType Leaf)
if ($bFileExists) {
"Loading $sInputFile for processing..."
$tblUsers = Import-CSV $sInputFile
} else {
Write-Host "$sInputFile file not found. Stopping the import process!" -foregroundcolor Red
exit
}

# Deleting the users
$msolAccountSKU=Get-MsolAccountSku
Write-Host "Adding the Office 365 licenses ..." -foregroundcolor Green
foreach ($user in $tblUsers)
{
Write-Host "--------------------------------------------------------"
Write-Host "Adding license $msolAccountSKU.AccountSkuId to the user " $user.UserPrincipalName.ToString()
Write-Host "--------------------------------------------------------"
#Setting the location for the user
Set-MsolUser -UserPrincipalName $user.UserPrincipalName -UsageLocation "ES"
switch ($sOperationType)
{
"Remove" {
#Remove complete SKU
Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicenses $msolAccountSKU.AccountSkuId
}
"Add" {
#Add complete SKU
Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -AddLicenses $msolAccountSKU.AccountSkuId
}
"CustomAdd" {
#Custom license assignment
$msolLicenseOptions = New-MsolLicenseOptions -AccountSkuId $msolAccountSKU.AccountSkuId -DisabledPlans $user.ServicePlan
Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -LicenseOptions $msolLicenseOptions
}
default {
Write-Host "Requested Operation not valid!!" -ForegroundColor Green
}
}
#Reading the licenses available for the user
(Get-MsolUser -UserPrincipalName $user.UserPrincipalName).Licenses.ServiceStatus

}

Write-Host "-----------------------------------------------------------" -foregroundcolor Green
Write-Host "All the licenses have been assigned. The processs is completed." -foregroundcolor Green
Write-Host "-----------------------------------------------------------" -foregroundcolor Green
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}

#Connection to Office 365
$sUserName="<Your_Office365_Admin_Account>"
$sMessage="Introduce your Office 365 Credentials"
#Connection to Office 365
$msolcred = get-credential -UserName $sUserName -Message $sMessage
connect-msolservice -credential $msolcred

$ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
$sInputFile=$ScriptDir+ "\PS_UsersToAddOffice365.csv"

#Adding Users
Add-Office365Users -sInputFile $sInputFile

#Adding Licenses to each user
Add-Office365LicensesToUsers -sInputFile $sInputFile -sOperationType "Remove"
Add-Office365LicensesToUsers -sInputFile $sInputFile -sOperationType "Add"
Add-Office365LicensesToUsers -sInputFile $sInputFile -sOperationType "CustomAdd"
56 changes: 56 additions & 0 deletions Office 365/PS_Add_Bing_Maps_Key_SPO.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
############################################################################################################################################
#Script that adds a Bing Maps Key to a SharePoint Online Site Collection
# Required Parameters:
# -> $sUserName: User Name to connect to the SharePoint Online Site Collection.
# -> $sPassword: Password for the user.
# -> $sSiteUrl: SharePoint Online Site Url.
# -> $sBingMapsKey: Bing Maps Key to be added.
############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that allows to add a Bing Maps Key to a SharePoint Onlione Site Collection
function Add-BingMapsKey
{
param ($sSiteUrl,$sUserName,$sPassword,$sBingMapsKey)
try
{
#Adding the Client OM Assemblies
Add-Type -Path "H:\03 Docs\10 MVP\03 MVP Work\11 PS Scripts\Office 365\Microsoft.SharePoint.Client.dll"
Add-Type -Path "H:\03 Docs\10 MVP\03 MVP Work\11 PS Scripts\Office 365\Microsoft.SharePoint.Client.Runtime.dll"

#SPO Client Object Model Context
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)
$spoCtx.Credentials = $spoCredentials

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
Write-Host "Adding Bing Maps Key to $sSiteUrl !!" -ForegroundColor Green
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green

$spoSiteCollection=$spoCtx.Site
$spoCtx.Load($spoSiteCollection)
$spoRootWeb=$spoSiteCollection.RootWeb
$spoCtx.Load($spoRootWeb)
$spoAllSiteProperties=$spoRootWeb.AllProperties
$spoCtx.Load($spoAllSiteProperties)
$spoRootWeb.AllProperties["BING_MAPS_KEY"]=$sBingMapsKey
$spoRootWeb.Update()
$spoCtx.ExecuteQuery()
$spoCtx.Dispose()
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}

#Required Parameters
$sSiteUrl = "https://<SPO_Site_Url>"
$sUserName = "<SPOUser>@<SPODomain>.onmicrosoft.com"
$sBingMapsKey= "<BingMaps_Key>"
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
$sPassword=convertto-securestring "<SPO_Password>" -asplaintext -force

Add-BingMapsKey -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sBingMapsKey $sBingMapsKey

52 changes: 52 additions & 0 deletions Office 365/PS_Add_GeolocalizationField_SPO.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
############################################################################################################################################
#Script that adds a geolocalization field to a list in a SharePoint Online site
# Required Parameters:
# -> $sUserName: User Name to connect to the SharePoint Online Site Collection.
# -> $sPassword: Password for the user.
# -> $sSiteUrl: SharePoint Online Site Url
# -> $sListName: Name of the list where the geolocalization field is going to be added.
############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that allows to add a geolocalization field to a list in SharePoint Online
function Add-GeolocalizationFieldSPO
{
param ($sSiteUrl,$sUserName,$sPassword,$sListName)
try
{
#Adding the Client OM Assemblies
Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"
Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

#SPO Client Object Model Context
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)
$spoCtx.Credentials = $spoCredentials

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
Write-Host "Adding a gelocalization field to $sListName !!" -ForegroundColor Green
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green

$spoList=$spoCtx.Web.Lists.GetByTitle($sListName)
$FieldOptions=[Microsoft.SharePoint.Client.AddFieldOptions]::AddToAllContentTypes
$spoList.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='Location'/>",$true,$FieldOptions)
$spoList.Update()
$spoCtx.ExecuteQuery()
$spoCtx.Dispose()
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}

#Required Parameters
$sSiteUrl = "https://<SPO_Site_Url>"
$sUserName = "<SPOUser>@<SPODomain>.onmicrosoft.com"
$sListName= "<ListName>"
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
$sPassword=convertto-securestring "<SPOPassword>" -asplaintext -force

Add-GeolocalizationFieldSPO -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sListName $sListName

64 changes: 64 additions & 0 deletions Office 365/PS_Add_SPO_Users_To_Group_CSOM.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
############################################################################################################################################
#Script that allows to add Users to a SPO Group
# Required Parameters:
# -> $sUserName: User Name to connect to the SharePoint Online Site Collection.
# -> $sPassword: Password for the user.
# -> $sSiteColUrl: SharePoint Online Site
# -> $sGroup: SPO Group where users are going to be added
# -> $sUserToAdd: User to be added
############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that allows to create a SharePoint Group in a SharePoint Online Site
function Add-SPOUsersToGroup
{
param ($sSiteColUrl,$sUsername,$sPassword,$sGroup,$sUserToAdd)
try
{
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
Write-Host "Adding User $sUser to group $sGroup $sSiteColUrl" -foregroundcolor Green
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green

#Adding the Client OM Assemblies
Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"
Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

#SPO Client Object Model Context
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)
$spoCtx.Credentials = $spoCredentials

#Getting the SharePoint Groups for the site
$spoGroups=$spoCtx.Web.SiteGroups
$spoCtx.Load($spoGroups)
#Getting the specific SharePoint Group where we want to add the user
$spoGroup=$spoGroups.GetByName($sGroup);
$spoCtx.Load($spoGroup)
#Ensuring the user we want to add exists
$spoUser = $spoCtx.Web.EnsureUser($sUserToAdd)
$spoCtx.Load($spoUser)
$spoUserToAdd=$spoGroup.Users.AddUser($spoUser)
$spoCtx.Load($spoUserToAdd)
$spoCtx.ExecuteQuery()

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
Write-Host "SharePoint User $sUser added succesfully!!" -foregroundcolor Green
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
$spoCtx.Dispose()
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}

#Required Parameters
$sSiteColUrl = "https://<Office365_Site>"
$sUsername = "<Office365_UserAccount>"
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
$sPassword=convertto-securestring "<Office365_Password>" -asplaintext -force
$sGroup="<SPO_Group>"
$sUserToAdd="i:0#.f|membership|<Office365_UserAccount>"

Add-SPOUsersToGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroup $sGroup -sUserToAdd $sUserToAdd
Loading

0 comments on commit 6cfdf66

Please sign in to comment.