Jump to content

[Python] Login Scripts em Python


Recommended Posts

Os créditos estão apresentados...

Title: Migrate your Windows login scripts to Python

Submitter: Fadly Tabrani (other recipes)

Last Updated: 2005/06/27

Version no: 1.0

Category: System

  --Rate-- 5 - Excellent 4 - Very good 3 - Good 2 - Needs work 1 - Poor 

Not Rated yet


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

Source: Text Source

#!/usr/bin/env python
# -*- coding: utf-8 -*


1. Install Python 2.3 from python.org to your computer.

2. Install your favourite modules:


        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


    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? Endlesssee 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(0network_drives.Count() -1 2) 
                     if network_drives.Item(i)]:

# Network drive mapping.
drive_mappings = [
for drive_letternetwork_path in drive_mapping:
    except Exceptionerr:
        print err

# Batch execute other python files in some_dir.
import sys
# virusupdate.py
import virusupdate
# checkhotfix.py
import checkhotfix

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


Have you been scouring for utilities from resource kitssitesjust to do something from your login scripts. Well I haveand I have given up. Moving to Python have eliminated that needeverything and I mean everything can be done from Python itself.

I work in a company where different user environments are created frequentlyongoing projectsnew clientsnew serversnew 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.


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.