About Me

SharePoint Architect with over 17 years of IT Experience in various roles as SharePoint Architect, Administrator, Technical Architect, IT Analyst, Application Developer, CRM Consultant, BI Developer, Microsoft Solution Architect in Client/Server, Web and Enterprise CRM Applications in E-Commerce, Financial, Healthcare, Insurance, Telecom, Outsourcing and Technology Services.
MCITP (Pro): Microsoft Certified IT Professional: SharePoint 2010 Administrator.
MCTS: Microsoft Certified Technology Specialist, SharePoint 2010 Configuration.

Tuesday, October 25, 2011

Import Users to Site Groups


.\ImportGroupsAndUsers -siteurl http://webapp.domain.com/sites/SalesCenter  .\SalesCenter1.csv


SalesCenter1.csv
User,Group
email1@domain.com,Sales Center Members
email1@domain.com,Sales Center Members
email1@domain.com,Sales Center Members


ImportGroupsAndUsers.ps1

param (
    [string] $siteurl = "",
                [string] $importtogroups = "",
                [string] $createspgroups = "",
                [string] $importwithperm = ""
)

if ($siteurl -eq "")
{
                $siteurl = Read-Host "Site url";
}

[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");

Write-Host -ForegroundColor green "Opening SharePoint site...";

$site = New-Object Microsoft.SharePoint.SPSite($siteurl)
$web = $site.RootWeb;

if ($createspgroups -ne "")
{
                Write-Host -ForegroundColor green "Importing sharepoint groups from $createspgroups";
                ipcsv $createspgroups | foreach {
                                $group = $_.GroupName;
                                $perm = $_.Permissions;
                                $owner = $_.Owner;
                                if ($owner -eq "") { $owner = $web.CurrentUser.LoginName }
                               
                                Write-Host -ForegroundColor green "  $group (owner = $owner, perm = $perm)"
                                $exists = $web.SiteGroups | where { $_.Name -eq $group }
                                if ($exists -eq $null)
                                {
                                                # Create group
                                                $web.SiteGroups.Add($group, $web.EnsureUser($owner), $null, "");
                                                # Give permissions to the group
                                                $assign = New-Object Microsoft.SharePoint.SPRoleAssignment($web.SiteGroups[$group]);
                                                $assign.RoleDefinitionBindings.Add($web.RoleDefinitions[$perm])
                                                $web.RoleAssignments.Add($assign)
                                }
                                else
                                {
                                                Write-Host -ForegroundColor magenta "    already exist"
                                }
                }
}

if ($importtogroups -ne "")
{
                Write-Host -ForegroundColor green "Importing users from $importtogroups";
                ipcsv $importtogroups | foreach {
                                $user = $_.User
                                $group = $_.Group
                                Write-Host -ForegroundColor green "  $user -> $group"
                                # Add user to the web
                                $spuser = $web.EnsureUser($user);
                                # Add user to group
                                if ($group -ne "")
                                {
                                                $web.SiteGroups[$group].AddUser($spuser);
                                }
                }
}

if ($importwithperm -ne "")
{
                Write-Host -ForegroundColor green "Importing ad groups from $importwithperm";
                ipcsv $importwithperm | foreach {
                                $group = $_.GroupName;
                                $perm = $_.Permissions;
                               
                                Write-Host -ForegroundColor green "  $group (perm = $perm)"
                                $sp = $web.EnsureUser($group);

                                # Give permissions to the group
                                $assign = New-Object Microsoft.SharePoint.SPRoleAssignment($sp);
                                $assign.RoleDefinitionBindings.Add($web.RoleDefinitions[$perm])
                                $web.RoleAssignments.Add($assign)
                }
}

$site.Dispose();
Write-Host -ForegroundColor green "Done.";
 

Monday, October 24, 2011

Get Worker Process with Application Pool Names and Memories

$list = gwmi win32_process -computername "Useaihp460" -filter "name='w3wp.exe'" | Select-Object @{Name="WS";Expression={ ($_.WS/ 1MB).ToString("0,000.0") } }, @{Name="VM";Expression={ ($_.VM / 1MB).ToString("0,000.0") } },@{Name="ProcessId";Expression={ $_.ProcessId.ToString("00000") } }, @{Name="Name";Expression={ $_.Name.ToString() } },@{Name="CommandLine";Expression={ $_.CommandLine.ToString() } }
Write-Host “WS      VM      ProcessId      Application Pool Name”
$list | foreach  {
$from = "c:\windows\system32\inetsrv\w3wp.exe -ap ".Length + 1
$locdll = $_.commandLine.IndexOf("webengine4.dll")
$poolnam = $locdll-16-$from
$len=$_.commandLine.Length
$memory = "4096"
$name1 = $_.commandLine.Substring($from , $poolnam)  
Write-Host $_.WS $_.VM $_.processId $name1
#"WS:{0:N0} VM:{1:N0} ProcessId:{2,6} Name:{3,6} Pool:{4}" -f $_.WS.ToString("00,000,000,000"), $_.VM.ToString("00,000,000,000"), $_.processId, $_.name, $name1
#@{Name="WS";Expression={ ($_.WS / 1MB).ToString("0,000.0") } },
#@{Name="VM";Expression={ ($_.VM / 1MB).ToString("0,000.0") } }
}

SQLServer MountPoint Disk Space

Get-WmiObject -class "win32_volume" -namespace "root\cimv2" -computername ServerName |
Where-Object {$_.name -notmatch "\\\\" -and $_.DriveType -eq "3"} |
Select-Object Name, Label, @{name="CapacityGB";Expression={"{0:N0}"  -f ($_.capacity / 1gb)}},
@{name="UsedGB";Expression={"{0:N0}"  -f ($($_.capacity-$_.freespace) / 1gb)}},
@{name="FreeGB";Expression={"{0:N0}"  -f ($_.freespace / 1gb)}},
@{name="FreePC";Expression={"{0:N0}"  -f ($_.freespace / $_.capacity * 100)}} |
Sort-Object SystemName,Name | ft -AutoSize



Get-WmiObject -class "win32_volume" -namespace "root\cimv2" -computername ServerName |
Where-Object {$_.name -notmatch "\\\\" -and $_.DriveType -eq "3"} |
Select-Object Name, Label, @{name="CapacityGB";Expression={"{0:N0}"  -f ($_.capacity / 1gb)}},
@{name="UsedGB";Expression={"{0:N0}"  -f ($($_.capacity-$_.freespace) / 1gb)}},
@{name="FreeGB";Expression={"{0:N0}"  -f ($_.freespace / 1gb)}},
@{name="FreePC";Expression={"{0:N0}"  -f ($_.freespace / $_.capacity * 100)}} |
Sort-Object SystemName,Name
| Out-GridView -Title "Drive Space"

 
will show as


Crawl Properties: Update / Set Crawl Properties

$ssaName="Search Service Application"
$context=[Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($ssaName)

$daily=New-Object Microsoft.Office.Server.Search.Administration.DailySchedule($context)
$daily.BeginDay="25"
$daily.BeginMonth = "1"
$daily.BeginYear = "2010"
$daily.StartHour = "2"
$daily.StartMinute = "30"
$daily.DaysInterval = "1"

$contentsource = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssaName -Identity "Local SharePoint Sites"

$contentsource.IncrementalCrawlSchedule=$daily
$contentsource.FullCrawlSchedule=$weekly
$contentsource.Update()

Crawls: Start and Stop Crawls

1. CrawlStart.ps1

Add-PSSnapin Microsoft.SharePoint.PowerShell

$SSA = Get-SPEnterpriseSearchServiceApplication -Identity "Search Service Application"
$ContentSource = $SSA | Get-SPEnterpriseSearchCrawlContentSource -Identity "My Content Source"
if ($ContentSource.CrawlStatus  -eq "Idle" )
{        
                $ContentSource.StartIncrementalCrawl()    
                Write-Host "Starting Incremental Crawl"
                if ($ContentSource.CrawlStatus  -eq "Paused" )
                {        
                $ContentSource.ResumeCrawl()    
                Write-Host "Resuminging Incremental Crawl"
                }
}

2. CrawlPause.ps1

Add-PSSnapin Microsoft.SharePoint.PowerShell

$SSA = Get-SPEnterpriseSearchServiceApplication -Identity "Search Service Application"
$ContentSource = $SSA | Get-SPEnterpriseSearchCrawlContentSource -Identity "My Content Source"
Write-Host $ContentSource.CrawlState
if (($ContentSource.CrawlStatus  -eq "CrawlingIncremental" ) -or ($ContentSource.CrawlStatus  -eq "CrawlingFull" ))
{
                $ContentSource.PauseCrawl()
                Write-Host "Pausing the current Crawl"
}
Write-host $ContentSource.CrawlState

Enum Crawl Properties.


Get-EnumCrawlProp.ps1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

param(
[String]$InputFile = ""
)

If ($InputFile)
{
$ContentSrc = Get-Content $InputFile
$searchapp = Get-SPEnterpriseSearchServiceApplication |where {$_.name -eq "ProdSearchSA1"};

ForEach ($c in $ContentSrc)
{
                $csource = Get-SPEnterpriseSearchCrawlContentSource $c -sea $searchapp;
                #Write-Host "Crawl State for $c " $csource.CrawlState
                $csource.IncrementalCrawlSchedule | select DaysOfWeek, DaysInterval,  StartHour, StartMinute, RepeatInterval, RepeatDuration
}
"Process complete."
}


Input File
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ContentSource1
ContentSource2

Start Pools or Websites if found in Stopped State

#***** Start the Pools if found in Stopped State
Import-Module WebAdministration
$poolNames = Get-SPWebApplication | select @{Expression={$_.ApplicationPool.name};label="PoolName"}
foreach($i in $poolNames)
{
Write-host "Application Pool Name:" $i.poolName
$statusNow = Get-WebAppPoolState -name $i.poolName
  if ($statusNow.Value -eq "Stopped")
  {
     Start-WebAppPool $i.poolName
     Write-Host -ForegroundColor Red  $i.poolName "Found in Stopped State : Starting the Pool"
  }
  else
  {
     Write-Host -ForegroundColor white  $i.poolName "Already in Started State."
  }
}


#*****Start the WebSite if found in Stopped State
Import-Module WebAdministration
foreach($i in Get-Website)
{
  if ($i.State -eq "Stopped")
  {
    Start-Website $i.Name
    Write-Host -ForegroundColor Red  $i.Name "Site Found in Stopped State : Starting the WebSite"
  }
  else
  {
     Write-Host -ForegroundColor white  $i.Name "Already in Started State."
  }
}