Some of you may have noticed my recent posts on some questions in the forum. Here’s another one… 🙂
I’m trying to create some deployment scripts using PowerShell. I’m able to connect to my tenant but am not able to do much with it afterwards. Any thoughts?
Add-Type -Path “C:\dll\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\dll\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\dll\Microsoft.SharePoint.Client.Taxonomy.dll”
$tenantAdmin = “https://tenant-admin.sharepoint.com”
$tenantRoot = “https://tenant.sharepoint.com”
$adminUser = “haniel@tenant.onmicrosoft.com”
$password = “password”
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adminUser, $securePassword)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($tenantRoot)
$ctx.Credentials = $credentials
$webs = $ctx.Web
$ctx.Web or other objects I’m trying to access come back as null. Any thoughts?
Hi David,
Thanks for the samples. Those are great. However, I’m running into an issue right at the start. Here’s what I get when I try to add the Microsoft.SharePoint.Powershell snapin:
Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.
I tried running it as administrator from ISE and from the SharePoint Online Management Shell.
Thanks,
-Haniel
Hi Ryan,
I tried your SPPS project first but am having issues running it (errors on Initialize amongst others errors). I’m running on Windows 10 (PowerShell v5.0).
I’ll try again and let you know if I make any progress.
-Haniel
Here is one that I am using to collect all active users and populate a list on an on premise sharepoint environment for reporting purposes…
$PSSnapinSP = Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
if ($PSSnapinSP -eq $Null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
}
#Specify tenant admin and URL
$User = “<UserName>”
#Configure Site URL and User
$SiteURL = “<SiteURL>”
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll”
$Password = Read-Host -Prompt “Please enter your password” -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
Write-Host Logged into as $Creds…
#Bind to Site Collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Context.Credentials = $Creds
#Identify users in the Site Collection
$Users = $Context.Web.SiteUsers
$Context.Load($Users)
$Context.ExecuteQuery()
#Open SharePoint List
$sourceWebURL = “<SiteURLForList”
$sourceListName = “ActiveUserReport”
$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
#Delete all the items in the SharePoint list
Write-Host Deleting SharePoint List items…
$items = $spSourceList.items
foreach ($item in $items)
{
$spSourceList.getitembyid($Item.id).Delete()
}
Write-Host Adding SharePoint List items…
#Create People Manager object to retrieve profile data
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
Foreach ($User in $Users)
{
$UserProfile = $PeopleManager.GetPropertiesFor($User.LoginName)
$Context.Load($UserProfile)
$Context.ExecuteQuery()
If($spSourceList -ne $null)
{
If ($UserProfile.Email -ne $null)
{
# Write-Host “User:” $User.LoginName -ForegroundColor Green
# $UserProfile.UserProfileProperties
# Write-Host “”
$UPP = $UserProfile.UserProfileProperties
#Parse and populate the SharePoint list (ActiveUserReport)
$spItem = $spSourceList.AddItem()
$spItem[“UserName”] = $UserProfile.DisplayName
$spItem[“Email”] = $UserProfile.Email
$spItem[“OneDriveUrl”] = $UserProfile.PersonalUrl
$spItem[“Phone”] = $UPP.WorkPhone
$spItem[“JobTitle”] = $UPP.’SPS-JobTitle’
$spItem[“Department”] = $UPP.Department
$spItem[“Office”] = $UPP.Office
$spItem.Update()
}
}
}
Have you looked at SPPS – as there area alot of the basics already built there not just for online but also On-Prem too
Just as a disclaimer – I’m the Dev lead for SPPS so feel free to ask me any questions directly on using them
http://sharepointpowershell.codeplex.com/
Ryan
