SharePoint Permissions Re-provision PowerShell
-
24-01-2021 - |
Pregunta
I've created 2 scripts, one to export SharePoint Online permissions to a .CSV and one to re-provision SharePoint Online access.
I've come across a slight problem which is preventing the script from running successfully, the script to export SharePoint permissions lists the groups within one cell. When the re-provision script reads the input file (.csv) it errors because the script can't seem to read multiple values within the same cell.
Here's an example input:
https://TEST.sharepoint.com test.user@test.com Test Group 1 Test Group 2 Test Group 3
I've also tried separating the groups via special characters but this doesn't work either:
Test Group 1, Test Group 2, Test Group 3
This is my re-provision script so far:
Write-Host "---Start of Script $(Get-Date)"
Connect-SPOService -URL https://******-admin.sharepoint.com -credential *****@*****
$SPOUserImport = Import-csv "PATH"
foreach($ImportData in $SPOUserImport)
{
$field1 = $ImportData.Group
$field2 = $ImportData.Site
$field3 = $ImportData.User
Add-SPOUser -Group $field1 -Site $field2 -LoginName $field3 |Out-Null
Write-Host "Added $field3 from .CSV to $field1, $field2" -ForegroundColor Green
}
Write-Host "---End of Script $(Get-Date)"
Any help appreciated!
Callum
Solución
The problem you are going to have is the way import-csv
is handling your group
column.
I'm assuming from your post where you say "lists the groups within one cell"your input file will look something like
site,user,group
https://test.sharepoint.com,test.user@test.com,Group1 Group2 Group3
The issue is that when you import that as a CSV your $importData
array won't recognise the multiple group names as it's a single field.
You can test this by typing $ImportData.group
at the prompt and observing the output, which will look something like :
Group1 Group2 Group3
What you will need to do is to turn that into it's own array
$importGroups = $importData.group.split(" ")
If you look at the content of the $importGroups
array now you should have an element for each group.
Group1
Group2
Group3
You need to use this variable to iterate through the available groups and add each individually to the new user, so something like :
Add-SPOUser -Group $importGroups[0] -Site $field2 -LoginName $field3 |Out-Null
foreach($group in $importGroups[1..$($importGroups.count-1)])
{
Add-SPOUser -Group $group -Site $field2 -LoginName $field3
}
Please note this is untested but the theory should be sound.