Generate version for Jenkins project

def majorVersion = 1
def minorVersion = 1

def currentBuild = Thread.currentThread().executable
def scmChanges = 0
currentBuild.parent.getSCMs().scmName.each {
    def process = ['cmd', '/C', "\"cd ${currentBuild.workspace}\\${it == null ? "" : it} & git log --pretty=oneline | find /c /v \"\"\""].execute()
    process.waitFor()
    scmChanges += process.in.text.tokenize("\n")[0].toInteger()
}
def previousBuild = currentBuild.previousBuild
def previousBuildVersion = previousBuild != null ? previousBuild.environment["VERSION"] : "NULL"
def previousBuildAttempt = previousBuild != null ? previousBuild.environment["BUILD_ATTEMPT"] : -1
def buildVersion = "${majorVersion}.${minorVersion}.${String.format( "%d%02d",new Date().month + 1 ,new Date().date )}.${scmChanges}"
def buildAttempt = previousBuildVersion == "${buildVersion}${previousBuildAttempt}" ? previousBuildAttempt.toInteger() + 1 : 0
return [VERSION: "${buildVersion}${buildAttempt}", BUILD_ATTEMPT: buildAttempt]

Rewrite domain to subfolder

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Virtual Directory Rewrite">
                    <match url=".*" />
                    <conditions>
                        <add input="{SUBDOMAINS:{HTTP_HOST}}" pattern="(.+)" />
                    </conditions>
                    <action type="Rewrite" url="{C:1}{REQUEST_URI}" appendQueryString="false" />
                </rule>
            </rules>
            <rewriteMaps>
                <rewriteMap name="SUBDOMAINS">
                    <add key="one.com" value="one" />
                    <add key="two.com" value="two" />
					<add key="three.com" value="three" />
                </rewriteMap>
            </rewriteMaps>
        </rewrite>
    </system.webServer>
</configuration>

Create Azure VM from existing Storage Account

[CmdletBinding()]
param(
    [Parameter(Mandatory=$false)]
    [string]$ResourceGroup,
    [Parameter(Mandatory=$false)]
    [string]$Location,
    [Parameter(Mandatory=$false)]
    [string]$VMName,
    [Parameter(Mandatory=$false)]
    [string]$VMSize,
    [Parameter(Mandatory=$false)]
    [string]$StorageAccountURI,
    [Parameter(Mandatory=$false)]
    [string]$SubscriptionName,
    [Parameter(Mandatory=$false)]
    [string]$VirtualNetworkName
)

Login-AzureRmAccount
Set-AzureRmContext -SubscriptionName $SubscriptionName

$Network = Get-AzureRmVirtualNetwork -Name $VirtualNetworkName `
                                     -ResourceGroupName $ResourceGroup

Try { 
    $NetworkSecurityGroup = Get-AzureRmNetworkSecurityGroup `
        -Name "$VMName-NetworkSecurityGroup" `
        -ResourceGroupName $ResourceGroup `
        -WarningAction Stop `
        -ErrorAction Stop 
}
Catch { 
    $NetworkSecurityGroup = New-AzureRmNetworkSecurityGroup `
        -Name "$VMName-NetworkSecurityGroup" `
        -ResourceGroupName $ResourceGroup `
        -Location $Location 
}

Try { 
    $PublicIp = Get-AzureRmPublicIpAddress `
        -Name "$VMName-PublicNetworkInterface-01" `
        -ResourceGroupName $ResourceGroup `
        -WarningAction Stop `
        -ErrorAction Stop 
} 
Catch { 
    $PublicIp = New-AzureRmPublicIpAddress `
        -Name "$VMName-PublicNetworkInterface-01" `
        -ResourceGroupName $ResourceGroup `
        -Location $Location `
        -AllocationMethod Static 
}

Try { 
    $Nic = Get-AzureRmNetworkInterface `
        -Name "$VMName-PrivateNetworkInterface-01" `
        -ResourceGroupName $ResourceGroup 
} 
Catch { 
    $Nic =  New-AzureRmNetworkInterface `
        -Name "$VMName-PrivateNetworkInterface-01" `
        -ResourceGroupName $ResourceGroup `
        -Location $Location `
        -Subnet $Network.Subnets[0] `
        -PublicIpAddress $PublicIp `
        -NetworkSecurityGroup $NetworkSecurityGroup
}

$VMConfig = New-AzureRmVMConfig `
                -VMName $VMName `
                -VMSize $VMSize `
                -Verbose | `
            Add-AzureRmVMNetworkInterface -Id $Nic.Id | `
            Set-AzureRmVMOSDisk `
                -VhdUri $StorageAccountURI `
                -Name "$($VMName)OSDisk" `
                -CreateOption attach `
                -Windows `
                -Caching ReadWrite

New-AzureRmVM -ResourceGroupName $ResourceGroup `
              -Location $Location `
              -VM $VMConfig `
              -Verbose

Mirror two Git repositories

git clone --mirror https://source.repository
cd source.repository.git
git push --mirror https://destination.repository

Clean internet explorer

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [string]$Name,
    [Parameter(Mandatory=$true)]
    [string]$User,
    [Parameter(Mandatory=$true)]
    [string]$ActionPath,
    [Parameter(Mandatory=$true)]
    [string]$ActionArguments
)

$Service = New-Object -ComObject("Schedule.Service")
$Service.Connect()
$RootFolder = $Service.GetFolder("\")

$TaskDefinition = $Service.NewTask(0) 
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$TaskDefinition.Settings.AllowHardTerminate = $true

$Action = $TaskDefinition.Actions.Create(0)
$Action.Path = $ActionPath
$Action.Arguments = $ActionArguments

$RootFolder.RegisterTaskDefinition($Name,$TaskDefinition,6,$User,$null,3) | Out-Null

($RootFolder.GetTask($Name)).Run($null) | Out-Null
$RootFolder.DeleteTask($Name,0)

Prev...Next