Powershell-With-IIS


Before moving to deployment topic let me clarify few thing that will help you create thought
process and answer certain question that raised in your mind like

Why we need powershell for deployment?
Powershell scripts basically allows you to take advantage of .NET libraries
and write own scripts which are almost as powerful as the .NET code itself.
You can do many powerful operations like copy dll from one location to other, call external DLLs,
use .NET namespaces like System.IO, System.Net, run processes and intercept output, call web services etc.
You can also IIS web sites and app pool with proper .net framework version.
You can manage to install windows services, stop and start operation with poweshell scripts.
The possibilities are endless.

Option if you dont have powrshell.
-You have to right your batch file, batch is helpful with certain extent,
with batch file you can only mananged to copy/paste and few other operation. you can use .net objects,
To use .net objects you have to right your own .net code or .NET libraries and call it from batch script file.

But, Is this right approach to proceed?
-You can customize TFS. for this you have to learn workflow, Integration TFS with .net framework code blocks.

Why we need to automate deployment process?
Make your deployment process reliable, fast and repeatable. See the status of your
deployments with the shared central dashboard, and manage all your deployments in one place.
-This is one time process.
-Deployment Automation Patterns are generally reusable solutions.
-Reliable & Powerful
-Eliminate the risk in deploying manually to live systems
-Spend less time on setup and custom scripting for deployments
-See on the dashboard which versions of your software are deployed to each server
-Ensure that deployments over the Internet are secure, with public-key encryption
-Get immediate confirmation that your deployment has succeeded
-NEW: Deploy SQL Server databases alongside your application

Note: Back in the days before automated deployment, we could spend months on one release.

Deployment Script

Deploying Web Service using MSDeploy & Powershell

Powershell Script for Web Service Deployment

Please verify the Microsoft Web Deploy v3 is installed

Example

    
        $ServerName = "ServerName"
        $UserName = "Domain\User"
        $Password = "Password"
        # package directory First we list all the folders using 
        # Get-ChildItem and then sort them in descending order and get the first item. 
        #This first item is our last build that was queued
        $latestbuildfolder = Get-ChildItem "C:\AppBuilds\" | Sort-Object 
        LastWriteTime -Descending | SELECT-Object -First 1

        # create destination path on Local machine or build server
        Copy-Item "$latestbuildfolder\*" "C:\Inetpub\wwwroot\website" -recurse -force

        # create destination path on Depoyment server
        $DestinationServicePath = "C:\Inetpub\wwwroot\website,
        computername=$ServerName,username=$UserName,password=$Password"

        if (-not (Test-path $DestinationServicePath -PathType Container)){
               New-Item $DestinationServicePath -type directory
        }

        # create msdelpoy arguments and deploy the web services, 
        # using MSDeploy to deploy the web service in the IIS.
        [string[]]$WebSiteMSdeployArgs = @("-verb:sync", 
               "-source:contentPath=C:\Inetpub\wwwroot\website",
               "-dest:contentPath=$DestinationServicePath",
               "-verbose"
        )
        & 'C:\Program Files\IIS\Microsoft Web Deploy v3\msdeploy.exe' $cvgmsdeployArgs
        # pass the path to MSDeploy commandline utility to deploy to IIS web server 
        # and Please verify the Microsoft Web Deploy v3 is installed

Copy and Try it

Clean IIS Resources

Example

#requires -version 2.0

[CmdletBinding()]
param
(
)

$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest


#$ResxFolder = "E:\Program Files\Folder\wwwroot"                       
$channelFolders = get-childitem -path "$ResxFolder\Resources"
foreach($channelFolder in $channelFolders)
{
    if(($channelFolder.Name.ToUpper() -eq "FolderName1") -or ($channelFolder.Name.ToUpper() -eq "FolderName2"))
    {
        $channelDirInfo = Get-childItem $channelFolder.FullName | Measure-Object
        if($channelDirInfo.count -ne 0)
        {      
            Get-childitem -Path $channelFolder.FullName -exclude ABCFolder,XYZFolder | remove-item -recurse -force
        }     
    }
    else
    {
        Remove-Item -Recurse -Force $channelFolder.FullName
    }
}

Copy and Try it