VMware Cloud Community
SureshDhanaraj
Contributor
Contributor

How to invoke/execute the existing script/function to esxi 5.0 host in a CSV file by PowerCLI ?

How to invoke/execute the existing script/function to esxi 5.0 host in a CSV file by PowerCLI ?

I have generated this script or cmdlet by project OnyX. But I need some suggestion or ways to execute the below cmdlet for the collation of esxi 5.0 hosts in a csv file. The below cmdlet will add several port group to vSwitch0, and make few other setting changes on that protgroup. Please suggest.

Please find the attached for the detailed Script/cmdlet.

Reply
0 Kudos
7 Replies
RvdNieuwendijk
Leadership
Leadership

If you create a file called vmhosts.csv that has the following structure:

Name

ESX1.yourdomain.com

ESX2.yourdomain.com

then you can use the following PowerCLI code as a wrapper around your code:

Import-Csv -Path vmhosts.csv |
ForEach-Object {
  $VMHost = Get-VMHost $_
  $VMHostView = $VMHost.ExtensionData
  $NetworkSystem = $VMHostView.Configmanager.NetworkSystem
  $VirtualNicManager = $VMHostView.Configmanager.VirtualNicManager
  $IscsiManager = $VMHostView.Configmanager.IscsiManager
  
  # Your code comes here!
}

At each place where in your code is used something like:

$_this = Get-View -Id 'IscsiManager-iscsiManager-360'
$_this.QueryPnicStatus("vmnic1")

you need to replace the MoRef with the corresponding variable:

$_this = Get-View -Id $IscsiManager
$_this.QueryPnicStatus("vmnic1")

Replace 'IscsiManager-iscsiManager-360' with $IscsiManager, 'HostVirtualNicManager-virtualNicManager-360' with $VirtualNicManager and 'HostNetworkSystem-networkSystem-360' with $NetworkSystem.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

First of all thanks a lot for timely reply. Let me check it and update you it works or not.

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

Hi,

I have tried running this script after suggested modifications but When it try to execute the variables $IscsiManager, $VirtualNicManager & $NetworkSystem it says “Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty”. I have attached detailed screenshot from PowerGUI debug screen, PowerCLI error in txt file, modified script & my .csv file. Please take a look. Waiting for the solution.

Note: Currently I have connected to the vCenter th’ connect-viserver. I didn’t specify credentials value for VMhost separately.

PowerGUI Debug Error.jpg

Regards

Suresh Dhanaraj

Reply
0 Kudos
LucD
Leadership
Leadership

That CSV file doesn't look to be a uni-code US-EN file.

Perhaps try adding the UseCulture parameter on the Import-Csv

Import-Csv -Path vmhosts.csv -UseCulture 

If that doesn't do the trick, how did you create that CSV file ?

From Excel ? And did you save it as a CSV file.

Try creating the CSV file from Notepad or even from PowerShell, and make sure it is a flat text file


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

Hi LucD,

Thanks a lot for the reply. I have followed your suggestion on UseCulture and creating the CSV by notepad. But yet the issue hasn’t resolved. Please find the attached for the detailed error code.

Do I need to use $HC = Get-Credential to supply the root credentials for the host to access the extensiondata? Currently I am executing the script connecting to vCenter itself with the administrative account which has full access.

Or Can we have simple PowerCLI script for the below purpose ?

Requirement:

We have a list of ESXi 5.0 host names in CSV file. We have add 8- 10 VM PortGroup on all the hosts in the CSV file (VLAN ID which is already mention in my script) to the vSwitch 0. VM NIC would be vmnic0 & 1.  At the port group level for all the 10 portgroup vmnic 0 has to be active & vmnic 1 has to be standby adaptor.

Regards

Suresh Dhanaraj

Reply
0 Kudos
LucD
Leadership
Leadership

Let's start with the beginning.

Is your CSV file correct. Do you see the configured values on screen when you do

Import-Csv -Path C:\Users\sureshdadmin\vmhosts.csv

And can you confirm that the CSV file you attached earlier is actually the CSV file you are using ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
SureshDhanaraj
Contributor
Contributor

Yes LucD. Here is the output when i run import-CSV. I am saving the CSV in the attached screenshot method. Please suggest.

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Import-Csv -Path C:\Users\sureshdadmin\vmhosts.csv
Name
----
testesx02.corp.org
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>
Reply
0 Kudos