Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

Cloud Service - Adding to Domain

5.00/5 (3 votes)
29 Mar 2017CPOL 9.4K  
Adding an Azure Cloud Service to a Domain and setting the App Pool to a Domain User

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:

PowerShell
# Initialize domain variables

$domain = "FQDN"
$dmuser = "DOMAIN\USERNAME"
$dmpswd = "PASSWORD"
$dmspwd = ConvertTo-SecureString $dmpswd -AsPlainText -Force
$dmcred = New-Object System.Management.Automation.PSCredential ($dmuser, $dmspwd)

# Add AD Domain Extension to the cloud service roles

Set-AzureServiceADDomainExtension -Service CSNAME -Role "ROLENAME" 
-Slot Production -DomainName $domain -Credential $dmcred -JoinOption 35 -Restart

Using Table Storage to get the Username & Password:

PowerShell
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")

#Query
$Ctx = New-AzureStorageContext -ConnectionString $ConfigurationStorageConnectionString
$TableName = "Configuration"

$table = Get-AzureStorageTable –Name $TableName -Context $Ctx
$query = New-Object Microsoft.WindowsAzure.Storage.Table.TableQuery

#Define columns to select.
$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
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)