Geeks With Blogs
Developers Friendly
Hi,

Providing sample powershell script to start and stop Application Pool of an application hosted in IIS.

* Save the below script in a file called as IISCmds.ps1
#Start To Start Application Pool

    Function fnStartApplicationPool([string]$appPoolName)
    {
  import-module WebAdministration
       if((Get-WebAppPoolState $appPoolName).Value -ne 'Started')
       {
      Start-WebAppPool -Name $appPoolName
       }
    }
    
#End To Start Application Pool

#Start To Stop Application Pool

    Function fnStopApplicationPool([string]$appPoolName)
    {
  import-module WebAdministration
       if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped')
       {
      Stop-WebAppPool -Name $appPoolName
       }
    }
    
#End To Stop Application Pool

* Caller of IISCmds.ps1 file.
    
#Start - To Stop Application Pool

$FTPUser = '$$$$$' #Provide Server User Name
        $FTPPassword = '*******' #Provide Password
        $UserName=$FTPUser
        $Password=$FTPPassword
        $ServerName = 'Server IP Address'
$appPoolName = 'TestSites'  #Application Pool Name

$pwd = convertto-securestring $Password -asplaintext -force
        $cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $UserName,$pwd

        invoke-command -computername $ServerName -Credential $cred `
        -ScriptBlock ${function:fnStopApplicationPool} `
        -ArgumentList $appPoolName

#End - To Stop Application Pool

#Start - To Start Application Pool

$FTPUser = '$$$$$' #Provide Server User Name
        $FTPPassword = '*******' #Provide Password
        $UserName=$FTPUser
        $Password=$FTPPassword
        $ServerName = 'Server IP Address'
$appPoolName = 'TestSites'  #Application Pool Name

$pwd = convertto-securestring $Password -asplaintext -force
        $cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $UserName,$pwd

        invoke-command -computername $ServerName -Credential $cred `
        -ScriptBlock ${function:fnStartApplicationPool} `
        -ArgumentList $appPoolName

#End - To Start Application Pool


Thanks!!!

Posted on Thursday, January 16, 2014 4:29 AM IIS , PowerShell | Back to top


Comments on this post: Starting and stopping Application Pool using Powershell

# re: Starting and stopping Application Pool using Powershell
Requesting Gravatar...
If I want to use script of starting and stopping application pool in my local machine then how to use script. because I have executed script of stopping app. pool but it gives me an error like 'import-module : Process should have elevated status to access IIS configuration data.'. Please help for the same.
Left by Paresh on May 09, 2016 3:49 PM

# re: Starting and stopping Application Pool using Powershell
Requesting Gravatar...
Use elevated PS console. Right-click and choose Run as Administrator
Left by Alexander on May 12, 2016 9:04 PM

Your comment:
 (will show your gravatar)


Copyright © Manjunath K | Powered by: GeeksWithBlogs.net