Compartilhar via


PowerShell and Farm Administrators

#how do you add someone to  the Farm Admin group?
---
#stsadm -o AddUser was the old way.. but that is dead.
----
#So, OK, let's use SPuser.
$user = New-SPUser -UserAlias "LITWAREINC\User1" -Web https://MSF1:9999
Set-SPUser -Identity "LITWAREINC\User1" -Web https://MSF1:9999 -Group "Farm Administrators"
----
# or.. hmm.. if I had to figure out things automatically, perhaps something like this:

function Get-SPfarmAdministrators {
  $adminwebapp = Get-SPwebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication}
  $adminsite = Get-SPweb($adminwebapp.Url)
  $AdminGroupName = $adminsite.AssociatedOwnerGroup
  $farmAdministratorsGroup = $adminsite.SiteGroups[$AdminGroupName]
  return $farmAdministratorsGroup.users
}

function Add-SPfarmAdministrator { Param ([string] $LoginName) 
  $adminwebapp = Get-SPwebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication}
  $adminsite = Get-SPweb($adminwebapp.Url)
  $admingroup = $adminsite.AssociatedOwnerGroup
  $adminsite.SiteGroups[$admingroup].AddUser($LoginName,"","","")
}
$farmadmins = Get-SPfarmAdministrators
$farmadmins | FT

$NewGuy = "LITWAREINC\User1"
Add-SPfarmAdministrator $NewGuy
$farmadmins = Get-SPfarmAdministrators
$farmadmins | FT

-----
#but that is very 2010 specific.. so create the SP-Blah calls and voila! 2007 works too.
# a Get-SPfarmAdministrators will need a little work, but...

[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function Get-SPSite([string]$url) {
 New-Object Microsoft.SharePoint.SPSite($url)
}
function Get-SPWeb([string]$url) {
 $SPSite = Get-SPSite $url
 return $SPSite.OpenWeb()
 $SPSite.Dispose()
}
function Get-SPAdminWebApplication {
 $SPfarm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
 $SPWebServices = new-object Microsoft.Sharepoint.Administration.SPWebServiceCollection($SPFarm)
 foreach ($SPWebService in $SPWebServices) {
   $SPWebApplicationCollection = $SPWebService.WebApplications
   foreach ($SPWebApplication in $SPWebApplicationCollection) {
     if ($SPWebApplication.IsAdministrationWebApplication) {
      $adminWebapp = $SPWebApplication
     }
  }
 }
 return $adminWebApp
}
function Get-SPfarmAdministrators {
  $admin = Get-SPAdminWebApplication
  foreach ($adminsite in $admin.Sites ) {
    $adminWeb = Get-SPweb($adminsite.url)
    $AdminGroupName = $adminWeb.AssociatedOwnerGroup
    $farmAdministratorsGroup = $adminweb.SiteGroups[$AdminGroupName]
    return $farmAdministratorsGroup.users
  }
}
$farmadmins = Get-SPfarmAdministrators
$farmadmins | FT