Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This script configures IIS application pool and app.
- # example usage:
- # PS> init-server.ps1 -AppName MyApp -TargetComputers machine1,machine2 -PSSessionUser $(PSSessionUser) -PSSessionPassword $(PSSessionPassword) -AppPoolUser $(AppPoolUser) -AppPoolPassword $(AppPoolPassword)
- param(
- [Parameter(Mandatory=$true)][string]$AppName,
- [string]$TargetComputers,
- [string]$Username,
- [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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement