Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Python file processing

I'm trying to get a script that loops through a list of servers, and does an iisreset.

Can someone help me out with this? I'm a little new, and I'm not sure why it fails. It's for windows. I had to use rstrip to get rid of \n. However, it seems to just say it's timing out, and not looping through the servers.

Thanks

import wmi, getpass
import sys
import os
import re

uname = raw_input(r"Username With Domain (Example: prod\a-bob.rick): ")
passwd = getpass.getpass()


file = open(sys.argv[1], "r")
line = file.readline().rstrip()
x = line
status = raw_input(r"Reset Web Server ")
if str(status).lower() == 'y':
    while line:
        try:
            print("attempting")
            c = wmi.WMI(computer=x, user=uname, password=passwd)
            process_id, return_value = c.Win32_Process.Create(CommandLine="iisreset")
            for process in c.Win32_Process(ProcessId=process_id):
                 result = "Success"
        except wmi.x_access_denied:
            print("access denied")
            result = 'Denied'
        except wmi.x_wmi:
             result = 'Timeout'
        except:
            result = 'Fail'
        print(x, result)

Open in new window

0
winthrup8
Asked:
winthrup8
1 Solution
 
gelonidaCommented:
There is an error in your while loop.

you should remove line 11

change line 15 to

for line in file:
    line = line.rstrip()
    x = line

I don't understand why you use the variable line and x if they store both the same value, but perhaps this is just because you simplified above example???
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now