How to run a PowerCLI script to collect VMware infrastructure data for Infrastructure Mapper

This document explains how to run a PowerCLI script to collect VMware infrastructure data from PowerCLI and upload it to Infrastructure Mapper (IFMP).

Using PowerCLI, we can extract an overview of the virtual infrastructure that relies on the VMware vSphere PowerCLI command line and scripting tool built on Windows PowerShell. To use this collection method, you will need a Windows machine with network access to your vCenter Server instance(s) and to the VMware infrastructure. Additionally, the Windows machine needs to have the PowerCLI software installed.

To run a script in PowerCLI, follow these steps:

0. Pre-requisites

  • Network connectivity between vCenter Server and the workstation with PowerCLI installation.
  • The Administrator user password is known for all vCenter Servers.

1. Install PowerCLI

First, please make sure that you have the latest version of PowerCLI installed on your machine. You can install it by running this command in an elevated PowerShell window:

Install-Module -Name VMware.PowerCLI -Scope CurrentUser

If prompted to trust the repository, type 'Y' and hit Enter.


2. Launch PowerCLI

Open a new PowerShell window and launch PowerCLI by typing the following command:

Import-Module VMware.PowerCLI


3. Connect to your vCenter or ESXi host

To connect to your vCenter Server or ESXi host, run the following command with your credentials:

Connect-VIServer -Server <server-address> -User <username> -Password <password>

Replace <server-address>, <username>, and <password> with the appropriate values for your environment.


4. Run the script

To run the script in PowerCLI, change the directory (cd) to a directory where you have write permission, and use the following command to execute the script:

.\path\to\your\VCenter_inventory.ps1

You can find our PowerCLI script here

Replace path\to\your\VCenter_inventory.ps1 with the actual path and name of the script file. The script will execute in the PowerCLI environment, and you'll see the results in the PowerShell window.

The following output files will be created under the current directory:

<VCenter Server Name>-HW-Inventory-Information.csv

Where <VCenter Server Name> is the name of the VCenter Server.

Troubleshooting


1. Policy restrictions: since the script you create is not digitally signed, you might encounter an issue when running it. If you receive any policy issues error message, please Copy and Paste the .ps1 code directly into the Windows Powershell console. This way, you should be able to execute the script. However, keep in mind that you will be limited by your current user privileges. 

Alternatively, you may need to change the execution policy on your machine. You can do this by running the following command in an elevated PowerShell window:

Set-ExecutionPolicy RemoteSigned

Type 'Y' and hit Enter if prompted to confirm the change. This will allow you to run locally created scripts while still requiring remote scripts to be signed by a trusted publisher.


2. PowerCLI login: please make sure you use the correct login credentials when connecting through PowerCLI. You can use simple commands to see if you have enough privileges:

$Global:DefaultVIServers

3. Insufficient privileges: please check if you have enough privileges to write by creating a test folder:

Mkdir Test