Last active
August 29, 2015 14:04
-
-
Save clementi/5060e30bd960b15783f5 to your computer and use it in GitHub Desktop.
Recycle IIS app pools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import json | |
import os.path | |
import subprocess | |
import sys | |
CONFIG_FILE_NAME = ".raprc" | |
APPCMD_PATH = "c:/windows/system32/inetsrv/appcmd.exe" | |
def get_default_apppool(): | |
home_dir = os.path.expanduser('~') | |
config_file_path = os.path.join(home_dir, CONFIG_FILE_NAME) | |
if os.path.isfile(config_file_path): | |
with open(config_file_path, 'rb') as config_file: | |
config = json.loads(config_file.read()) | |
return config['defaultAppPool'] | |
return None | |
def get_apppools(): | |
output = subprocess.Popen([APPCMD_PATH, 'list', 'apppool'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0] | |
lines = output.split(os.linesep) | |
for line in lines: | |
if line.strip(): | |
yield line | |
def get_apppool_names(): | |
for apppool in get_apppools(): | |
yield apppool[9:apppool[9:].index('"') + 9] | |
def recycle_apppool(apppool_name): | |
return subprocess.Popen([APPCMD_PATH, 'recycle', 'apppool', apppool_name], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0] | |
def main(): | |
parser = argparse.ArgumentParser(description='Recycle IIS app pools.') | |
parser.add_argument('apppool', metavar='APPPOOL', type=str, help="the name of the app pool to recycle", nargs='?', default=get_default_apppool()) | |
parser.add_argument('-a', '--all', help="recycle all app pools (ingores any provided app pool name)", default=False, action='store_true') | |
parser.add_argument('-l', '--list', help="list app pools (ignores any provided app pool name)", default=False, action='store_true') | |
parser.add_argument('-v', '--verbose', help="increase verbosity", default=False, action='store_true') | |
args = parser.parse_args(sys.argv[1:]) | |
try: | |
if args.list: | |
for apppool in get_apppools(): | |
print apppool | |
elif args.all: | |
# Get all app pools | |
if args.verbose: | |
print "Get all app pools" | |
apppool_names = get_apppool_names() | |
# Recycle each one | |
if args.verbose: | |
print "Recycle each app pool" | |
for apppool_name in apppool_names: | |
print recycle_apppool(apppool_name), | |
elif args.apppool: | |
# recycle the specified app pool | |
if args.verbose: | |
print "Recycling {}".format(args.apppool) | |
print recycle_apppool(args.apppool), | |
else: | |
raise Exception("No app pool specified either in config file or on command line.") | |
except Exception as ex: | |
print "Error: {}".format(ex) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment