Advertisement
Guest User

Untitled

a guest
Feb 4th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. # This script configures IIS application pool and app.
  2. # example usage:
  3. # PS> init-server.ps1 -AppName MyApp -TargetComputers machine1,machine2 -PSSessionUser $(PSSessionUser) -PSSessionPassword $(PSSessionPassword) -AppPoolUser $(AppPoolUser) -AppPoolPassword $(AppPoolPassword)
  4.  
  5. param(
  6. [Parameter(Mandatory=$true)][string]$AppName,
  7. [string]$TargetComputers,
  8. [string]$Username,
  9. [string]$PSSessionUser,
  10. [string]$PSSessionPassword,
  11. [string]$AppPoolUser,
  12. [string]$AppPoolPassword,
  13. [switch]$Cleanup,
  14. [switch]$Show)
  15.  
  16. $script = {
  17. param(
  18. $AppName,
  19. $AppPoolUser,
  20. $AppPoolPassword,
  21. $Cleanup,
  22. $Show
  23. )
  24.  
  25. $apppool = $AppName
  26. $site = 'Default Web Site'
  27. $path = "/Apps/$AppName"
  28. $app = "$site$path"
  29. $physicalPath = "C:\Applications\Apps\$AppName"
  30.  
  31. $appcmd = [System.Environment]::ExpandEnvironmentVariables("%systemroot%\system32\inetsrv\AppCmd.exe")
  32.  
  33. $appPoolExists = (&$appcmd list apppool /name:$apppool) -ne $null
  34. if ($appPoolExists -eq $false)
  35. {
  36. &$appcmd add apppool /name:$apppool `
  37. /startMode:AlwaysRunning `
  38. /autoStart:true `
  39. /managedRuntimeVersion:'v4.0' `
  40. /processModel.identityType:SpecificUser `
  41. /processModel.userName:$AppPoolUser `
  42. /processModel.password:$AppPoolPassword
  43. }
  44. else
  45. {
  46. &$appcmd set apppool $apppool `
  47. /startMode:AlwaysRunning `
  48. /autoStart:true `
  49. /managedRuntimeVersion:'v4.0' `
  50. /processModel.identityType:SpecificUser `
  51. /processModel.userName:$AppPoolUser `
  52. /processModel.password:$AppPoolPassword
  53. }
  54.  
  55. $appExists = (&$appcmd list app /path:$path /site.name:$site) -ne $null
  56. if ($appExists -eq $false)
  57. {
  58. &$appcmd add app `
  59. /site.name:$site `
  60. /path:$path `
  61. /physicalPath:$physicalPath `
  62. /applicationPool:$apppool
  63. }
  64. else
  65. {
  66. &$appcmd set app $app `
  67. /site.name:$site `
  68. /path:$path `
  69. /applicationPool:$apppool
  70. }
  71.  
  72. if ($Cleanup -eq $true)
  73. {
  74. &$appcmd delete apppool $apppool
  75. &$appcmd delete app $app
  76. }
  77.  
  78. if ($Show -eq $true) {
  79. &$appcmd list apppool $apppool /config
  80. &$appcmd list app $app /config
  81. }
  82. }
  83.  
  84. $pw = ConvertTo-SecureString $PSSessionPassword -AsPlainText -Force
  85. $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $PSSessionUser, $pw
  86.  
  87. foreach ($computer in $TargetComputers.Split(@(','), [System.StringSplitOptions]::RemoveEmptyEntries))
  88. {
  89. Write-Host "Configuring $AppName on $($computer.Trim())" -BackgroundColor Black -ForegroundColor Green
  90.  
  91. $session = New-PSSession -ComputerName $computer -Credential $credential
  92.  
  93. Invoke-Command -Session $session `
  94. -ArgumentList ($AppName, $AppPoolUser, $AppPoolPassword, $Cleanup, $Show) `
  95. -ScriptBlock $script
  96.  
  97. Remove-PSSession $session
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement