How to start and stop application pool in IIS using powershell script

后端 未结 5 980
小蘑菇
小蘑菇 2021-02-19 19:53

I want to start and stop application pool in IIS using powershell script. I had try to write the script but i didn\'t get this.

相关标签:
5条回答
  • 2021-02-19 20:37

    To stop an App Pool using PowerShell use

    Stop-WebAppPool -Name YourAppPoolNameHere
    

    And to start the App Pool

    Start-WebAppPool -Name YourAppPoolNameHere
    

    You will need the WebAdministration module installed so check you have it with this command

     Get-Module -ListAvailable
    
    0 讨论(0)
  • 2021-02-19 20:39

    These days the IISAdminstration module has mostly superceded WebAdministration. So if you're on Windows 10 / Server 2016, you can use Get-IISAppPool:

    (Get-IISAppPool "name").Recycle()
    
    0 讨论(0)
  • 2021-02-19 20:42

    You have to import the WebAdministration module using Import-Module and then you can use Start-WebAppPool and Stop-WebAppPool

    0 讨论(0)
  • 2021-02-19 20:49

    You can use this

    if your use (PowerShell 2.0) import WebAdministration module

    import-module WebAdministration
    

    Please check the state of the application pool before. If the application pool is already stopped you get an exception.

    Stop application pool:

    $applicationPoolName = 'DefaultAppPool'
    
    if((Get-WebAppPoolState -Name $applicationPoolName).Value -ne 'Stopped'){
        Write-Output ('Stopping Application Pool: {0}' -f $applicationPoolName)
        Stop-WebAppPool -Name $applicationPoolName
    } 
    

    Start application pool:

    if((Get-WebAppPoolState -Name $applicationPoolName).Value -ne 'Started'){
        Write-Output ('Starting Application Pool: {0}' -f $applicationPoolName)
        Start-WebAppPool -Name $applicationPoolName
    }
    

    Permissions: You have to be a member of the "IIS Admins" group.

    0 讨论(0)
  • 2021-02-19 20:52

    You can stop and stop all application pools respectively using the following powershell script. The second line below elevates permissions. You could exclude this and just run as administrator.

    Stop all application pools script

    Import-Module WebAdministration
    
    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
    
    $AppPools=Get-ChildItem IIS:\AppPools | Where {$_.State -eq "Started"}
    
    ForEach($AppPool in $AppPools)
    {
     Stop-WebAppPool -name $AppPool.name
    # Write-Output ('Stopping Application Pool: {0}' -f $AppPool.name)
    }
    

    Start all application pools script

      Import-Module WebAdministration
    
        if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
    
        $AppPools=Get-ChildItem IIS:\AppPools | Where {$_.State -eq "Stopped"}
        ForEach($AppPool in $AppPools)
        {
         Start-WebAppPool -name $AppPool.name
        # Write-Output ('Starting Application Pool: {0}' -f $AppPool.name)
        }
    
    0 讨论(0)
提交回复
热议问题