In case you need to update a bunch of sites that look the same. in my case I have to update on a regular base about 120 sites.
Connect-SPOService –Url 'YOUR ADMIN SITE FULL URL"
# Get all sites
Get-SPOHubSite
# so you get the the hub id you need if not just remove the loop on next section
$hubSiteId= "ID HERE"
$sites = Get-SPOSite -Limit ALL
foreach ($site in $sites)
{
$siteDetailed = Get-SPOSite -Detailed $site.Url
if($siteDetailed.HubSiteId -eq $hubSiteId)
{
$siteUrl = $site.url
$siteUrl
$username = "USER HERE"
$password = "PASS SITE"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, (ConvertTo-SecureString $Password -AsPlainText -Force))
$ctx.Credentials = $credentials
$web = $ctx.Web
$ql = $web.Navigation.QuickLaunch
$ctx.load($ql)
$ctx.ExecuteQuery()
$node = $web.Navigation.QuickLaunch | where { $_.Title -eq "TITLE HERE" }
$node.DeleteObject()
$ctx.ExecuteQuery()
}
}
What you will find here
If you are one of those multi hat kind of person you might want to take a look here.
Some of the sections here will cover: SharePoint, SQL, Server, VB Script, Batch files, PCI Compliance
Some of the sections here will cover: SharePoint, SQL, Server, VB Script, Batch files, PCI Compliance
Icon
Showing posts with label SharePoint Online. Show all posts
Showing posts with label SharePoint Online. Show all posts
Wednesday, March 6, 2019
Add user to security group - SharePoint Online - Power Shell - Loop
To add a user to all sub sites on your hub site here the WORKING script using power shell and
Connect-SPOService –Url "YOUR ADMIN SITE HERE"
# Get all sites
Get-SPOHubSite
#SO YOU CAN GET THE ID YOU NEED
# add users to a security group
$hubSiteId= "HUB ID HERE"
$sites = Get-SPOSite -Limit ALL
foreach ($site in $sites)
{
$siteDetailed = Get-SPOSite -Detailed $site.Url
if($siteDetailed.HubSiteId -eq $hubSiteId)
{
$site.Url
$x = Get-SPOSiteGroup -Site $site.url
foreach ($y in $x)
{
if($y.Title -like "*Owners*") #OR GROUP YOU NEED
{
$group = Get-SPOSiteGroup -Site $site.URL -Group $y.Title
$group.title
Add-SPOUser -Site $site.url -LoginName "FULL EMAIL OF USER" -Group $group.title
}
}
}
}
Connect-SPOService –Url "YOUR ADMIN SITE HERE"
# Get all sites
Get-SPOHubSite
#SO YOU CAN GET THE ID YOU NEED
# add users to a security group
$hubSiteId= "HUB ID HERE"
$sites = Get-SPOSite -Limit ALL
foreach ($site in $sites)
{
$siteDetailed = Get-SPOSite -Detailed $site.Url
if($siteDetailed.HubSiteId -eq $hubSiteId)
{
$site.Url
$x = Get-SPOSiteGroup -Site $site.url
foreach ($y in $x)
{
if($y.Title -like "*Owners*") #OR GROUP YOU NEED
{
$group = Get-SPOSiteGroup -Site $site.URL -Group $y.Title
$group.title
Add-SPOUser -Site $site.url -LoginName "FULL EMAIL OF USER" -Group $group.title
}
}
}
}
Uncheck or Unlock Document or Page SharePoint Online - power shell
after trying A LOT to discard changed on a page by a user, I finally got it.
This is actually working like a charm for pages and documents.
Connect-SPOService –Url "YOUR ADMIN URL"
{
$siteUrl = "YOUR SITE URL"
$siteUrl
$username = "USER HERE"
$password = "PASS HERE"
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName,(ConvertTo-SecureString $Password -AsPlainText -Force))
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$Ctx.Credentials = $SPOCredentials
$web = $Ctx.Web
$ctx.ExecuteQuery()
# Load the up list
$lookupList = $ctx.Web.Lists.GetByTitle("YOUR LIBRARY")
$ctx.Load($lookupList)
# Prepare the query
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = '<View Scope="RecursiveAll"><RowLimit Paged="TRUE">1000</RowLimit></View>'
$listItems = $lookupList.getItems($query)
$ctx.Load($listItems)
$ctx.ExecuteQuery()
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
#Load File
$file = $ctx.Web.GetFileByServerRelativeUrl("RELATIVE URL OF PAGE HERE");
#Relative URL of document is : sample.. /sites/mysite/SitePages/Home.aspx
$ctx.Load($file)
$Ctx.ExecuteQuery()
$file.UndoCheckOut()
$file.Update()
$Ctx.ExecuteQuery()
}
I hope this helps. !!!
This is actually working like a charm for pages and documents.
Connect-SPOService –Url "YOUR ADMIN URL"
{
$siteUrl = "YOUR SITE URL"
$siteUrl
$username = "USER HERE"
$password = "PASS HERE"
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName,(ConvertTo-SecureString $Password -AsPlainText -Force))
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$Ctx.Credentials = $SPOCredentials
$web = $Ctx.Web
$ctx.ExecuteQuery()
# Load the up list
$lookupList = $ctx.Web.Lists.GetByTitle("YOUR LIBRARY")
$ctx.Load($lookupList)
# Prepare the query
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = '<View Scope="RecursiveAll"><RowLimit Paged="TRUE">1000</RowLimit></View>'
$listItems = $lookupList.getItems($query)
$ctx.Load($listItems)
$ctx.ExecuteQuery()
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
#Load File
$file = $ctx.Web.GetFileByServerRelativeUrl("RELATIVE URL OF PAGE HERE");
#Relative URL of document is : sample.. /sites/mysite/SitePages/Home.aspx
$ctx.Load($file)
$Ctx.ExecuteQuery()
$file.UndoCheckOut()
$file.Update()
$Ctx.ExecuteQuery()
}
I hope this helps. !!!
Subscribe to:
Comments (Atom)