Deployment Solution

 View Only

Add the Power of Python to WinPE 

Jan 11, 2007 04:13 PM

This article explains how to incorporate the Python interpreter into the WinPE automation environment. This allows you to write sophisticated object oriented scripts without relying upon Windows Scripting Host. Also, if you use the two other articles I've written explaining how to make the DOS and Linux preboot Python enabled, you can now construct scripts that are 100% platform independent.

Python is an object oriented programming language that dynamically typed, and has automatic memory management. Its vast selection of modules allows you to accomplish nearly any programming task with ease. Python is not compiled, but is run by an interpreter. This is why it is often called a "Scripted Language". As previously mentioned, interpreters (and instructions on how to install them on their respective pre-boots) are available for DOS and Linux. While Python will run the same no matter what interpreter is used, it should be remembered that their interaction with the host OS may be different (for instance the necessity of using short file names in DOS).

For simplicity and for programming convenience, in this example I will not be entering Python programs directly into the jobs themselves. Embedded scripts have size limitations, not syntax highlighting and are by default batch scripts. I find it better to write a Python file using IDLE, save it to the eXpress share and then use a simple one-line batch script to call the Python interpreter as it resides on the server and supply the Python file pathname. If you prefer to have your scripts embedded in the jobs, you may be able to pipe them into the interpreter by hand, by I have not tried it.

I should probably mention that this is not supported by Altiris, and should always be tried in a test environment before it is performed on any vital systems. Luckily we are not modifying any preboot images, so the following steps are fairly harmless and can be undone by running the Python uninstaller.

The official website for Python is: http://www.python.org/, with excellent module documentation available at http://docs.python.org/modindex.html .

Ok, let's get started, review the following pre-requisites and start with Step 1.

Prerequisites: Deployment Solution version 6.5 SP1 or newer, and the WinPE preboot environment installed.

Part I

Step 1: First we need to visit www.python.org and download the latest version of the Python interpreter for Windows.

  1. Open a web browser and enter http://www.python.org into the address bar, and press GO.
  2. Click "Download" located on the left hand side.
  3. Click "Releases"
  4. Click "Python X.X" (X's represent the latest version. I will use 2.5 from now on in the example)
  5. Scroll down to the portion of the page that has the link to the file Python-2.5.msi. Alternatively you many enter the following link: http://www.python.org/ftp/python/2.5/python-2.5.msi for version 2.5 of the installer.
  6. Save this file to a temporary location.

Step 2: Double click on the file python-2.5.msi. This will launch a graphical installer wizard.

Step 3: Click "Install just for me" and click Next.

Step 4: The next screen will ask for the location in which you want to install your Python files. In this example I have chosen to host all the Python files on the eXpress share rather than modifying the WinPE base image. This allows for quicker download time for the PXE image, as well as lowers the memory requirements for the client. Therefore, change the default location from "C:\Python25\" to a folder called pythonWinPE in your express share. This will usually be "C:\Program Files\ Altiris\eXpress\Deployment Server\pythonWinPE". Once the location has been entered, click Next.

Step 5: The screen should now give you installation options as to what Python components you want to install. Since we aren't putting these in the preboot image itself, feel free to install everything (especially if you want to use IDLE to edit your scripts). Once you have made your installation selections, click Next.

Step 6: Once Python is finished installing click Finished. You can now open IDLE, the Python editor by going to Start >All Programs > Python 2.5 > IDLE (Python GUI). You can also run the interpreter locally on the DS server (useful for testing or server side scripts).

Step 7: Open your eXpress share and go to your pythonWinPE folder. Make sure that python25.dll and msvcr71.dll are there. If they aren't, they may need to be copied from C:\Windows\System32.

Step 8: Create a folder called pyScripts on your eXpress share. This folder is a convenient place to store your scripts.

You're done installing the interpreter! Now, let's run a sample script.

Part II

Step 1: Go to Start > All Programs > Python 2.5 > IDLE (Python GUI). This will open a window with a Python command prompt.

Step 2: Go to File > New Window. This will open a new blank document.

Step 3: Enter the following script:

File = open("F:\pyScripts\Hello.txt","w")
File.writelines("Hello from the Python World")
File.close()

Step 4: Go to File > Save As and name save the file to pyScripts\test.py

This script will simply write "Hello from the Python World" to a file located in the pyScripts directory.

Step 5: Open the Deployment Console and create a new job naming it "Python Test".

Step 6: Go to Add > Run Script and enter the following Script.

%ALTIRIS_SHARE%\pythonWinPE\python %ALTIRIS_SHARE%\pyScripts\test.py > %ALTIRIS_SHARE%\pythonDebug.txt

Select "Windows" radio button and press Next.

Step 7: Click "Automation pre-boot environment" and change the dropdown menu to read "WinPE Managed (32-bit)"

Step 8: Click Finish.

Step 9: Drag "Python Test" to a machine and watch the pyScripts folder to see if Hello.txt appears. If it does, everything is set up correctly. If it doesn't, you can reference pythonDebug.txt to see any text that may have been output to standard out.

Installing the Python Interpreter in the image itself

If you are in an environment where you would want the whole Python interpreter embedded in your WinPE image, such as if you are using boot discs instead of PXE, and have a slow connection to your DS server this is possible without too much difficulty. Follow these steps:

Step 1: Do all steps in Part I, except install Python to it's default location of C:\Program Files\python25. Also, it is wise to do a minimal installation of Python (unselect all optional components) because of limitations in the number of files that can be included in a WinPE image, as well as speed in downloading the image. Also, the Boot Disk Creator will take a forever if it has to include all the optional components.

Step 2: Open the PXE Configuration Utility, click your WinPE Configuration and press Edit and then Edit Boot Image. You will then be presented with a window in which you can edit the file structure of your WinPE image.

Step 3: First click on the "startup.bat" file and add the line "set PYTHONCASEOK=1". Click the little save icon in the upper left hand corner. This was not necessary in a normal configuration (hosting the interpreter off the eXpress Share) because networked drives are not case sensitive to begin by default. We will be referencing the files in a RAM drive, so we need to tell the Python interpreter to ignore case or else the modules won't import properly.

Step 4: Right Click the %SystemRoot% folder and click on "New > Folder". Name this folder "pythonWinPE". Do not press next, but go to step 5.

Step 5: Since it would be very tedious to add each file one by one, we are going to slip all our files into this directory your created by using Windows Explorer. Open your eXpress share in Windows explorer and do a search for "pythonWinPE". It will find a location something like:

C:\Program Files\Altiris\eXpress\Deployment Server\Bootwiz\cfg-tmp\MenuOption131\shared\@sys@\pythonWinPE

This is a temporary location where files would go if you were to add them using the Boot Disk Creator. Open that folder, and copy all the contents of C:\Program Files\python2.5 (or wherever you installed it to) into that folder. Make sure to close the file explorer window after the files have copied!

Note: If you are doing very simple scripting you may want to go through C:\Program Files\python2.5 and delete any modules, or scripts that you don't plan on using. This will increase build time, as well as download time for the image. Not doing this will mean about a 15 megabyte increase to your image size.

Step 6: Go back to the Altiris Boot Disk Creator and press Next. Note: The Boot Disk creator was not designed to handle such a large amount of files, so depending on the speed of your machine, you may not see any responses for up to a minute. Just be patient and let the boot disk creator do it's work.

Step 7: Finish generating the images and press "Save". Close the PXE Configuration Utility.

You're done! The Python interpreter now will reside in X:\i386\pythonWinPE when you boot into WinPE. If you still want to host your scripts off of the eXpress share you may now begin writing a test script by going to PART II, except in step 6, your reference to the Python interpreter will be as follows:

X:\i386\pythonWinPE\python %ALTIRIS_SHARE%\pyScripts\test.py > %ALTIRIS_SHARE%\pythonDebug.txt

Instead of

%ALTIRIS_SHARE%\pythonWinPE\python %ALTIRIS_SHARE%\pyScripts\test.py > %ALTIRIS_SHARE%\pythonDebug.txt

Notes to Consider:
The Python interpreter is a 32bit application and while Windows 2003 64-bit and Windows XP 64-bit are capable of running 32 bit applications, WinPE 64-bit is not. So if you try to run Python scripts in WinPE 64-bit you'll be disappointed.

Statistics
0 Favorited
0 Views
2 Files
0 Shares
0 Downloads
Attachment(s)
JPG file
pic1.JPG   13 KB   1 version
Uploaded - Feb 25, 2020
JPG file
pic2.JPG   59 KB   1 version
Uploaded - Feb 25, 2020

Tags and Keywords

Related Entries and Links

No Related Resource entered.