Powershell to Add serviceAutoStartProvider in IIS

on Friday, April 18, 2014

I’ve used the serviceAutoStart attribute a couple times since its introduction. And mostly only in conjuction with WCF based services. I’ve had pretty good success with Application Initialization (IIS 7.5, IIS 8.0) for normal website preloading.

Recently I wanted to add a new serviceAutoStartProvider and set an application to use it through an automated script. I couldn’t really find a premade tool that would do that on both IIS 7.5 and 8.0 so I tried to put together something.

This worked for me, but that may be specific to the environment I was working.

(Note: When adding a new line to the .config file it doesn’t really get the spacing right. I understand why it’s happening but I don’t really know how to fix the issue. If you know how, please drop in a comment.)

Function Test-AutoStartProviderExists {
Param(
[Parameter(Mandatory = $true)]
[string]$AutoStartProvider,
[string]$AppServerName = [System.Net.Dns]::GetHostName()
)
Process {
[xml]$appHost = New-Object xml;
$appHost.psbase.PreserveWhitespace = $true;

$configDir = "\\$AppServerName\C$\Windows\System32\inetsrv\config";
$configPath = "$configDir\applicationHost.config";
$appHost.Load($configPath);

$provider = $appHost.configuration.'system.applicationHost'.serviceAutoStartProviders.add |? {$_.name -eq $AutoStartProvider};

return $provider -ne $null;
}
}

Function Get-AutoStartProvider {
Param(
[Parameter(Mandatory = $true)]
[string]$SiteName,
#[string]$AppName, # untested
[string]$AppServerName = [System.Net.Dns]::GetHostName()
)
Process {
[xml]$appHost = New-Object xml;
$appHost.psbase.PreserveWhitespace = $true;

$configDir = "\\$AppServerName\C$\Windows\System32\inetsrv\config";
$configPath = "$configDir\applicationHost.config";
$appHost.Load($configPath);

$site = $appHost.configuration.'system.applicationHost'.sites.site |? {$_.name -eq $SiteName};
#$app = $site.application |? {$_.path -eq $("/" + $AppName)}; # untested

return $site.serviceAutoStartProvider;
#return $app.serviceAutoStartProvider; # untested
}
}

Function Set-AutoStartProvider {
Param(
[Parameter(Mandatory = $true)]
[string]$SiteName,
#[string]$AppName, # untested
[Parameter(Mandatory = $true)]
[string]$AutoStartProvider,
[string]$AppServerName = [System.Net.Dns]::GetHostName()
)
Process {
[xml]$appHost = New-Object xml;
$appHost.psbase.PreserveWhitespace = $true;

$configDir = "\\$AppServerName\C$\Windows\System32\inetsrv\config";
$configPath = "$configDir\applicationHost.config";
$appHost.Load($configPath);

$site = $appHost.configuration.'system.applicationHost'.sites.site |? {$_.name -eq $SiteName};
#$app = $site.application |? {$_.path -eq $("/" + $AppName)}; # untested

$site.serviceAutoStartProvider = $AutoStartProvider;
#$app.serviceAutoStartProvider = $AutoStartProvider; # untested

Write-Warning "Updating IIS $AppServerName - Setting $SiteName's serviceAutoStartProvider to '$AutoStartProvider'";
try {
$appHost.Save($configPath);
} catch {
Write-Warning "Unable to save, waiting 5 seconds for file lock to release and try again ..."
Start-Sleep 5
$appHost.Save($configPath);
}
Write-Host "Updated IIS $AppServerName - Set $SiteName's serviceAutoStartProvider to '$AutoStartProvider'";
}
}

Function New-AutoStartProvider {
Param(
[Parameter(Mandatory = $true)]
[string]$AutoStartProvider,
[Parameter(Mandatory=$true)]
[string]$Type,
[string]$AppServerName = [System.Net.Dns]::GetHostName()
)
Process {
if(Test-AutoStartProviderExists -AutoStartProvider $AutoStartProvider -AppServerName $AppServerName) {
throw "IIS $AppServerName - Unable to add autoStartProvider $AutoStartProvider. It already exists."
}

[xml]$appHost = New-Object xml;
$appHost.psbase.PreserveWhitespace = $true;

$configDir = "\\$AppServerName\C$\Windows\System32\inetsrv\config";
$configPath = "$configDir\applicationHost.config";
$appHost.Load($configPath);

$autoStartProviders = $appHost.configuration.'system.applicationHost'.serviceAutoStartProviders;

$provider = $appHost.CreateElement("add")
$provider.SetAttribute("name", $AutoStartProvider);
$provider.SetAttribute("type", $Type);
$autoStartProviders.AppendChild($provider);

Write-Warning "Updating IIS $AppServerName - Adding serviceAutoStartProvider '$AutoStartProvider'";
try {
$appHost.Save($configPath);
} catch {
Write-Warning "Unable to save, waiting 5 seconds for file lock to release and try again ..."
Start-Sleep 5
$appHost.Save($configPath);
}
Write-Host "Updated IIS $AppServerName - Added serviceAutoStartProvider '$AutoStartProvider'";
}
}

0 comments:

Post a Comment


Creative Commons License
This site uses Alex Gorbatchev's SyntaxHighlighter, and hosted by herdingcode.com's Jon Galloway.