param( [Parameter(Mandatory=$true)] [string]$url, [Parameter(Mandatory=$true)] [string]$filePath ) function Write-File($msg) { ($msg) | Out-File "log-restore.txt" -Append } function Write-Info($msg) { Write-Host $msg -foregroundcolor green Write-File $msg } function Write-Warn($msg) { Write-Host $msg -foregroundcolor yellow Write-File $msg } function Add-Users-To-Group($web, $group, $usersStr) { if ([System.String]::IsNullOrEmpty($usersStr)) { return } Write-Info ("Add users to group " + $group.Name) $users = $usersStr.Split(";") foreach ($u in $users) { if ([System.String]::IsNullOrEmpty($u)) { continue } $user = $web.EnsureUser($u) Write-Info (" " + $user.LoginName) $group.AddUser($user) } } function Check-Web($web, $lines) { $url = $web.Url.Substring($web.Url.LastIndexOf("/")) if ($url.ToLower() -eq "/lomakkeet" -or $url.ToLower() -eq "/epostitus") { return } Write-Info ("Check users for " + $url) $idx = -1 $i = 0 foreach ($line in $lines) { if ($line.ToLower().EndsWith($url.ToLower())) { $idx = $i break } $i = $i + 1 } if ($idx -lt 0) { Write-Warn ("Condo url not found") return } $groupAsukkaat = $lines[$idx + 1] $groupOsakkaat = $lines[$idx + 2] $groupHallitus = $lines[$idx + 3] $groupRekAsukkaat = $lines[$idx + 4] $groupRekOsakkaat = $lines[$idx + 5] Write-Info ("Asukkaat: " + $groupAsukkaat) Write-Info ("Osakkaat: " + $groupOsakkaat) Write-Info ("Hallitus: " + $groupHallitus) Write-Info ("RekAsukkaat: " + $groupRekAsukkaat) Write-Info ("RekOsakkaat: " + $groupRekOsakkaat) foreach ($g in $web.Groups) { if ($g.Name.EndsWith("- Asukkaat")) { Add-Users-To-Group $web $g $groupAsukkaat } elseif ($g.Name.EndsWith("- Osakkaat")) { Add-Users-To-Group $web $g $groupOsakkaat } elseif ($g.Name.EndsWith("- Hallitus")) { Add-Users-To-Group $web $g $groupHallitus } elseif ($g.Name.EndsWith("- Rekisteröityneet asukkaat")) { Add-Users-To-Group $web $g $groupRekAsukkaat } elseif ($g.Name.EndsWith("- Rekisteröityneet osakkaat")) { Add-Users-To-Group $web $g $groupRekOsakkaat } } } $lines = [System.IO.File]::ReadAllLines($filePath) $site = Get-SPSite $url foreach ($w in $site.RootWeb.Webs) { Check-Web $w $lines }