Welcome, guest | Sign In | My Account | Store | Cart

The following is a guide for Windows administrators who would like to use Python for their login scripts. Once, you've found Python, you'll never go back to VbScript or the dreaded batch files.

Python, 61 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*
    
"""
1. Install Python 2.3 from python.org to your computer.
2. Install your favourite modules:
    Recommended:
        pywin32 module - accessing COM and other lovely windows stuff
        subprocess module - launching subprocesses from ya login scripts
        ctypes module - wrap any win32api        
3. Copy your Python23 folder to the NETLOGON (or any shared network) directory.
4. Copy these dll files from your system directory to that folder
    python23.dll
    pythoncom23.dll - pywin32
    pywintypes23.dll - pywin32
5. Running your python scripts
    create a batch file with the lines:
        rem - path to python23 folder, %\0.. refers to the current directory
        SET PYTHONPATH=%0\..\Python23
        rem - path to script to execute
        %PYTHONPATH%\python.exe %0\..\login.py
6. What can you do in python? Endless, see below...
"""

# pywintypes must be imported b4 any win32 modules
import pywintypes
import win32com.client

# Disconnect previously mapped drives.
wshnetwork = win32com.client.Dispatch('Wscript.Network')
network_drives = wshnetwork.EnumNetworkDrives()
for mapped_drive in [network_drives.Item(i) 
                     for i in range(0, network_drives.Count() -1 , 2) 
                     if network_drives.Item(i)]:
    wshnetwork.RemoveNetworkDrive(mapped_drive, True, True)

# Network drive mapping.
drive_mappings = [
    ('I:', '\\\\server1\\users'),
    ('J:', '\\\\server2\\sharing')]
for drive_letter, network_path in drive_mapping:
    try:
        wshnetwork.MapNetworkDrive(drive_letter, network_path)
    except Exception, err:
        print err

# Batch execute other python files in some_dir.
import sys
sys.path.append("some_dir")
# virusupdate.py
import virusupdate
# checkhotfix.py
import checkhotfix

# Other advantages:
# GUI for your logon scripts with TKInter, Win32Gui.
# Centrally managed python installation.
# Access to the Win32 API.
# Robust error handling.
# Not to mention, multithreading!
# Can also be used with Group Policy scripts.

Have you been scouring for utilities from resource kits, sites, just to do something from your login scripts. Well I have, and I have given up. Moving to Python have eliminated that need, everything and I mean everything can be done from Python itself.

I work in a company where different user environments are created frequently, ongoing projects, new clients, new servers, new requirements. Moving to Python allows me to code faster and be more flexible.

Login scripts run much faster and long running processes can be multithreaded for performance.

Don't believe me? Try it and you'll never look back.

2 comments

Luc Stepniewski 18 years, 9 months ago  # | flag

checkhotfix.py? Is there any chance you could post the source for checkhotfix.py too?

Chris Arndt 17 years, 11 months ago  # | flag

Useful links. There is a repository of Python scripts for Windows admin task on the Microsoft web site:

http://www.microsoft.com/technet/scriptcenter/scripts/python/default.mspx

If you need to access the Windows registry in your Python script, have a look at this recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66011