How can I fetch the sites or subsites from a site collection and export them to excel document? - powershell

I've been trying to fetch the subsites from a site collection and export them into excel document, and I've been checking different scripts and testing to get a special one which could help me to get this information, even though none of them have work.
I've tested these two:
https://sposcripts.com/get-all-sites-and-subsites/
https://sharepoint.stackexchange.com/questions/300136/how-can-i-get-all-site-collections-and-sub-sites-inside-a-powershell-script
enter image description here

You may need to -UseWebLogin to sign in the MFA enabled account. Try using this:
Connect-PnPOnline -Url https://contoso.sharepoint.com -UseWebLogin
OR
Connect-PnPOnline -Url https://contoso.sharepoint.com -PnPManagementShell -LaunchBrowser

Related

How get users permissions report to all site collections on SharePoint Online using PowerShell

I am looking for a solution on how I could generate a report with users permissions to all site collections on SharePoint Online using PowerShell.
Using the script below, I get all data about the sites. Is it possible to retrieve only the url value of each site as a response from this script?
Connect-SPOService -url "https://...-admin.sharepoint.com" -Credential (Get-credential)
Get-SPOSite -Limit ALL | Export-CSV -LiteralPath C:\Temp\SitesInventory.csv -NoTypeInformation
Below is a fragment of the second script generating the report with users permissions to the specified site collection
...
$SiteURL = "..."
$ReportFile = "C:\Temp\UsersPermissionsReport.csv"
GeneratePnPSitePermissionsReport -SiteURL $SiteURL -ReportFile $ReportFile -Recursive
Using the second script generates a report with users permissions to the specified site collection but I would need to iterate on all sites returned from the first script and generate a report with users permissions for each of them
this link is valid for a single site collection:
link
You can try to loop this in all site collections, like this below:
foreach ($SiteCollection in SPOSite) {do your stuff here}

SharePoint Online : manage folder permission with PowerShell

I'm trying to write a Powershell script to grant user permission to a sub-folder in our SharePoint online site. I tried the solutions from the similar questions already open but without success.
I've got an online SharePoint site : https://xxx.sharepoint.com/sites/MySharePoint
In this SharePoint site I've got a library called Test which contains 2 sub-folders Test1 and Test2
I want the user "adtest#x.com" to be granted access to Test1 only in read/write (i.e. contributor).
I used the following code :
Connect-PnPOnline -Url $SiteURL -Credentials $psCred
$folder = Get-PnPFolder -URL "https://xxx.sharepoint.com/sites/MySharePoint/Test/Test1"
Set-PnPfolderPermission -list "Test" -identity $folder -User "adtest#x.com" -AddRole "Contribute"
When I run these commands, on the last step I get an error which translates as : "Impossible to find the level of authorization". My system is in French so I had to translate the message.
I tried the commands found from the other questions on the site but none worked.
Has anybody any idea on the issue ?
Thanks !
Fred
In the pnp documention the identity is always the server relative path.
Try that, just use as identity:
"/Sites/MySharePoint/Test/Test1"
try set the exact name of the permission role. Check it in the advanced permission level.
System need the name in french language resp. in the language of site collection, not english. Example in slovak language which i use

Sharepoint Check-In Document using Powershell

Does anyone know why the Set-PnPFileCheckedIn cmdlet below does nothing when executed? The file doesn't get checked in. I'm logging in with the same user that has the file checked out.
I'm not even seeing an error message. The script just runs through.
$SiteUrl = "https://orgsite.com/sites/TestSite"
$fileRelativePath = "/sites/TestSite/Documents/File.pdf"
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
Set-PnPFileCheckedIn -Url $fileRelativePath
Per my test, this works for me.
Please make sure the relative url of the file is right, I assume the fileRelativePath shuold be "/sites/TestSite/Shared Documents/File.pdf".

Remove SharePoint Site with Remove-PnPTenantSite in PowerShell not working

I want to Remove a SharePoint Site via PowerShell by using Remove-PnPTenantSite.
This is not working because my Site is still part of an Office 365-Group and i have to delete that first.
To delete the Office 365-Group i have to get the Unified Group.
I tried to use Get-PnPUnifiedGroup, but that throws this:
Get-PnPUnifiedGroup : Exception while invoking endpoint https://login.microsoftonline.com/TOKEN/oauth2/token.
It would already help to get one of this two Methods to work,
Thanks in advance!
I had to create an app with the Microsoft-Graph-Api permissions, connect pnp online with the app credentials, then delete the unified Group , connect with the normal credentials and then delete the site:
Connect-PnPOnline -AppId AppID -AppSecret AppSecret -AADDomain Domain
Connect-PnPOnline -Url $siteUrl -Credentials $cred
Remove-PnPUnifiedGroup -Identity Title
Remove-PnPTenantSite Url -Force

My Azure Automation Runbook uses a stored credential to get a list of new groups, need an alternative that does not use stored credentials

I have a requirement to disable external sharing on SPOnline sites associated with new Groups (e.g., MS Teams, Planner, etc.). When an Office 365 user provisions a new Team, Planner, or Group an SPOnline site is provisioned in the background to store files for the associated Group. Be default, this "background" SPOnline site allows external sharing of content.
I have created an Azure Automation Runbook that creates a session to Exchange Online using a Stored Credential. I then get a list of the groups created in the past day.
$groups = Get-UnifiedGroup -Filter "WhenCreated -gt '$dt'"
With the list of Groups, I then iterate through them, connect to SPOnline with an AppId and AppSecret and disable sharing.
Connect-PnPOnline -AppId $appId -AppSecret $appSecret -Url $tenantUrl
$site = Get-PnPTenantSite -Url $group.SharePointSiteUrl
Write-Output "Sharing Capability: " $site.SharingCapability
if($site.SharingCapability -ne "Disabled")
{
#Set-PnPTenantSite -Url $site.Url -Sharing Disabled
Write-Output "Disabling external sharing"
}
else
{
Write-Output "External sharing already disabled"
}
My problem is that our production environment uses a PCS system to manage passwords for accounts. This would require more scripting to access the PCS, checkout the password, update the stored credential then do the work that I want to do.
I am able to get the list of Groups directly from Azure AD but the returned objects do not have the WhenCreated and SharePointSiteURL properties. Without these two pieces of information, I am not able to limit my script to the past day nor change the setting in SharePoint.
My goal is to explore any alternative that allow me to get the list of new Groups and the associated SPOnline site URL without using a stored credential in Azure Automation. Any ideas?
Thank you for your time.
Generally you only have 4 options:
hardcode credentials
use variables + certificates (you can make them secure string, so they are encrypted)
use run as account (allows use of certificates)
use credentials + certificates (pretty much the same as variables)