Gino Abraham's Blog

March 2017 Entries

Custom Role Claim Based Authentication on SharePoint 2013

We had a requirement in a project to authenticate users in a site collection based on Country claim presented by a User.

Below powershell sample is to add a US country claim value to a Visitors group of a site collection to allow any users from US to get authorized to view the site.


$web = Get-SPWeb "https://SpSiteCollectionUrl"
$claim = New-SPClaimsPrincipal -TrustedIdentityTokenIssuer “users” -ClaimValue “US" -ClaimType “http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country
$group = $web.Groups[“GDE Home Visitors“]
$group.AddUser($claim.ToEncodedString(), “”, “US", “”)


Note : for this solution to work you we have used an ADFS solution which pull our calim values from Enterpise directory and sends SAML claim to SharePoint.


Powershell to monitor Server Resource and make html report for a set number of iteration and intreval

While building a SharePoint farm for a project, i did load testing with VSTS.
Since my SharePoint farm was in a different domain than that of my Load test controllers, I had to monitor few performance counters to take a well informed decision while tuning the farm capacity.

Below powershell was developed by me to generate a html report for all the different servers at specified intervals.
The output html file will be generated in D Drive. 



#Array which contains the different Server names
$ServerList  = @('server01', 'server02', 'server03','Server04','Server05')

#Array which represents the role of the server
$ServerRole=@('WFE1', 'WFE2', 'Central Admin', 'WorkFlow1','WorkFlow2')

#Number of times this powershell should be executed
$runcount = 15;

$Outputreport = "<HTML><TITLE> Server Health Report </TITLE>
                     <BODY background-color:peachpuff>
                     <font color =""#99000"" face=""Microsoft Tai le"">
                     <H2> Server Health Report </H2></font>
                     <Table border=1 cellpadding=0 cellspacing=0>
                     <TR bgcolor=gray align=center>
                       <TD><B>Server Name</B></TD><TD><B>Server Role</B></TD>
                       <TD><B>Avrg.CPU Utilization</B></TD>
                       <TD><B>Memory Utilization</B></TD>
                       <TD><B>C Drive Utilizatoin</B></TD>
                        <TD><B>Iteration</B></TD>                        
                        </TR>"



for($i=1; $i -le $runcount;$i++)
{

        
    $ArrayCount = 0
 
    ForEach($computername in $ServerList) 
    {

        $role = $ServerRole[$ArrayCount]
        $ArrayCount = $ArrayCount  + 1
        
        Write-Host $i $computername $role 

        $AVGProc = Get-WmiObject -computername $computername win32_processor | 
        Measure-Object -property LoadPercentage -Average | Select Average
        $OS = gwmi -Class win32_operatingsystem -computername $computername |
        Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
        $vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" |
        Select-object @{Name = "C PercentFree"; Expression = {“{0:N2}” -f  (($_.FreeSpace / $_.Capacity)*100) } }
  
        $result += [PSCustomObject] @{ 
                ServerName = "$computername"
                CPULoad = "$($AVGProc.Average)%"
                MemLoad = "$($OS.MemoryUsage)%"
                CDrive = "$($vol.'C PercentFree')%"
            }

    
                        
        Foreach($Entry in $result) 
        { 
            if(($Entry.CpuLoad) -or ($Entry.memload) -ge "80") 
            { 
            $Outputreport += "<TR bgcolor=red>" 
            } 
            else
            {
            $Outputreport += "<TR>" 
            }
            $Outputreport += "<TD>$($Entry.Servername)</TD> <TD>$role</TD>  <TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.Cdrive)</TD><TD>$i</TD></TR>" 
        }

        $result = $null
     
    } 
    
}

$Outputreport += "</Table></BODY></HTML>" 
 
$time = $(get-date -f MM-dd-yyyy_HH_mm_ss) 
$file = 'D:\' + $time + '.htm'

$Outputreport | out-file $file

Invoke-Expression $file