Sunday, November 13, 2011

Fill factor explained

The Fill Factor specifies the % of fullness of the leaf level pages of an index. When an index is created or rebuilt the leaf level pages are written to the level where the pages are filled up to the fill factor value and the remainder of the page is left blank for future usage. This is the case when a value other than 0 or 100 is specified. For example, if a fill factor value of 70 is chosen, the index pages are all written with the pages being 70 % full, leaving 30 % of space for future usage.

You might choose a high fill factor value when there is expected little to no change on the underlying table. High fill factor value creates an index smaller in size and the queries on the underyling table can retrieve data with less disk I/O operations since there are less pages to read. But if you have an index that changes frequently, you want to have a lower value to keep some free space available for the new index entries. otherwise, if fill factor is large and there is little free space in the indexes, SQL Server would have to do a lot of page splits to fit the new values into the index pages.

the lower fill factor of your index, the larger the index size. if we take a fill factor of 50% increases index size to about 2 times the size of an index with fill factor of 100%.

With new data added to the table, the index page needs to have sufficient space to take the new entries. Where there is not enough space, a page split needs to take place, therefore impacting the performance.

Friday, November 11, 2011

Powershell and Active Directory: Moving user to another OU

Given a username passed as a parameter to the script, the code below will find the specified user in AD and move to the specified OU


$UserName = $args[0]

$root=[ADSI]''

$searcher = new-object System.DirectoryServices.DirectorySearcher($root)

$searcher.filter = "(&(objectClass=user)(sAMAccountName= $UserName))"

$User = $searcher.findone()

# Binding the user account to $AUser and the OU to move to to $MovetoOU
Write-Host $User.Properties.adspath

$ADSPath = $User.Properties.adspath

$MoveToOU = [ADSI]("LDAP://OU=NewOU,DC=mydomain,DC=com")

$AUser = [ADSI]("$ADSPath")


# Command to Do the actual move
$AUser.PSBase.moveto($MoveToOU)

Running PowerShell script with parameters

When running powershell from a command line and passing it one or more parameters, parameters passed in should be separated by a space and if any of the parameters contain spaces, they should be wrapped in double quotes:


C:\> .\MyPScript.ps1 "this is foirst parameter" "this is second"


And when accessing command line arguments from within the script, use args array with subscripts:


$param1=args[0]
$param2=args[1]

Monday, September 26, 2011

AD Security Groups and Creating Folders on a Share with Powershell

The below script is used to loop through all security groups in a particuler OU in Active Directory and create a folder on the share for each group and then, inside that folder, create a separate folder for each member of that group.



function CreateSubdirectory($path)
{
New-Item -ItemType Directory -Path $path

}


foreach ($group in get-adgroup -searchbase "OU=MyOU,DC=mydomain,DC=com" -filter *){

#create a subfolder $group.name
$sharepath="\\SERVER\SHARE" + $group.name
CreateSubdirectory $sharepath

foreach ($member in ($group.member | sort))
{
#create a folder for each memeber $member.name
CreateSubdirectory $sharepath + $member.name

}
}

Friday, September 23, 2011

Moving AD User to another OU using different credentials

In order to move Active Directory User to another OU you will need a service account with priviledges to move AD objects. Below is VBScript that worked for me:


Const ADS_SECURE_AUTHENTICATION = 1
sADDomain = "mydomain"
sADUser = "serviceuser"
sADPassword = "mypassword"

sDestOU = "LDAP://OU=My Group,DC=mydomain,DC=net"
Set objRootDSE = GetObject("LDAP:")
Set objSysInfo = CreateObject("ADSystemInfo")
Set objDestOU = objRootDSE.OpenDSObject(sDestOU, sADDomain & "\" & sADUser, sADPassword, ADS_SECURE_AUTHENTICATION)

Set objUser = objRootDSE.OpenDSObject("LDAP://" & objSysInfo.UserName, sADDomain & "\" & sADUser, sADPassword, ADS_SECURE_AUTHENTICATION)

objDestOU.MoveHere "LDAP://" & objUser.distinguishedName, vbNullString
If Err.Number = 0 Then
MsgBox "User moved successfully."
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
Err.Clear
End If

Thursday, September 22, 2011

Running Powershell script with spaces in the name from VBScript

Running Powershell script from VBScript is rather simple:


Set objShell=CreateObject("Wscript.Shell)
sCmd="C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe C:\test.ps1"
objShell.Run sCmd, 1, true


But what to do when the Powershell script file has spaces in the name, like C:\My Path\test.ps1?

Theanswer is to use ampersand and single quotes:


Set objShell=CreateObject("Wscript.Shell)
sCmd="C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe " & Chr(34) & "& 'C:\script with spaces.ps1'" & Chr(34)
objShell.Run sCmd, 1, true


or


Set objShell=CreateObject("Wscript.Shell)
sPSPath="C:\script with spaces.ps1"
sCmd="C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe " & Chr(34) & "& '" & sPSPath & "'" & Chr(34)
objShell.Run sCmd, 1, true

Friday, September 16, 2011

Active Directory container vs. organizational unit

Active directory container is a built-in container that comes with AD and they cannot be altered to suit your organizational needs, without altering the AD schema. (Examples: Computers and Users containers).

An OU or Organizational Unit is a special purpose container created by the user and to which administrators can apply group policy. Group policy cannot be applied to a container. An organizational unit is a subdivision within an Active Directory into which you can place users, groups, computers, and other organizational units. You can create organizational units to suit your organization's functional or business structure. Each domain can implement its own organizational unit hierarchy.