Wednesday, July 22, 2015

How to Create Web site and Application pool in IIS using PowerShell scripting?

In IIS Web Site/Application Pool creation task automation and configuration using Powershell scripting:

You can easily create Web application (web site) and new application pool in IIS with the use of PowerShell scripting. Here with I am sharing the code for your reference.

Before creating the application pool you need to verify the app pool name in the IIS. The App Pool name not available in the Application pool means you need to create new App pool otherwise no need to create. For that the below code will help you to check app pool name exist or not using the PowerShell scripting.

                #navigate to the app pools root
                cd IIS:\AppPools\

                #check if the app pool exists
                if (!(Test-Path $iisAppPoolName -pathType container))
                {
                        .....
                 }



The same way before creating any Application (web site) name in the IIS you need to check if already exist or not. Please find the below code for that.

                #navigate to the sites root
                cd IIS:\Sites\

                #check if the web site exists
                if (Test-Path $iisAppName -pathType container)
                {
                #New-WebApplication -Name $iisAppName -Site 'Default Web Site' -PhysicalPath $directoryPath -ApplicationPool $iisAppPoolName
                }



The below completed code will run in the Try Catch block in PowerShell for preventing the error message and catch the error messages. First I have checked the App pool name and if not exist I have created the new Application Pool name and set identity values.

Sample Code: 

#cls
echo 'Web Site Configuration Started...'

Import-Module WebAdministration
try
{
                $iisAppPoolName = "MyTestAppPool"
                $iisAppPoolDotNetVersion = "v2.0"
                $iisAppName = "MyReport"
                $UserName=""
                $Password=""              
                $identitytype=2                           #A4Lidentitytype(0=LocalService,1=LocalSystem,2=NetworkService,3=ApplicationPoolIdentity)



                #navigate to the app pools root
                cd IIS:\AppPools\

                #check if the app pool exists
                if (!(Test-Path $iisAppPoolName -pathType container))
                {
                                #create the app pool
                                $appPool = New-Item $iisAppPoolName
                                $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
                             
                                #Configure Application Pool Identity to particular the username and password
                                if($UserName)
                                {
                                                Set-ItemProperty IIS:\AppPools\$iisAppPoolName -name processModel -value @{userName=$UserName;password=$Password;} #identitytype=3
                                }
                                else
                                {
#Configure Application Pool Identity
                                                                Set-ItemProperty IIS:\AppPools\$iisAppPoolName -name processModel -value @{identitytype=$identitytype}
                                }
                                write-host $iisAppPoolName " AppPool Created Successfully." -foregroundcolor green
                }

                #navigate to the sites root
                cd IIS:\Sites\

                #check if the web site exists
                if (Test-Path $iisAppName -pathType container)
                {
                #New-WebApplication -Name $iisAppName -Site 'Default Web Site' -PhysicalPath $directoryPath -ApplicationPool $iisAppPoolName
                }
}
catch [Exception]
{
    "Application failed:`n`n " + $_.Exception
}
finally
{
cd c:
}



The above code will check in the application pool the required app pool name available or not. If not available then it will create new one with user identity and verify the web site name.

Monday, May 25, 2015

Use Full links for ASP.NET and C# .Net


Here with i am sharing the list of link for learning ASP.Net,C# and .Net related for understanding more details of the concept.




What is the difference between Web Farm and Web Garden?

http://www.codeproject.com/Articles/114910/What-is-the-difference-between-Web-Farm-and-Web-Ga

Exploring Session in ASP.NET
http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net


Beginner’s Guide: How IIS Process ASP.NET Request
http://abhijitjana.net/2010/03/14/beginner%E2%80%99s-guide-how-iis-process-asp-net-request/

How to create simple Unit Test application in C#.Net
https://msdn.microsoft.com/en-us/library/ms182532.aspx

Tuesday, July 22, 2014

How to get SQL Server IP Address in SQL Query?

Get IP Address of SQL Server using SQL Query:

I have a situation to get the SQL Server IP Address. My SQL Server was in different IP Address and my web application was in different address. Some time we need to get both SQL Client and SQL Server Local IP Address. The below Example will help me to get the both client and sql server IP’s.
@@SPID – This is the session ID of particular connection. Each connection have different session ID.


SELECT   client_net_address,local_net_address, @@SPID as SPID
FROM sys.dm_exec_connections
WHERE Session_id = @@SPID;


I hope the above SQL Query will help you to get the SQL Server IP Address.

If you want to learn more ablut @@SPID Please check the below link

http://sqlserverplanet.com/dba/spid-what-is-it

Wednesday, March 26, 2014

How to Select/Display/List tables from MS SQL Server 2008 R2 ?

Some time we need to select list of user table in the Database to identify the table list. For that we have different SQL querys to get display the Table list. Here I am give some small code to display the user table in different SQL query language.


List out the Data Base Tables detail:

SELECT * FROM sys.Tables

SELECT * FROM INFORMATION_SCHEMA.TABLES


SELECT sc.name +'.'+ ta.name TableName ,SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa  ON pa.OBJECT_ID = ta.OBJECT_ID
INNER JOIN sys.schemas sc ON ta.schema_id = sc.schema_id
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
GROUP BY sc.name,ta.name ORDER BY SUM(pa.rows) DESC



How to Select / Display / List Views in the MS SQL Server 2008 R2:

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE= 'view'



How to Select / Display / List Table Columns in the MS SQL Server 2008 R2:

SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='MyTable'



Thursday, February 13, 2014

How to check if stored procedure exists or Not in SQL server 2008R2 ?

Drop and Create Stored Procedure in SQL before Creating any SP:


In my project I need to give some Stored Procedure to client system to run some bug fixing. The client environment already have the same SP name. So we can give directly ALTER SP query to execute in the SQL server. But that not a good practices. As a good developer need to think future also. What happen if new client dos not have the Stored Procedure in that system? It will give error message. So avoiding the Error Message we need to add little bit code before the SP.

Before creating SP or deleting any Stored Procedure we need to check if that SP already exist or not in the Database. If Exist DROP the procedure and recreate again. If not exist that SP means don’t Drop the SP just create new Stored Procedure. This is the Good way to Creating Script in the SQL server. Below example first I have checked that SP name already Exist in the DB or Not. If exist I will DROP the Procedure and after that I will create New SP. If Not exist that SP name means it will not run the Drop function.

Drop Stored Procedure if exist in SQL Server and Create New one:

--My_SP_Name - Stored Procedure Name


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[My_SP_Name]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[My_SP_Name]
GO


CREATE PROCEDURE [dbo].[My_SP_Name]
@STARTDATE DATETIME,
@ENDDATE DATETIME
AS

GO





I hope the above code will help you to make good SQL script wit out error free script running. This is not only for SPs, You can use same method for Create Table also.

Wednesday, January 1, 2014

How to Restart IIS in command line

          Some time we need to restart the IIS to certain configuration changes take effect in the IIS server. There are different methods available to reset or restart the IIS. The below example will help you to restart IIS in command line.

How to restart IIS in command line:
          From the Start menu, click Run and type iisreset . This is the simplest method to restart the IIS. The below list will help you to do other works in IIS.

To restart IIS using the IIS Restart command-line utility
1. From the Start menu, click Run.
2. In the Open box, type cmd, and click OK.
3. At the command prompt, type iisreset , and press ENTER.
The below examples for other way of managing IIS
iisreset /stop – Stop the IIS services
iisreset /start – Start the IIS services
IISreset /restart - Stop and then restart all Internet services.
iisreset /status - will show the current status of IIS
iisreset /noforce - will prevent the server from forcing close applications and process. This can cause IIS to reset slower but is more graceful.
iisreset /disable - this command disables IIS and prevents all iisreset calls from executing
iisreset /enable - to re-enable the IIS Admin service
iisreset /REBOOT - Reboot the computer.
iisreset /REBOOTONERROR - Reboot the computer if an error occurs when starting, stopping, or restarting Internet services.
iisreset /NOFORCE - Do not forcefully terminate Internet services if attempting to stop them gracefully fails.

Saturday, September 28, 2013

Simple string encryption and decryption function in SQL

Simple Password encryption / decryption SQL function:

In the SQL Query we can easily do Encryption and Decryption. Mostly we use to encrypt password column value for the security purpose.  For the SQL Encryption the below function will help you to easy encrypt and decrypt your value.

The below function for Encrypt the user value using SQL Encryption. The ‘Your Key’ value is for the encrypt KEY. The same key you need to use for the decryption also.



CREATE FUNCTION [dbo].[FN_ENCRYPTION]
(
            @INPUT VARCHAR(MAX)
)
            RETURNS VARBINARY(MAX)
            AS BEGIN
            DECLARE @OUTPUT VARBINARY(MAX)
            SELECT @OUTPUT = ENCRYPTBYPASSPHRASE('Your Key',@INPUT)
            RETURN @OUTPUT
END

Example :
SELECT [dbo].[FN_ENCRYPTION] ('vijay')
Result: 0x010000008A2D548FFD5D193EE01FF10DA1356C7D858ADAAA2A014A4F


The below function will decrypt the encrypted binary value to normal string value. The same encryption KEY you need to pass for the decryption.



CREATE FUNCTION [dbo].[FN_DECRYPTION]
(
            @INPUT VARBINARY(MAX)
)
            RETURNS VARCHAR(MAX)
            AS BEGIN
            DECLARE @OUTPUT VARCHAR(MAX)
            SELECT @OUTPUT = DECRYPTBYPASSPHRASE(Your Key ',@INPUT)
            RETURN @OUTPUT
END


Example :
SELECT [dbo].[FN_ENCRYPTION] ('vijay')
Result: 0x010000008A2D548FFD5D193EE01FF10DA1356C7D858ADAAA2A014A4F

SELECT [dbo].[FN_DECRYPTION] (0x010000008A2D548FFD5D193EE01FF10DA1356C7D858ADAAA2A014A4F)
Result: vijay