#Get-InstalledModule -Name "PnP.PowerShell"
#Uninstall-Module -Name PnP.PowerShell
#Install-Module -Name PnP.PowerShell -RequiredVersion 1.12.0
#Get-Command -Module PnP.Powershell
#Update-Module -Name "PnP.PowerShell"
#Script reads a csv file name "file.csv" having threee columns SiteURL,AppID and AppDisplayName
#Script grant Azure AD App having API sites.selected to access SharePoint site with "FullControl" permission.
try {
$csvData = Import-Csv -Path "file.csv"
# Get the current date and time
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
foreach ($row in $csvData)
{
$siteURL = $row.SiteURL
$appID = $row.AppID
$appDisplayName = $row.AppDisplayName
$displayNameConcatenated = "$appID - $appDisplayName"
Connect-PnPOnline -Url $siteURL -Interactive
# Grant Write permissions to the Azure AD app on the site
Grant-PnPAzureADAppSitePermission -AppId $appID -DisplayName $displayNameConcatenated -Site $siteURL -Permissions Write
# Fetch Permission ID
$PermissionId = Get-PnPAzureADAppSitePermission -AppIdentity $appID
# Set permissions to FullControl using the fetched permission ID
Set-PnPAzureADAppSitePermission -Site $siteURL -PermissionId $($PermissionId.Id) -Permissions "FullControl"
# Construct the filename with a timestamp
$filename = "Success-AzureADAppSitePermissions_$timestamp.txt"
# Retrieve Azure AD app permissions for the site and store the output in a file
$permissionOutput = Get-PnPAzureADAppSitePermission -Site $siteURL
$permissionOutput | Out-File -FilePath $filename -Encoding UTF8 -Force
# Completion message in green
Write-Host "Script executed successfully!" -ForegroundColor Green
}
}
catch {
# Display error message in red
Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
# Log error to a separate file
$errorLogFilename = "ErrorLog_$timestamp.txt"
$_.Exception.Message | Out-File -FilePath $errorLogFilename -Encoding UTF8 -Force
}
No comments:
Post a Comment