Enable Software iSCSI Adapter Using PowerCLI

I recently began studying for the VCAP5-DCA (VDCA550) and in the course of administering a 2 host environment, it’s very rare that I have to use PowerCLI. The time it would take to write the script would easily exceed the time to perform the task by hand. But section 7 of the VCAP5-DCA blueprint states that the exam will cover PowerCLI and vCenter Orchestrator (vCO) so as I go work through the blueprint, I intend on becoming very comfortable with many different tasks in PowerCLI.

From what I’ve read about the VCAP5-DCA (VDCA 550 version), the exam environment now contains 5 hosts. Performing the same task on this many hosts in a limited amount of time is a good opportunity to automate as much as possible. I don’t think creating a powershell script for each minute task is a good use of time but over the course of learning PowerCLI, I will post many PowerCLI scripts that coincide with VCAP5-DCA blueprint objectives.

We’ll begin with enabling the software iSCSI initiator on all hosts in the datacenter. If the environment is already setup with NFS storage, there’s a good chance that the the software iSCSI initiator will need to be added to the hosts to connect to iSCSI storage. The script uses a hardcoded username and password since this is a test environment but I wouldn’t recommend doing that in production. To use in your environment, use your username, password, and name of each host. Make sure the name of your hosts matches what’s in vCenter. If it’s in vCenter with a FQDN, mirror the name in the script. Next, open PowerCLI and run the script with .\iSCSI.ps1.

#variables

$vcenter “name”

$username “username”

$password “password”

$listofhosts = “host1”, “host2”, “host3”

#connect to vcenter

write-host “Connecting to vcenter”

connect-viserver $vcenter -user $username -password $password

#Loop through hosts

foreach ($esxhost in $listofhosts)

{

$currentesxhost = get-vmhost $esxhost

write-host “Enabling software iSCSI initiator on $currentesxhost”

get-vmhoststorage $currentesxhost | set-vmhoststorage -softwareiscsienabled $true

}

write-host “Disconnecting from vcenter”

disconnect-viserver $vcenter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s