Monday, 28 July 2014

PowerShell

Run PowerShell Prompt

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

PowerShell Script File

A PowerShell script is simply a text file with a .ps1 extension that contains a list of commands PowerShell should execute.

Host Version
$host.version


PowerShell Modules

http://technet.microsoft.com/en-us/library/hh847741.aspx

Reusable Functions

Create App Pool in IIS

# create app pool
if(Test-Path IIS:\AppPools\$websiteName)
{
    echo "App pool exists - removing"
    Remove-WebAppPool $websiteName
    gci IIS:\AppPools
}
$pool = New-Item IIS:\AppPools\$websiteName
$pool.processModel.identityType = 4 #ApplicationPoolIdentity
$pool | set-item


Write-Host "app pool created"

Create Website in IIS

$iisApp = New-Item iis:\Sites\$websiteName -bindings @{protocol="http";bindingInformation=":80:$websiteName"} -physicalPath $destinationFolder -force


$iisApp | Set-ItemProperty -Name "applicationPool" -Value $websiteName


Copy File Contents

Copy-Item ($PSScriptRoot + "\Ext\*") $destinationExtFolder -recurse -force

Add to AppSettings

$webConfig = ($interactCollectionsFolder + "\web.config")
$doc = (Get-Content $webConfig) -as [Xml]

$appSettings = $doc.SelectSingleNode("//appSettings")

if ($appSettings -eq $null)
{
    $appSettings = $doc.CreateElement("appSettings")
    $doc.configuration.AppendChild($appSettings)  
}

$key = $appSettings.SelectSingleNode("//add[@name='CsvFilePath']")

if ($key -eq $null)
{
     $key = $doc.CreateElement("add")
     $key.SetAttribute("name", "CsvFilePath")
     $appSettings.AppendChild($key)  
}
$key.SetAttribute("value", $destinationCsvFilePath)

$doc.Save($webConfig)

TransformXml.ps1

#Notes: To transform a web.config or app.config with a Transform Config file:

$XmlTransformAssemblyPath = $PSScriptRoot + "\Lib\Microsoft.Web.XmlTransform.dll"

function TransformXml($xmlPath, $xdtPath)
{
write-host
write-host 'Transforming...' $xmlPath
write-host 'With...' $xdtPath

    Add-Type -LiteralPath "$XmlTransformAssemblyPath"

    $xmlPathdoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;  
    $xmlPathdoc.PreserveWhitespace = $true  
    $xmlPathdoc.Load($xmlPath);  
    $transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdtPath);  
    if ($transf.Apply($xmlPathdoc) -eq $false)
    {
        throw "Transformation failed."
    }
    $xmlPathdoc.Save($xmlPath);

write-host 'Config transformed and saved.'
write-host
}

ExecuteSqlScripts.ps1

#Notes: To execute a script file against a database from web.config or app.config

function ExecuteSqlScripts($connectionString, $sqlScriptsPath)
{
write-host
write-host 'Executing SQL Scripts from ' $sqlScriptsPath
write-host

$sqlScripts = [System.IO.File]::ReadAllLines($sqlScriptsPath)

ExecuteNonQuery $connectionString $sqlScripts

write-host 'SQL Scripts executed successfully.'
write-host
}

function ExecuteNonQuery($connectionString, $sqlScripts)
{    
Try{
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $connectionString
$SqlConnection.Open()

foreach($sqlScriptLine in $sqlScripts)
{
if($sqlScriptLine -ne "GO" -And $sqlScriptLine -ne "go")
{
#Preparation of SQL packet
$sqlCommandText += $sqlScriptLine + "`n"
}
else
{
$SQLCommand = New-Object System.Data.SqlClient.SqlCommand($sqlCommandText, $SQLConnection)
$result = $SQLCommand.ExecuteNonQuery();                  
$sqlCommandText = ""
}
}
}
Catch [System.Exception]
{
Write-Host 'An error occurred: ' +  $_.Exception
}
Finally{
$SqlConnection.Close()
$SqlConnection.Dispose()
}
}

GetConnectionString.ps1

# Gets the connection string value from app.config or web.config based on the name of the connection string.
function GetConnectionString($configPath, $name)
{
$xml = [xml](Get-Content $configPath)

$connectionString = ($xml.configuration.connectionStrings.add | where {$_.name -eq $name})

return $connectionString.connectionString

}

No comments: