Introduction
I was having an issue accessing a SQL server that didn't have mixed mode enabled, so it required an IAAS box on the domain with the relevant permissions and app pool set.
I found a rather un-documented Powershell cmd that allows you to extend the domain onto a cloud service. With the use of a startup task, you can then get variables and use that in a string
to update the apppool.
This has now been made into a VSTS extension.
Using the Code
The first section adds the cloud service to the domain:
$domain = "FQDN"
$dmuser = "DOMAIN\USERNAME"
$dmpswd = "PASSWORD"
$dmspwd = ConvertTo-SecureString $dmpswd -AsPlainText -Force
$dmcred = New-Object System.Management.Automation.PSCredential ($dmuser, $dmspwd)
Set-AzureServiceADDomainExtension -Service CSNAME -Role "ROLENAME"
-Slot Production -DomainName $domain -Credential $dmcred -JoinOption 35 -Restart
Using Table Storage to get the Username & Password:
function Install-MSIFile {
[CmdletBinding()]
Param(
[parameter(mandatory=$true,ValueFromPipeline=$true,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]$msiFile,
[parameter()]
[ValidateNotNullorEmpty()]
[string]$targetDir
)
if (!(Test-Path $msiFile)){
throw "Path to the MSI File $($msiFile) is invalid. Please supply a valid MSI file"
}
$arguments = @(
"/i"
"`"$msiFile`""
"/qn"
)
if ($targetDir){
if (!(Test-Path $targetDir)){
throw "Path to the Installation Directory $($targetDir) is invalid.
Please supply a valid installation directory"
}
$arguments += "INSTALLDIR=`"$targetDir`""
}
Write-Verbose "Installing $msiFile....."
$process = Start-Process -FilePath msiexec.exe -ArgumentList $arguments -Wait -PassThru
if ($process.ExitCode -eq 0){
Write-Verbose "$msiFile has been successfully installed"
}
else {
Write-Verbose "installer exit code $($process.ExitCode) for file $($msifile)"
}
}
$Azure="Azure"
while(!($myWeb = Get-Website -name "WEBSITE NAME*")){
Write-Host "Website not installed. Waiting 30 seconds..."
Start-Sleep 30
}
if(Get-Module -ListAvailable | Where-Object{ $_.Name -eq $Azure })
{
[Reflection.Assembly]::LoadWithPartialName("Microsoft.WindowsAzure.ServiceRuntime")
$ConfigurationStorageConnectionString =
[Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment]::GetConfigurationSettingValue
("ConfigurationStorageConnectionString")
$Ctx = New-AzureStorageContext -ConnectionString $ConfigurationStorageConnectionString
$TableName = "Configuration"
$table = Get-AzureStorageTable –Name $TableName -Context $Ctx
$query = New-Object Microsoft.WindowsAzure.Storage.Table.TableQuery
$list = New-Object System.Collections.Generic.List[string]
$list.Add("PartitionKey")
$list.Add("RowKey")
$list.Add("Data")
$query.FilterString = "RowKey eq 'PAPI App Pool Connection'"
$query.SelectColumns = $list
$entities = $table.CloudTable.ExecuteQuery($query)
$Data1=$entities.Properties
$Data2=$Data1.Values
$String=$Data2.PropertyAsObject
Write-Host "Assigning AppPool"
$UserName,$Password = $string.split(' ',2)
Import-Module WebAdministration
$IISName= get-childitem -path iis:\apppools\ | where Name -NotLike ".Net*" | select name
$IIS=$IISName.name
Set-ItemProperty iis:\apppools\$IIS -name processModel
-value @{userName="$UserName";password="$Password";identitytype=3}
}
else
{
"StartUp\Powershell.msi" | Install-MSIFile
}