Skip to content

Instantly share code, notes, and snippets.

@niaher
Last active April 2, 2019 08:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niaher/8cc8185f6952cea3bc1d to your computer and use it in GitHub Desktop.
Save niaher/8cc8185f6952cea3bc1d to your computer and use it in GitHub Desktop.
# This script configures IIS application pool and app.
# example usage:
# PS> init-server.ps1 -AppName MyApp -TargetComputers computer1,computer2 -PSSessionUser $(PSSessionUser) -PSSessionPassword $(PSSessionPassword) -AppPoolUser $(AppPoolUser) -AppPoolPassword $(AppPoolPassword)
param(
[Parameter(Mandatory=$true)][string]$AppName,
[string]$TargetComputers,
[string]$PSSessionUser,
[string]$PSSessionPassword,
[string]$AppPoolUser,
[string]$AppPoolPassword,
[switch]$Cleanup,
[switch]$Show)
$script = {
param(
$AppName,
$AppPoolUser,
$AppPoolPassword,
$Cleanup,
$Show
)
$apppool = $AppName
$site = 'Default Web Site'
$path = "/Apps/$AppName"
$app = "$site$path"
$physicalPath = "C:\Applications\Apps\$AppName"
$appcmd = [System.Environment]::ExpandEnvironmentVariables("%systemroot%\system32\inetsrv\AppCmd.exe")
$appPoolExists = (&$appcmd list apppool /name:$apppool) -ne $null
if ($appPoolExists -eq $false)
{
&$appcmd add apppool /name:$apppool `
/startMode:AlwaysRunning `
/autoStart:true `
/managedRuntimeVersion:'v4.0' `
/processModel.identityType:SpecificUser `
/processModel.userName:$AppPoolUser `
/processModel.password:$AppPoolPassword
}
else
{
&$appcmd set apppool $apppool `
/startMode:AlwaysRunning `
/autoStart:true `
/managedRuntimeVersion:'v4.0' `
/processModel.identityType:SpecificUser `
/processModel.userName:$AppPoolUser `
/processModel.password:$AppPoolPassword
}
$appExists = (&$appcmd list app /path:$path /site.name:$site) -ne $null
if ($appExists -eq $false)
{
&$appcmd add app `
/site.name:$site `
/path:$path `
/physicalPath:$physicalPath `
/applicationPool:$apppool
}
else
{
&$appcmd set app $app `
/site.name:$site `
/path:$path `
/applicationPool:$apppool
}
if ($Cleanup -eq $true)
{
&$appcmd delete apppool $apppool
&$appcmd delete app $app
}
if ($Show -eq $true) {
&$appcmd list apppool $apppool /config
&$appcmd list app $app /config
}
}
$pw = ConvertTo-SecureString $PSSessionPassword -AsPlainText -Force
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $PSSessionUser, $pw
foreach ($computer in $TargetComputers.Split(@(','), [System.StringSplitOptions]::RemoveEmptyEntries))
{
Write-Host "Configuring $AppName on $($computer.Trim())" -BackgroundColor Black -ForegroundColor Green
$session = New-PSSession -ComputerName $computer -Credential $credential
Invoke-Command -Session $session `
-ArgumentList ($AppName, $AppPoolUser, $AppPoolPassword, $Cleanup, $Show) `
-ScriptBlock $script
Remove-PSSession $session
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment