Tuesday, March 26, 2013

Validate the existence of user account in AD using Powershell


Check for user AD account existing with PowerShell script.
Once I needed to check if AD user account exists on server and is enabled.
There are two option to do that.
The first option is to use  Get-ADUser.
Make sure you install the module Active Directory.  


How to install and use Active Directory Module
http://blogs.technet.com/b/heyscriptingguy/archive/2011/08/30/install-active-directory-management-service-for-easy-powershell-access.aspx

The following example shows how to use the function:

Import-Module ActiveDirectory

$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value ;

$SplitUserName = ($AccountName.Split("\")[1])
$UserExists = Get-ADUser -Filter {sAMAccountName -eq $UserName}
$UserName = $splitusername;
if ($UserExists -eq $null)
{
      Write-Host "User $AccountName does not exist in AD "
}
else 
{
   if (!$UserExists.Enabled)
     {
       Write-Host "User $AccountName is disabled"
     }
     Else
     {
        Write-Host "AD account  $AccountName is ok";
     }
 }
In my case I didn’t have the module Active Directory installed on production server, so I need to find the second way. I couldn’t use the function Get-ADUser from module ActiveDirectory. My opinion is to better to install the module because it has a lot of usefull functions that will makes yourlife easear. I didn’t have such opotunaty because installing module means to restart production server, but this approach I needed to escape, so I found the second option and wrote function.

# function Check-ADUser gets user name as a parametr
# return two properties:
# Status
#              return "0" if AD account doesn't exist in Active directory or was deleted
#              return "1" if the user exists.
# AccountEnable
#              return "0" if AD account is disabled
#          return "1" if AD account is enabled
#Example:
# $UserStatus = (Check-ADUser -Username "testuser1").Status;
# $UserAccountEnabled = (Check-ADUser -Username "$SplitUserName").AccountEnable;
function Check-ADUser
{
Param ($Username)

    $ADRoot = [ADSI]''
    $ADSearch = New-Object System.DirectoryServices.DirectorySearcher($ADRoot);
    $SAMAccountName = "$Username";
$ADSearch.Filter = "(&(objectClass=user(sAMAccountName=$SAMAccountName))";
    $Result = $ADSearch.FindAll();
     
      $Status = "-1";
      $Enabled = "-1";
    if($Result.Count -eq 0)
    { # "No such user on the Server"
        $Status = "0";
    }
    Else
    { #"User exist on the Server"
        $Status = "1";
            foreach ($objResult in $Result)
      {
            $objResult = $objResult.GetDirectoryEntry()
             if ($objResult.accountdisabled)
             {
             #"Account diabled"
             $Enabled = "0";
      }
            else
            {  # "Account enabled"
            $Enabled = "1";
            }
           
      }
    }
    $Results = New-Object Psobject
    $Results | Add-Member Noteproperty Status $Status
      $Results | Add-Member Noteproperty AccountEnable $Enabled
    Write-Output $Results   
}

Example how to use the function Check-ADUser:

$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName]
.Value 
$SplitUserName = ($AccountName.Split("\")[1])
$UserName = $splitusername;
# check if user exists in AD
$UserExists = (Check-ADUser -Username $SplitUserName).Status;
$UserAccountEnabled = (Check-ADUser -Username $SplitUserName).AccountEnable;

if ($UserExists -ne 1)
 {
   #"User does not exist in AD"
 }
else
 {
   if ($UserAccountEnabled -eq 0)
    {
             #"Account is disabled"
    }
    else
    {
           #"Account is ok"
     }

I hope you will help this information.
Have a good day. :)

Tuesday, February 12, 2013

Create SharePoint Site Quota Templates using PowerShell

Original article

Here you can find PowerShell script that creates and changes Quota Template for Site Collection. You can find full original article here

Create SharePoint Site Quota Templates using PowerShell


function New-SPQuotaTemplate {
<#
    This advanced function creates a new Site Quota Template.
    This function uses .NET code to instantiate an instance of an
    SPQuotaTemplate class. Once the object is created, an instance of the
    SPWebService class is instantiated and the Quota Template is added to the
    Quota Templates Collection.
.Example
    C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 2GB -StorageWarningLevel 1GB -UserCodeMaximiumLevel 100 -UserCodeWarningLevel 75
    This example creates an SP Quota Template called Custom with a maximum size
    of 2GB and a warning size of 1GB. Sandboxed solutions are
    limited to 100, with a warning level of 75.
.Example
    C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 4GB -StorageWarningLevel 3GB
    This example creates an SP Quota Template called Custom with a maximum size
    of 4GB and a warning size of 3GB
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String]$Name,
[Parameter(Mandatory=$true)][Int64]$StorageMaximumLevel,
[Parameter(Mandatory=$true)][Int64]$StorageWarningLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeMaximumLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeWarningLevel
)
# Instantiate an instance of an SPQuotaTemplate class #
Write-Host "Instantiating an instance of an SPQuotaTemplate class"
$Quota = New-Object Microsoft.SharePoint.Administration.SPQuotaTemplate
# Set the Properties #
Write-Host "Setting properties on the Quota object"
$Quota.Name = $Name
$Quota.StorageMaximumLevel = $StorageMaximumLevel
$Quota.StorageWarningLevel = $StorageWarningLevel
$Quota.UserCodeMaximumLevel = $UserCodeMaximumLevel
$Quota.UserCodeWarningLevel = $UserCodeWarningLevel
# Get an Instance of the SPWebService Class #
Write-Host "Getting an instance of an SPWebService class"
$Service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
if ($Service.QuotaTemplates[$Name] -eq $null )
{
# Use the Add() method to add the quota template to the collection #
Write-Host "Adding the $($Name) Quota Template to the Quota Templates Collection"
$Service.QuotaTemplates.Add($Quota)
# Call the Update() method to commit the changes #
$Service.Update()
}
else
{
Write-Host "Quota Template $Name exists"
}
}

To change the quota template for a site collection by using Windows PowerShell

function Set-SPQuotaTemplate ($SiteCollectionName,$QuotaTemplateName)
{
    Set-SPSite -Identity $SiteCollectionName -QuotaTemplate $QuotaTemplateName 
Write-Host "Quota template $QuotaTemplateName was set to site collection $SiteCollectionName"
}

Tuesday, January 8, 2013

Display more than 50 items in SharePoint menu



Recently I had a ticket where SahrePoint site owner wanted to add more then 51 sites to the SharePoint and the problem was that 51th site wasn't showed in the Left navigation and Site Setting > Navigation.

After I changed the web.config of the applications everything work, so please post below to resolve the issue.

SharePoint navigation is based on the ASP.NET 2 SiteMap Provider. By default the limit is 50 items per menu.


1. Open web application  web.config in C:\inetpub\wwwroot\wss\VirtualDirectories\<yourwebapp>
2. If you need to change the limit, justadd the DynamicChildLimit attribute to the GlobalNavSiteMapProvider, CombinedNavSiteMapProvider, CurrentNavSiteMapProvider and CurrentNavSiteMapProviderNoEncode nodes and specify the limit e.g. 100:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
  ...
  <system.web>
    ...
    <siteMap defaultProvider="CurrentNavSiteMapProvider" enabled="true">
      <providers>
        ...
        <add name="GlobalNavSiteMapProvider" ... DynamicChildLimit="100" />
        <add name="CombinedNavSiteMapProvider" ... DynamicChildLimit="100" />
        <add name="CurrentNavSiteMapProvider" ... DynamicChildLimit="100" />
        <add name="CurrentNavSiteMapProviderNoEncode" ... DynamicChildLimit="100" />
        ...
    </providers>
    </siteMap>
    ...
  </system.web>
  ...
</configuration>

3. Restart iis




Thursday, December 13, 2012

SharePoint 2010 Feature activation and disabling with PowerShell script

The script activates a feature with ID 00BFEA71-EB8A-40B1-80C7-506BE7590102 at a web site under “YourServer/Sites/” site collection

Feature activation:

Enable-SPFeature "00BFEA71-EB8A-40B1-80C7-506BE7590102" -Url http://YourServer/Sites/testsite

Feature disabling:

Disable-SPFeature "00BFEA71-EB8A-40B1-80C7-506BE7590102" -Url http://YourServer/Sites/testsite

Note!!!

The feature must be already deployed and installed, the scripts just activating  and disabling the feature.

Friday, November 9, 2012

SharePoint 2010, MS Office and Windows 7



I came across with issue in SharePoint 2010 and Windows 7.

There is a feature when SharePoint user can open a document from Document Library for editing and click on "Save as.." to save the document directly to the library  The problem is that the user gets the window to enter his credentials, but after entering his username and password, the user cannot pass the security and save the document.

After some research I found many post about this issue, I took most information from Microsoft link, and made instruction with images for this. I tried many times in different computers and it helped me.

More details about the issue you can find here:

http://support.microsoft.com/?id=943280

Here is a step by step instruction that helped me:


HOTFIX:  http://support.microsoft.com/?id=943280

SYMPTOMS

You are prompted to enter your credentials, even though the user account that you are using has sufficient permission to access this site.
For example, when you open a Microsoft Office file from a Microsoft Office SharePoint site by using 2007 or 2010 Microsoft Office, or after you opened document from Document Library in SharePoint 2010 and try to Save as, or you (user) try to edit document and enter the hyperlink in Excel file cell on a Windows Vista-based or Windows 7 client computer, you are prompted for authentication.

RESOLUTION
Restart requirement
You have to restart the computer after you apply this .
Registry information
To use this hotfix, you have to modify the registry.
! Make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry.
! ( for more info read http://support.microsoft.com/?id=943280)
Follow these steps:
1. Click Start, type regedit in the Start Search box, and then press ENTER.



2. Locate and then click the following registry subkey
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WebClient\Parameters





3. On the Edit menu, point to New, and then click Multi-String Value.




4. Type AuthForwardServerList, and then press ENTER.



5. On the Edit menu, click Modify



6. In the Value data box, type the URL (http://*.yourcompany.com) of the server that hosts the Web share, and then click OK.

Note: You can also type a list of URLs in the Value data box. For more information, see the "Sample URL list" section in this article.

7. Find BasicAuthLevel registry key  and change Value data from 1 to 2.

Note The mapping is as follows:
0 - Basic authentication disabled
1 - Basic authentication enabled for SSL shares only
2 or greater - Basic authentication enabled for SSL shares and for non-SSL shares


8. Exit Registry Editor.

Note: You have to restart the WebClient service after you modify the registry.

1. Click Start, type Services in the Start Search box, and click on Services.


2. Find WebClient and click on Restart



3. Restart your computer.

Know you could save document directly to SharePoint Document Library:



Note: If you cannot see the Document Library as at last picture, check User Accounts > Credential Manager > Windows Credentials. There should be SharePoint AD User Account, if no create it.

I hope this information will help you.