Creating function and needing some coding help...

Jul 20, 2012 at 5:44 PM

I am creating a function that will add specific users/groups to web application policies during the install. I have the code needed to add the users/groups but I can't figure out how to read the XML file to return mulitple items into an array...

Here is the XML format:

<WebApplicationPerms>
  <Add>true</Add>
  <Groups-Users>domain\group domain\user</Groups-Users>
 </WebApplicationPerms>

PowerShell code:

Function  Add-WebAppPolicy-Perms
{
 If ($xmlinput.Configuration.WebApplications.WebApplicationPerms.Add -eq $true)
 {

$allWebApps = Get-SPWebApplication

foreach($WebApp in $allWebApps)
{
 try
 {
    foreach($Account in $Accountlist)
    {
       #$Account
    #Add full control to Virusscanuser and Loadtestuser
    $ADMINPolicy = $WebApp.Policies.Add($Account,$Account)
    $ADMINPolicy.PolicyRoleBindings.Add($WebApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl)) 
    #Add full read to objectCachePortalSuperUserRead
    #$ObjectCachePolicy = $WebApp.Policies.Add($objectCachePortalSuperUserRead,$objectCachePortalSuperUserRead)
    #$ObjectCachePolicy.PolicyRoleBindings.Add($WebApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead)) 
    }
 $WebApp.Update()
 }
 Catch
 {
 }
}
 }
}

Jul 23, 2012 at 11:14 AM

Hi,

try something like this:

<WebApplicationPerms enable="true">
  <PermUser Name="Domain\User1">
  <PermUser Name="Domain\User2">
</WebApplicationPerms>

and then use in your code like this:

Function  Add-WebAppPolicy-Perms
{
  If ($xmlinput.Configuration.WebApplications.WebApplicationPerms.enable -eq "true")
  {
    $allWebApps = Get-SPWebApplication
    foreach($WebApp in $allWebApps)
    {
      try
      {
         $xmlinput.Configuration.WebApplications.WebApplicationPerms.PermUser | ForEach-Object 
         {
           $UserName = $_.Name
           $ADMINPolicy = $WebApp.Policies.Add($UserName,$UserName)
           $ADMINPolicy.PolicyRoleBindings.Add($WebApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl)) 
         }
         $WebApp.Update()
      }
      Catch { }
      }
   }
}

Stefan 

Jul 23, 2012 at 2:36 PM

Thanks Stefan! I will give this a try.

Jul 24, 2012 at 9:48 AM

by the way, totally missed this one. The script already contains some functions to handle this which is called Set-WebAppUserPolicy. Try to make use of that one.

Stefan