Thursday, March 22, 2012

Get Random string function for SQL

Most of the time we need a function in sql server to add random string in any field.

So I have created a function, which accepts the guid and generates the random string form that.



IF OBJECT_ID (N'dbo.get_RandomChar', N'FN') IS NOT NULL
    DROP FUNCTION dbo.get_RandomChar;
GO
-- Function to generate the random char, if the char is less then A then use A and if it is greater then z then use Z.
CREATE FUNCTION dbo.get_RandomChar(@newguid varchar(255))
RETURNS varChar(10)
WITH EXECUTE AS CALLER
AS
BEGIN
    declare @NewStr varchar(25)
set @NewStr =''
declare @Randomint int 
set @Randomint =ABS(CAST(@newguid AS binary(10)) %100)
-- not in A to Z and a to Z  _ - space and *
if not((@Randomint >= 65 and @Randomint <=90 ) or (@Randomint >= 97 and @Randomint <=122 ) or @Randomint=95 or @Randomint=45 or @Randomint =32 or @Randomint =42)
Begin
if (@Randomint <65)
set @Randomint =65
else
set @Randomint =122
End
set @NewStr =@NewStr+ convert(varchar,CHAR(@Randomint))
return(@NewStr);
END;
GO

-- function to create random string of 10 chars.
IF OBJECT_ID (N'dbo.get_RandomString', N'FN') IS NOT NULL
    DROP FUNCTION dbo.get_RandomString;
GO
CREATE FUNCTION dbo.get_RandomString(@newguid1 varchar(255),@newguid2 varchar(255),@newguid3 varchar(255),@newguid4 varchar(255),@newguid5 varchar(255),@newguid6 varchar(255),@newguid7 varchar(255),@newguid8 varchar(255),@newguid9 varchar(255),@newguid10 varchar(255))
RETURNS varChar(10)
WITH EXECUTE AS CALLER
AS
BEGIN
    return(dbo.get_RandomChar(@newguid1) +dbo.get_RandomChar(@newguid2)+dbo.get_RandomChar(@newguid3)+dbo.get_RandomChar(@newguid4)+dbo.get_RandomChar(@newguid5)+dbo.get_RandomChar(@newguid6)+dbo.get_RandomChar(@newguid7)+dbo.get_RandomChar(@newguid8)+dbo.get_RandomChar(@newguid9) +dbo.get_RandomChar(@newguid10));
END;
GO





to use this function call  like get_RandomString(NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID()) 

most of the time when we do data scramble we need such function.




Get Random string function for SQL

Most of the time we need a function in sql server to add random string in any field.

So I have created a function, which accepts the guid and generates the random string form that.



IF OBJECT_ID (N'dbo.get_RandomChar', N'FN') IS NOT NULL
    DROP FUNCTION dbo.get_RandomChar;
GO
-- Function to generate the random char, if the char is less then A then use A and if it is greater then z then use Z.
CREATE FUNCTION dbo.get_RandomChar(@newguid varchar(255))
RETURNS varChar(10)
WITH EXECUTE AS CALLER
AS
BEGIN
    declare @NewStr varchar(25)
set @NewStr =''
declare @Randomint int 
set @Randomint =ABS(CAST(@newguid AS binary(10)) %100)
-- not in A to Z and a to Z  _ - space and *
if not((@Randomint >= 65 and @Randomint <=90 ) or (@Randomint >= 97 and @Randomint <=122 ) or @Randomint=95 or @Randomint=45 or @Randomint =32 or @Randomint =42)
Begin
if (@Randomint <65)
set @Randomint =65
else
set @Randomint =122
End
set @NewStr =@NewStr+ convert(varchar,CHAR(@Randomint))
return(@NewStr);
END;
GO

-- function to create random string of 10 chars.
IF OBJECT_ID (N'dbo.get_RandomString', N'FN') IS NOT NULL
    DROP FUNCTION dbo.get_RandomString;
GO
CREATE FUNCTION dbo.get_RandomString(@newguid1 varchar(255),@newguid2 varchar(255),@newguid3 varchar(255),@newguid4 varchar(255),@newguid5 varchar(255),@newguid6 varchar(255),@newguid7 varchar(255),@newguid8 varchar(255),@newguid9 varchar(255),@newguid10 varchar(255))
RETURNS varChar(10)
WITH EXECUTE AS CALLER
AS
BEGIN
    return(dbo.get_RandomChar(@newguid1) +dbo.get_RandomChar(@newguid2)+dbo.get_RandomChar(@newguid3)+dbo.get_RandomChar(@newguid4)+dbo.get_RandomChar(@newguid5)+dbo.get_RandomChar(@newguid6)+dbo.get_RandomChar(@newguid7)+dbo.get_RandomChar(@newguid8)+dbo.get_RandomChar(@newguid9) +dbo.get_RandomChar(@newguid10));
END;
GO





to use this function call  like get_RandomString(NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID()) 

most of the time when we do data scramble we need such function.




Wednesday, December 21, 2011

How to check Site collection on Content DB or Content DB from Web Application in SharePoint 2010


There is Power shell command by which you can get the details of Site collection listed in a content db.

 

 

List of Site collection from Content DB Name

 

PS C:\Users\Administrator > Get-SPsite -ContentDatabase WSS_Content

 

Result of this command will be like

 

Url                                                   

---                                                   

http://<Server Name>                                  

http://<ServerName>/sites/SearchCenter    

 

 

List of content DB  from Site URL

 

PS C:\Users\Administrator > Get-SPContentDataBase  -WebApplication http://<Server Name>

 

Result of this command will be like

 

 

Id                                   : 6535eccc-0189-43d4-b022-5123e4ee8507

Name                             : WSS_Content

WebApplication               : SPWebApplication Name=SharePoint - 80

Server                           : ServerName

CurrentSiteCount            : 2


Monday, December 19, 2011

Deploy WSP through PowerShell script in SharePoint 2010

In SharePoint 2010, we have power shell commands to do most of share point functionality.

Here is simple script to Uninstall-remove- add-install for any WSP


<#
Name       : WSPDeployment.ps1
Created by : Rajesh Soni
Created on : 19th Dec 2011
Purpose    : Deployment Script for WSPs,Before executing change all the veriables name defined at the top of ps script
#>

Add-PSSnapin Microsoft.SharePoint.Powershell

$WSPPath ="C:\WSPs\"
$WSPName="abc.wsp"
$WSPNameWithPath=$WSPPath +''+$WSPName
$SiteURL ="http://<URL Site>/"


function WaitForJobToFinish([string]$SolutionFileName)
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null) 
    {
        Write-Host 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"
        
        while ((Get-SPTimerJob $JobFullName) -ne $null) 
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        Write-Host  "Finished waiting for job.."
    }
}


Write-Host "Uninstall Solutions "

Uninstall-SPSolution –Identity $WSPName -confirm:$false -WebApplication $SiteURL  
WaitForJobToFinish  $WSPName

Write-Host "Remove Solutions "
Remove-SPSolution –Identity $WSPName -confirm:$false


Write-Host "Add Solutions "

Add-SPSolution $WSPNameWithPath

Write-Host "Install Solutions "
Install-SPSolution –Identity $WSPName -Force -GACDeployment  -WebApplication $SiteURL  
WaitForJobToFinish  $WSPName

Write-Host -Fore Green "Successfully deployed"


Write-Host -Fore Green "List of solutions at server"

Get-SPSolution 

Remove-PsSnapin Microsoft.SharePoint.PowerShell