Power Shell for Adding User to SPGroup

param(
[string] $envType = $(throw “envType(PersonalDev, SharedDev, Test, Sandbox or Production)required”),
[string] $workFolder = (Get-Item (“.”)).FullName,
[string] $wspName = “MyTest.My.Intra.wsp”
)

if(-not(Get-PSSnapin | Where { $_.Name -eq “Microsoft.SharePoint.PowerShell”})) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

$solName = “MyTest.My.Intra.wsp”

#
# Functions
#

function LogProgress($message)
{
$now = Get-Date –f “yyyy-MM-dd HH:mm:ss”
if ($isInterActiveSession -eq “True”)
{
Write-Host `n $now.ToString() -: $message
}
else
{
Write-Output ($now.ToString() + “: ” + $message) | Out-File $logFile -append
}

}

function SetPSSiteCollectionAdminArray($alAddUsersToSP)
{
[string] $UserNameID=”AddUserName”;
[string] $UserGroupID=”SPUserGroup”;
[string] $UserName;
[string] $UserGroup;
[string] $TypeOfUserID = “IsSiteCollectionAdmin”;
[string] $TypeOfUser;

$Counter=$alAddUsersToSP.Count;
for ($i = 0; $i -lt $Counter; $i++)
{
[HashTable] $ht=$alAddUsersToSP[$i];
$KeyCounter= $ht.Count;
$ht.GetEnumerator() | ForEach-Object {
[String] $Key=$_.Key;
[String] $Val=$_.Value;
if($UserNameID -eq $Key)
{
$UserName=$Val;
}
if($UserGroupID -eq $Key)
{
$UserGroup=$Val;
}
if($TypeOfUserID -eq $Key)
{
$TypeOfUser=$Val;
}

}
if($TypeOfUser -eq “Yes”)
{
$SiteCollectionAdminArray.Add($UserName);
}

}
}

[HashTable] $htSPUserGroup=@{
1 = “Home Owners”;
2 =”Home Members”;
3 =”Home Visitors”;
}

###### Start : AddUser and AddAdminUser ########

function AddUsers($alAddUsersToSP)
{
[string] $UserNameID=”AddUserName”;
[string] $UserGroupID=”SPUserGroup”;
[string] $UserName;
[string] $UserGroup;
[string] $TypeOfUserID = “IsSiteCollectionAdmin”;
[string] $TypeOfUser;

$Counter=$alAddUsersToSP.Count;
for ($i = 0; $i -lt $Counter; $i++)
{
[HashTable] $ht=$alAddUsersToSP[$i];
$KeyCounter= $ht.Count;
$ht.GetEnumerator() | ForEach-Object {
[String] $Key=$_.Key;
[String] $Val=$_.Value;
if($UserNameID -eq $Key)
{
$UserName=$Val;
}
if($UserGroupID -eq $Key)
{
$UserGroup=$Val;
}
if($TypeOfUserID -eq $Key)
{
$TypeOfUser=$Val;
}

#Write-Host “$Key : $Val”
}
if($TypeOfUser -eq “Yes”)
{
#Write-Host “I am Admin => $UserName : $UserGroup added to $siteUrl”
New-SPUser -UserAlias $UserName -Web $siteUrl -SiteCollectionAdmin
}
else
{
#Write-Host “I am not Admin => $UserName : $UserGroup added to $siteUrl”
New-SPUser -UserAlias $UserName -Web $siteUrl -Group $UserGroup
}
#Write-Host “__________________New user Added_______________________”
}
}

switch ($envType)
{
“PersonalDev”
{
$comp = Get-WmiObject -Class Win32_ComputerSystem
$hostHeader = “intra.Mydev.local”
$port = 80
$siteUrl = “http://” + $hostHeader
$appPoolName = “SharePoint – ” + $hostHeader + $port
#$solPath = (Get-ChildItem (“..\bin\Debug\” + $wspName)).FullName

}
“SharedDev”
{
$comp = Get-WmiObject -Class Win32_ComputerSystem
$hostHeader = “intra.Mydev.MyTest.com”
$port = 80
$siteUrl = “http://” + $hostHeader
$appPoolName = “SharePoint – ” + $hostHeader + $port
$solPath = $workFolder + “\” + $wspName
}
“Test”
{
$comp = Get-WmiObject -Class Win32_ComputerSystem
$hostHeader = “test-avenue.My.com”
$port = 80
$siteUrl = “http://” + $hostHeader
$appPoolName = “SharePoint – ” + $hostHeader + $port
$solPath = $workFolder + “\” + $wspName
}
“Sandbox”
{
$comp = Get-WmiObject -Class Win32_ComputerSystem
$hostHeader = “playground-avenue.My.com”
$port = 80
$siteUrl = “http://” + $hostHeader
$appPoolName = “SharePoint – ” + $hostHeader + $port
$solPath = $workFolder + “\” + $wspName
}
“Production”
{
$comp = Get-WmiObject -Class Win32_ComputerSystem
$hostHeader = “avenue.My.com”
$port = 80
$siteUrl = “http://” + $hostHeader
$appPoolName = “SharePoint – ” + $hostHeader + $port
$solPath = $workFolder + “\” + $wspName
}
default
{
throw “Error: unknown value given for parameter 1 envType”
}
}

###### Start : Hashtable : AddUser and AddAdminUser ########
[HashTable] $htMyAddUser1=@{
“AddUserName”= @($comp.Name + “\MyVisitor1”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}

[HashTable] $htMyAddUser2=@{
“AddUserName”= @($comp.Name + “\MyVisitor2”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser3=@{
“AddUserName”= @($comp.Name + “\MyVisitor3”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser4=@{
“AddUserName”= @($comp.Name + “\MyVisitor4”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser5=@{
“AddUserName”= @($comp.Name + “\MyVisitor5”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser6=@{
“AddUserName”= @($comp.Name + “\MyVisitor6”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser7=@{
“AddUserName”= @($comp.Name + “\MyVisitor7”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser8=@{
“AddUserName”= @($comp.Name + “\MyVisitor8”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser9=@{
“AddUserName”= @($comp.Name + “\MyVisitor9”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser10=@{
“AddUserName”= @($comp.Name + “\MyVisitor10”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser11=@{
“AddUserName”= @($comp.Name + “\MyVisitor11”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser12=@{
“AddUserName”= @($comp.Name + “\MyVisitor12”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser13=@{
“AddUserName”= @($comp.Name + “\MyVisitor13”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser14=@{
“AddUserName”= @($comp.Name + “\MyVisitor14”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser15=@{
“AddUserName”= @($comp.Name + “\MyVisitor15”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser16=@{
“AddUserName”= @($comp.Name + “\MyVisitor16”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser17=@{
“AddUserName”= @($comp.Name + “\MyVisitor17”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser18=@{
“AddUserName”= @($comp.Name + “\MyVisitor18”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser19=@{
“AddUserName”= @($comp.Name + “\MyVisitor19”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser20=@{
“AddUserName”= @($comp.Name + “\MyVisitor20”);
“SPUserGroup” = “Home Visitors”;
“IsSiteCollectionAdmin” = “No”;
}

##### Home Contributors
[HashTable] $htMyAddUser21=@{
“AddUserName”= $comp.Name + “\MyContributor1”;
“SPUserGroup” = “Home Members”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser22=@{
“AddUserName”= $comp.Name + “\MyContributor2”;
“SPUserGroup” = “Home Members”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser23=@{
“AddUserName”= $comp.Name + “\MyContributor3”;
“SPUserGroup” = “Home Members”;
“IsSiteCollectionAdmin” = “No”;
}

##### Home Owners

[HashTable] $htMyAddUser24=@{
“AddUserName”= $comp.Name + “\MyAdmin1”;
“SPUserGroup” = “Home Owners”;
“IsSiteCollectionAdmin” = “No”;
}
[HashTable] $htMyAddUser25=@{
“AddUserName”= $comp.Name + “\MyAdmin2”;
“SPUserGroup” = “Home Owners”;
“IsSiteCollectionAdmin” = “No”;
}

### Start : Add UserHash to Collection ArrayList ###
$alAddUsers = New-Object System.Collections.ArrayList ;
$alAddUsers.Add($htMyAddUser1);
$alAddUsers.Add($htMyAddUser2);
$alAddUsers.Add($htMyAddUser3);
$alAddUsers.Add($htMyAddUser4);
$alAddUsers.Add($htMyAddUser5);
$alAddUsers.Add($htMyAddUser6);
$alAddUsers.Add($htMyAddUser7);
$alAddUsers.Add($htMyAddUser8);
$alAddUsers.Add($htMyAddUser9);
$alAddUsers.Add($htMyAddUser10);
$alAddUsers.Add($htMyAddUser11);
$alAddUsers.Add($htMyAddUser12);
$alAddUsers.Add($htMyAddUser13);
$alAddUsers.Add($htMyAddUser14);
$alAddUsers.Add($htMyAddUser15);
$alAddUsers.Add($htMyAddUser16);
$alAddUsers.Add($htMyAddUser17);
$alAddUsers.Add($htMyAddUser18);
$alAddUsers.Add($htMyAddUser19);
$alAddUsers.Add($htMyAddUser20);
$alAddUsers.Add($htMyAddUser21);
$alAddUsers.Add($htMyAddUser22);
$alAddUsers.Add($htMyAddUser23);
$alAddUsers.Add($htMyAddUser24);
$alAddUsers.Add($htMyAddUser25);
### End : Add UserHash to Collection ArrayList ###

LogProgress “Adding site users”
AddUsers $alAddUsers;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s