diff --git a/documentation/Remove-PnPTenantRestrictedSearchAllowedList.md b/documentation/Remove-PnPTenantRestrictedSearchAllowedList.md new file mode 100644 index 000000000..fb7a187db --- /dev/null +++ b/documentation/Remove-PnPTenantRestrictedSearchAllowedList.md @@ -0,0 +1,109 @@ +--- +Module Name: PnP.PowerShell +schema: 2.0.0 +applicable: SharePoint Online +online version: https://pnp.github.io/powershell/cmdlets/Remove-PnPTenantRestrictedSearchAllowedList.html +external help file: PnP.PowerShell.dll-Help.xml +title: Remove-PnPTenantRestrictedSearchAllowedList +--- + +# Remove-PnPTenantRestrictedSearchAllowedList + +## SYNOPSIS +Removes site URLs from the allowed list when Restricted SharePoint Search is enabled. The URLs can be provided as a string array or read from a CSV file. + +## SYNTAX + +```powershell +Remove-PnPTenantRestrictedSearchAllowedList [-SitesListFileUrl ] [-SitesList ] [-ContainsHeaders ] [-Connection ] +``` + +## DESCRIPTION + +Removes site URLs from the allowed list when Restricted SharePoint Search is enabled. The URLs can be provided directly as a string array or read from a CSV file. At present, a maximum of 100 sites can be added to the allowed list. + +## EXAMPLES + +### EXAMPLE 1 +```powershell +Remove-PnPTenantRestrictedSearchAllowedList -SitesListFileUrl "C:\temp\sitelist.csv" -ContainsHeader +``` + +Removes site URLs from the allowed list from a CSV file. The first line, which is assumed to be a header, is skipped. + +### EXAMPLE 2 +```powershell +Remove-PnPTenantRestrictedSearchAllowedList -SitesListFileUrl "C:\temp\sitelist.csv" +``` + +Removes site URLs from the allowed list from a CSV file. + +### EXAMPLE 3 +```powershell +Remove-PnPTenantRestrictedSearchAllowedList -SitesList @("https://contoso.sharepoint.com/sites/Company311","https://contoso.sharepoint.com/sites/contosoportal") +``` +Removes the specified sites from the allowed list. + +## PARAMETERS + +### -Connection +Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. + +```yaml +Type: PnPConnection +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SitesListFileUrl +Specifies the path of the CSV file that contains a list of site URLs to be removed from the allowed list when the tenant is set to Restricted Tenant Search Mode. + +```yaml +Type: String +Parameter Sets: File + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SitesList +Specifies a collection of sites to remove from the allowed list. + +```yaml +Type: String[] +Parameter Sets: SiteList + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Containsheader + +If specified, this switch skips the first line from the CSV file, which is assumed to be a header. + +```yaml +Type: SwitchParamter +Parameter Sets: File + +Required: False +Position: Named +Default value: False +Accept pipeline input: False +Accept wildcard characters: False +``` + +## RELATED LINKS + +[How does Restricted SharePoint Search work?](https://learn.microsoft.com/sharepoint/restricted-sharepoint-search) +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/src/Commands/Admin/RemoveTenantRestrictedSearchAllowedList.cs b/src/Commands/Admin/RemoveTenantRestrictedSearchAllowedList.cs new file mode 100644 index 000000000..99086756d --- /dev/null +++ b/src/Commands/Admin/RemoveTenantRestrictedSearchAllowedList.cs @@ -0,0 +1,75 @@ +using Microsoft.Online.SharePoint.TenantAdministration; +using Microsoft.SharePoint.Client; +using PnP.PowerShell.Commands.Base; +using System.Collections.Generic; +using System.Management.Automation; +using System.Linq; +using System; + +namespace PnP.PowerShell.Commands.Admin +{ + [Cmdlet(VerbsCommon.Remove, "PnPTenantRestrictedSearchAllowedList", DefaultParameterSetName = ParameterSet_SiteList)] + public class RemoveTenantRestrictedSearchAllowedList : PnPAdminCmdlet + { + private const string ParameterSet_SiteList = "SiteList"; + private const string ParameterSet_File = "File"; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_SiteList)] + public string[] SitesList; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_File)] + public string SitesListFileUrl; + + [Parameter(Mandatory = false, ParameterSetName = ParameterSet_File)] + public SwitchParameter ContainsHeader; + + protected override void ExecuteCmdlet() + { + IList _sitelist = null; + if (ParameterSetName == ParameterSet_File) + { + _sitelist = ReadFileContents(); + } + else if (ParameterSetName == ParameterSet_SiteList) + { + _sitelist = SitesList; + } + else + { + throw new ArgumentException("Parameter set cannot be resolved using the specified named parameters."); + } + + if (_sitelist == null) + { + throw new InvalidOperationException("SiteList cannot be null"); + } + + if(_sitelist.Count > 100) + { + WriteWarning($"The maximum number of sites that can be added to the allowed list is 100. You have specified {_sitelist.Count} sites. Will try to add them anyway."); + } + + Tenant.RemoveSPORestrictedSearchAllowedList(_sitelist); + AdminContext.ExecuteQueryRetry(); + } + + private IList ReadFileContents() + { + var lines = System.IO.File.ReadAllLines(SitesListFileUrl); + if (ContainsHeader) + { + lines = lines.Skip(1).ToArray(); + } + + foreach (var line in lines) + { + if (line.Contains(',')) + { + throw new InvalidOperationException("File should only contain one column and no commas"); + } + } + + return lines.ToList(); + } + } +} \ No newline at end of file