<#====================================================================

Copyright © 2015, September. Michael Pomfret

Adds a number of SharePoint Administrators to the local administrator Group

1. Function to check for the existence of Local group.

2. function to create the local group

====================================================================#>

# Powershell function to check for the Local user account…

function LocalUserExist($userName)

{

$Computer = [ADSI]”WinNT://$Env:COMPUTERNAME,Computer”

# Local user account creation:

$colUsers = ($Computer.psbase.children | Where-Object {$_.psBase.schemaClassName -eq “User”} | Select-Object -expand Name)

$userFound = $colUsers -contains $userName

return $userFound

}

# Powershell to check for the existence of Local group…

function LocalGroupExist($groupName)

{

return [ADSI]::Exists(“WinNT://$Env:COMPUTERNAME/$groupName,group”)

}

# Powershell function to create the local user…

function CreateLocalUser($userName,$password)

{

$userExist = LocalUserExist($userName)

if($userExist -eq $false)

{

$User = $Computer.Create(“User”, $userName)

$User.SetPassword($password)

$User.SetInfo()

$User.FullName = “Full Name”

$User.SetInfo()

$User.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD

$User.SetInfo()

}

else {

“User : $userName already exist.”

}

}

# PS function to create the local group

function CreateLocalGroup($groupName)

{

$groupExist = LocalGroupExist($groupName)

if($groupExist -eq $false)

{

$Group = $Computer.Create(“Group”, $groupName)

$Group.SetInfo()

$Group.Description = $groupName

$Group.SetInfo()

}

else

{

“Group : $groupName already exist.”

}

}

# PS function to check for the group in the local machine…

function CheckGroupMember($groupName,$memberName)

{

$group = [ADSI]”WinNT://$Env:COMPUTERNAME/$groupName”

$members = @($group.psbase.Invoke(“Members”))

$memberNames = $members | foreach {$_.GetType().InvokeMember(“Name”, ‘GetProperty’, $null, $_, $null)}

$memberFound = $memberNames -contains $memberName

return $memberFound

}

# PS function to add a user to the group…

function AddUserToGroup ($groupName, $userName)

{

$group = [ADSI]”WinNT://$Env:COMPUTERNAME/$groupName”

$user = [ADSI]”WinNT://$Env:COMPUTERNAME/$userName”

$memberExist = CheckGroupMember $groupName $userName

if($memberExist -eq $false)

{

$group = [ADSI]”WinNT://$Env:COMPUTERNAME/$groupName”

$user = [ADSI]”WinNT://$Env:COMPUTERNAME/$userName”

$group.Add($user.Path)

}

}

Write-Host “Windows Server 2012 – Adding SharePoint Setup Administrator to local administrators group”

$ADusername = “xxx_SP_Farm”

$groupName = “Administrators”

if (CheckGroupMember($groupName,$ADusername)-eq $false)

{

$domain = “AD”

$strComputer = $env:computername

$username = “xxx_SP_Farm”

$computer = [ADSI](“WinNT://” + $strComputer + “,computer”)

$computer.name

$Group = $computer.psbase.children.find(“Administrators”)

$Group.name

$Group.Add(“WinNT://” + $domain + “/” + $username)

}

else

{

“Username : $ADusername already exist.”

}

cmd.exe /c pause