Rajnish Noonia

Category: Powershell

  • Host Website programmatically including apppool

    The Powershell reads the sample json file and setup your website based on information in json file..

    #Powershell to create web site host from metadata
    function Get-Service-Name
    {
    param(
    [parameter(Mandatory=$false)]
    [ValidateNotNullOrEmpty()]
    [object] $component
    )
    
    
    function Get-Service-Name
    {
     param(
     [parameter(Mandatory=$false)] 
     [ValidateNotNullOrEmpty()] 
     [object] $component 
     )
     
     return [System.IO.Path]::GetFileNameWithoutExtension($component.solutionPath)
    }
    
    function Get-WebHost-Descriptor
    {
     param(
     [parameter(Mandatory=$false)] 
     [ValidateNotNullOrEmpty()] 
     [object] $component 
     )
     $webDescription = $component.hostDescription;
     
     return $webDescription ;
    }
    
    function Create-WebHost
    {
     param(
     [parameter(Mandatory=$false)] 
     [Object] $component, 
     [bool] $recreateHost = $false
     )
    
     process
     {
     $webDescription = Get-WebHost-Descriptor $component
    
     $websitePath = "IIS:Sites$($webDescription.Name)";
     if ($webDescription.appPool)
     {
     $webAppPoolPath = "IIS:AppPools$($webDescription.appPool.Name)"
     }
    
     if ($recreateHost)
     {
     if (Test-Path $websitePath -PathType Container)
     {
     Write-Verbose "Removing site $($webDescription.Name)"
     Remove-Item $websitePath -Confirm:$false -Force -Recurse 
     if ($webAppPoolPath)
     { 
     Write-Verbose "Removing appPool $($webDescription.appPool.Name)"
     Remove-Item $webAppPoolPath -Confirm:$false -Force -Recurse 
     }
     }
     }
    
     if ($webAppPoolPath -and -Not (Test-Path $webAppPoolPath -PathType Container))
     {
     Write-Verbose "Creating app pool $($webDescription.appPool.Name)"
     $appPool = New-Item $webAppPoolPath
     $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "v4.0"
     if ($webDescription.appPool.User)
     {
     Write-Verbose "Setting app pool $($webDescription.appPool.Name) identity"
     $appPool.processModel.userName = $webDescription.appPool.User;
     $appPool.processModel.password = $webDescription.appPool.Password;
     $appPool.processModel.identityType = 3;
     $appPool | Set-Item
     $appPool.Stop();
     $appPool.Start();
     }
     }
     
     if (-Not (Test-Path $websitePath -PathType Container))
     {
     New-Item -ItemType Directory -Force -Path $webDescription.PhisicalPath > $null
     
     $bindings =$webDescription.Protocols | % {$i=0}{ @{protocol=$_;bindingInformation="$($webDescription.BindingInformations[$i++])"}} | ? {-Not [string]::IsNullOrWhiteSpace($_.bindingInformation)}
     
     Write-Verbose "Creating web site $($webDescription.Name)"
     New-Website -Name $webDescription.Name -Port $bindings[0].bindingInformation.Replace(":","") -PhysicalPath $webDescription.PhisicalPath > $null
    
     #$a = New-Item $websitePath -bindings ($b) -physicalPath $webDescription.PhisicalPath
    
     $bindings | Select -skip 1 | % {New-ItemProperty -path $websitePath -name bindings -value @{protocol=$_.protocol.ToString();bindingInformation=$_.bindingInformation.ToString()}}
     #$bindings | Select -skip 1 | % {New-WebBinding -Name $webDescription.Name -IPAddress "*" -Port $_.bindingInformation.Replace(":","").Replace("*","") -Protocol $_.protocol > $null}
     Set-ItemProperty $websitePath -name EnabledProtocols -Value (($webDescription.Protocols | select -uniq) -join ',')
     if ($webAppPoolPath)
     {
     Set-ItemProperty $websitePath -Name "applicationPool" -Value $webDescription.appPool.Name
     }
     }
    
     $webAppPath = "$websitePath$(Get-Service-Name $component)"
    
     if (-Not (Test-Path $webAppPath -PathType Container))
     {
     Write-Verbose "Creating web app $webAppPath"
     New-Item $webAppPath -physicalPath $webDescription.PhisicalPath -type Application > $null
     if ($webDescription.Protocols -and $webDescription.Protocols.Count -gt 0)
     {
     $protocols = (($webDescription.Protocols | select -uniq) -join ',')
     Write-Verbose "Setting EnabledProtocols on WebApp $protocols"
     Set-ItemProperty $webAppPath -name enabledProtocols -Value (($webDescription.Protocols | select -uniq) -join ',')
     if ($webAppPoolPath)
     {
     Set-ItemProperty $webAppPath -Name "applicationPool" -Value $webDescription.appPool.Name
     }
     }
     }
     return @{Name=$webDescription.Name}
     }
    }
    
    
    # Example to create web site from json defination
    
     [string] $json = '[
     {
     "type": "Web",
     "hostDescription": {
     "BindingInformations": [":323:", "3808:*"],
     "Name": "YourWebsiteName",
     "Protocols": ["http", "net.tcp"],
     "PhisicalPath": "c:\inetpub\wwwroot\",
     "solutionPath": "YourWebDirectoryName",
     "appPool": {
     "Name": "YourAppPoolName",
     "User": "AccountForAppPool",
     "Password": "PasswordForAppPool"
     }
     }
     }
     ]'
    
    $components = ($json | ConvertFrom-Json)
    $firstWebSite = $components[0];
    Write-Host $firstWebSite
    Create-WebHost $firstWebSite , $true