Vmware powercli on python

Windows 10/11 – VMware PowerCLI Installation and Configuration¶

Installing and Configuring VMware PowerCLI to build a custom image using ImageBuilder on a Windows 10/11 workstation. With PowerCLI version 13.1 and greater there is no longer a requirement to install Python 3.7.9. The new requirement is Python 3.7.1 or greater.

Below are the five required steps, with links.

The required configuration steps.

Install VMware PowerCLI¶

Install VMware PowerCLI, which is set of scripts that run inside PowerShell from VMware.

Install PowerCLI¶

Open PowerShell on your workstation.

To install all PowerCLI modules, run the command:

Install-Module VMware.PowerCLI -Scope CurrentUser
  • If you see a warning that you are installing modules from an untrusted repository, press y and then press Enter to confirm the installation.

List PowerCLI Modules¶

To list version and path for PowerCLI and ImageBuilder modules, run the command:

Get-Module -ListAvailable -name VMware.PowerCLI,VMware.ImageBuilder | select name,path,version | Format-List
Name : VMware.ImageBuilder Path : C:\Users\username\OneDrive\Documents\WindowsPowerShell\Modules\VMware.ImageBuilder\8.0.0.21610262\VMware.ImageBuilder.psd1 Version : 8.0.0.21610262 Name : VMware.PowerCLI Path : C:\Users\username\OneDrive\Documents\WindowsPowerShell\Modules\VMware.PowerCLI\13.1.0.21624340\VMware.PowerCLI.psd1 Version : 13.1.0.21624340

PowerShell script ExecutionPolicy¶

To run VMware PowerCLI scripts the execution policies needs to be set. The default execution policy for Windows client computers is Restricted. Restricted means do not load configuration files or run scripts. There are two parameters for the execution policy, the acceptable execution policy and the scope.

Читайте также:  Python os walk все файлы

Personally, I use Scope ExecutionPolicy Unrestricted and CurrentUser. Loads all configuration files and runs all scripts for my username. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs).

List Execution Policy¶

To list the current execution policies for each scope in the order of precedence, run the command:

Scope ExecutionPolicy ----- --------------- MachinePolicy Undefined UserPolicy Undefined Process Undefined CurrentUser Restricted LocalMachine Undefined

ExecutionPolicy

Specifies the execution policy. If there are no Group Policies and each scope’s execution policy is set to Undefined, then Restricted becomes the effective policy for all users.

The acceptable execution policy values are as follows:

  • AllSigned — Requires that all scripts and configuration files are signed by a trusted publisher, including scripts written on the local computer.
  • Bypass — Nothing is blocked and there are no warnings or prompts.
  • Default — Sets the default execution policy. Restricted for Windows clients or RemoteSigned for Windows servers.
  • RemoteSigned — Requires that all scripts and configuration files downloaded from the Internet are signed by a trusted publisher. The default execution policy for Windows server computers.
  • Restricted — Doesn’t load configuration files or run scripts. The default execution policy for Windows client computers.
  • Undefined — No execution policy is set for the scope. Removes an assigned execution policy from a scope that is not set by a Group Policy. If the execution policy in all scopes is Undefined, the effective execution policy is Restricted.
  • Unrestricted — Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.

Specifies the scope that is affected by an execution policy. The default scope is LocalMachine.

The effective execution policy is determined by the order of precedence as follows:

  • MachinePolicy — Set by a Group Policy for all users of the computer.
  • UserPolicy — Set by a Group Policy for the current user of the computer.
  • Process — Affects only the current PowerShell session.
  • CurrentUser — Affects only the current user.
  • LocalMachine — Default scope that affects all users of the computer.

Set Execution Policy¶

To set and list the execution policy, run the commands:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser Get-ExecutionPolicy -List
Scope ExecutionPolicy ----- --------------- MachinePolicy Undefined UserPolicy Undefined Process Undefined CurrentUser Unrestricted LocalMachine Undefined

Installing Python3¶

To use VMware ImageBuilder module Python 3.7.1 or greater is required. The two most common ways to install Python 3 are from Python.org Downloads and the Microsoft Market Store. You should only choose one medthod not both.

Installing Python3 From Microsoft Store¶

This is my preferred method since it installs the package and sets up the PATH when installing.

  • If Python3 is not installed on the workstation open Powershell and type python it will open Microsoft Store to install the latest stable version on Python3.
  • To install Python 3.11 open the Microsoft Store app and search for Python, click on Python 3.11. This will install the latest stable version of 3.11, which is Python 3.11.3 at the time on this writing

Test Python 3.11 installation¶

To test the installation, open PowerShell on your workstation, run this command:

Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>

Install Required Python Modules¶

To build ESXi images using VMware ImageBuilder you need to install six psutil lxml pyopenssl python 3.7 modules.

Open a new PowerShell session.

First upgrade pip3, this is good practice before installing new Python modules.

To upgrade pip3, run this command:

python3 -m pip install pip --upgrade

To install required Python modules, run this command:

python3 -m pip install six psutil lxml pyopenssl --upgrade

To list the required modules and their version, run this command:

python3 -m pip list | Select-String -Pattern 'six|psutil|lxml|pyopenssl'
lxml 4.9.2 psutil 5.9.5 pyOpenSSL 23.1.1 six 1.16.0

Set the Python path for VMware PowerCLI¶

First you need to get the Python executable PATH this is needed to set the PATH.

To get the executable PATH, run this command:

C:\Users\username\AppData\Local\Microsoft\WindowsApps\python3.exe

Verify you have the execution policy to run the command:

If the output doesn’t show one of the following, Bypass, RemoteSigned or Unrestricted. Click here to set Set Execution Policy .

To set the VMwareCLI Python path replace the PATH with the results from the get executable PATH above, run the command:

Set-PowerCLIConfiguration -PythonPath $HOME\AppData\Local\Microsoft\WindowsApps\python3.exe -Scope User

To list the VMware PowerCLI Python PATH, run this command:

Get-PowerCLIConfiguration -scope Session | select PythonPath
PythonPath ---------- C:\Users\username\AppData\Local\Microsoft\WindowsApps\python3.exe

Install Python 3.7.9 From python.org Downloads¶

Second option for install Python3, Do not do this if you already have Python 3.7.1 or higher installed from the Microsoft Store.

To test you need to first find the path where it was installed, since installing this way there is no PATH to the executable.

Get Executable Path¶

To find the executable path open PowerShell on your workstation, run this command:

Get-ItemProperty -Path HKCU:\SOFTWARE\Python\PythonCore\3.7\InstallPath\ |select ExecutablePath
ExecutablePath -------------- C:\Users\John Doe\AppData\Local\Programs\Python\Python37\python.exe

When installing from python.org Downloads you need to enter the full path.

Test Python 3.7.9 installation¶

To test the installation, open PowerShell on your workstation and copy the ExecutablePath from the previous command.

& $HOME\AppData\Local\Programs\Python\Python37\python.exe
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 16:30:00) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>

Install Required Python Modules¶

To build ESXi images using VMware ImageBuilder you need to install six psutil lxml pyopenssl python 3.7 modules.

Upgrade pip3.7, this is good practice before installing new Python modules.

& $HOME\AppData\Local\Programs\Python\Python37\python.exe -m pip install pip --upgrade

To install required Python modules, run this command:

& $HOME\AppData\Local\Programs\Python\Python37\python.exe -m pip install six psutil lxml pyopenssl --upgrade

To list installed required modules and their version, run this command:

& $HOME\AppData\Local\Programs\Python\Python37\python.exe -m pip list | Select-String -Pattern 'six|psutil|lxml|pyopenssl'
lxml 4.9.2 psutil 5.9.5 pyOpenSSL 23.1.1 six 1.16.0

Set the Python path for VMware PowerCLI¶

Verify you have the execution policy to run the command:

If the output doesn’t show one of the following, Bypass, RemoteSigned or Unrestricted. Click here to set Set Execution Policy .

To set the VMwareCLI Python path replace the PATH with the results from the get executable PATH above, run the command:

Set-PowerCLIConfiguration -PythonPath $env:USERPROFILE\AppData\Local\Programs\Python\Python37\python.exe -Scope User

To list the VMware PowerCLI Python PATH, run this command:

Get-PowerCLIConfiguration -scope Session | select PythonPath
PythonPath ---------- C:\Users\John Doe\AppData\Local\Programs\Python\Python37\python.exe

Table of Contents

  • Windows 10/11 – VMware PowerCLI Installation and Configuration
    • Overview
    • Install VMware PowerCLI
      • Install PowerCLI
      • List PowerCLI Modules
      • List Execution Policy
      • Set Execution Policy
      • Installing Python3 From Microsoft Store
        • Test Python 3.11 installation
        • Install Required Python Modules
        • Set the Python path for VMware PowerCLI
        • Get Executable Path
        • Test Python 3.7.9 installation
        • Install Required Python Modules
        • Set the Python path for VMware PowerCLI

        Источник

        Saved searches

        Use saved searches to filter your results more quickly

        You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

        anudeep404/vmware_automation

        This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

        Name already in use

        A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

        Sign In Required

        Please sign in to use Codespaces.

        Launching GitHub Desktop

        If nothing happens, download GitHub Desktop and try again.

        Launching GitHub Desktop

        If nothing happens, download GitHub Desktop and try again.

        Launching Xcode

        If nothing happens, download Xcode and try again.

        Launching Visual Studio Code

        Your codespace will open once ready.

        There was a problem preparing your codespace, please try again.

        Latest commit

        Git stats

        Files

        Failed to load latest commit information.

        README.md

        «# vmware_automation» There are two ways to complete the task.

        Using PowerCLI (File extension .ps1) Using Python (File extension .py) Note: Use Python script ONLY if you have a list of hosts saved in the iplist.txt file and SSH is enabled on all the hosts.

        Steps to run PowerCLI script:

        Make sure the PowerCLI script and plink (tool, download it from https://the.earth.li/~sgtatham/putty/latest/w32/plink.exe) are in the same directory(referred as script directory here on). Open PowerCLI and navigate to the script directory. Run 1_alpha.ps1 This would log in vCenter, get the list of all the host management IPs, considering mgmt kernel port is vmk0. Enable SSH on them. Check for respective service(in this case, VSFWD) Start VSFWD if it is STOPPED on any host. Once the task is complete, you may have to disable SSH on all the hosts, execute 3_delta.ps1 Steps to run Python script:

        For any reason, if the customer doesn’t have PowerCLI and would like to use Python. Run the script 2_omega.py Tested with:

        Python 3.5 or 3.7 VMware vSphere PowerCLI 6.3 Release 1 build 3737840 VMware ESXi 6.5.0 build-4887370 vCenter 6.5 Build-5178943

        Источник

Оцените статью